]> git.sesse.net Git - mlt/blobdiff - src/framework/mlt_filter.c
Return a sensible size for glsl image types.
[mlt] / src / framework / mlt_filter.c
index 3df813fd9951545ab5a1d8f26c387e45b0d54a02..29be4a66dab0a3376be18c917dd6a7a7d33f79cc 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "mlt_filter.h"
 #include "mlt_frame.h"
+#include "mlt_producer.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -73,9 +74,15 @@ int mlt_filter_init( mlt_filter self, void *child )
 mlt_filter mlt_filter_new( )
 {
        mlt_filter self = calloc( 1, sizeof( struct mlt_filter_s ) );
-       if ( self != NULL )
-               mlt_filter_init( self, NULL );
-       return self;
+       if ( self != NULL && mlt_filter_init( self, NULL ) == 0 )
+       {
+               return self;
+       }
+       else
+       {
+               free(self);
+               return NULL;
+       }
 }
 
 /** Get the service class interface.
@@ -202,29 +209,84 @@ mlt_position mlt_filter_get_length( mlt_filter self )
        return ( out > 0 ) ? ( out - in + 1 ) : 0;
 }
 
-/** Get the relative position of a frame.
+/** Get the duration.
+ *
+ * This version works with filters with no explicit in and out by getting the
+ * length of the frame's producer.
  *
  * \public \memberof mlt_filter_s
  * \param self a filter
- * \param frame a frame
- * \return the progress in the range 0.0 to 1.0
+ * \param frame a frame from which to get its producer
+ * \return the duration or zero if unlimited
  */
 
-double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
+mlt_position mlt_filter_get_length2( mlt_filter self, mlt_frame frame )
 {
-       double progress = 0;
-       mlt_position out = mlt_filter_get_out( self );
+       mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
+       mlt_position in = mlt_properties_get_position( properties, "in" );
+       mlt_position out = mlt_properties_get_position( properties, "out" );
 
-       if ( out != 0 )
+       if ( out == 0 && frame )
        {
-               mlt_position in = mlt_filter_get_in( self );
-               mlt_position position = mlt_frame_get_position( frame );
-               progress = ( double ) ( position - in ) / ( double ) ( out - in + 1 );
+               // If always active, use the frame's producer
+               mlt_producer producer = mlt_frame_get_original_producer( frame );
+               if ( producer )
+               {
+                       producer = mlt_producer_cut_parent( producer );
+                       in = mlt_producer_get_in( producer );
+                       out = mlt_producer_get_out( producer );
+               }
        }
-       return progress;
+       return ( out > 0 ) ? ( out - in + 1 ) : 0;
+}
+
+/** Get the position within the filter.
+ *
+ * The position is relative to the in point.
+ * This will only be valid once mlt_filter_process is called.
+ *
+ * \public \memberof mlt_filter_s
+ * \param self a filter
+ * \param frame a frame
+ * \return the position
+ */
+
+mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
+{
+       mlt_properties properties = MLT_FILTER_PROPERTIES( self );
+       mlt_position in = mlt_properties_get_position( properties, "in" );
+       const char *unique_id = mlt_properties_get( properties, "_unique_id" );
+       char name[20];
+
+       // Make the properties key from unique id
+       snprintf( name, 20, "pos.%s", unique_id );
+       name[20 - 1] = '\0';
+
+       return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
+}
+
+/** Get the percent complete.
+ *
+ * This will only be valid once mlt_filter_process is called.
+ *
+ * \public \memberof mlt_filter_s
+ * \param self a filter
+ * \param frame a frame
+ * \return the progress in the range 0.0 to 1.0
+ */
+
+double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
+{
+       double position = mlt_filter_get_position( self, frame );
+       double length = mlt_filter_get_length2( self, frame );
+       return position / length;
 }
 
 /** Process the frame.
+ *
+ * When fetching the frame position in a subclass process method, the frame's
+ * position is relative to the filter's producer - not the filter's in point
+ * or timeline.
  *
  * \public \memberof mlt_filter_s
  * \param self a filter
@@ -235,11 +297,34 @@ double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
 
 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
 {
-       int disable = mlt_properties_get_int( MLT_FILTER_PROPERTIES( self ), "disable" );
+       mlt_properties properties = MLT_FILTER_PROPERTIES( self );
+       int disable = mlt_properties_get_int( properties, "disable" );
+       const char *unique_id = mlt_properties_get( properties, "_unique_id" );
+       mlt_position position = mlt_frame_get_position( frame );
+       char name[30];
+
+       // Make the properties key from unique id
+       snprintf( name, sizeof(name), "pos.%s", unique_id );
+       name[sizeof(name) -1] = '\0';
+
+       // Save the position on the frame
+       mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
+
        if ( disable || self->process == NULL )
+       {
                return frame;
+       }
        else
+       {
+               // Add a reference to this filter on the frame
+               mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(self) );
+               snprintf( name, sizeof(name), "filter.%s", unique_id );
+               name[sizeof(name) -1] = '\0';
+               mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), name, self, 0,
+                       (mlt_destructor) mlt_filter_close, NULL );
+
                return self->process( self, frame );
+       }
 }
 
 /** Get a frame from this filter.