Simplified playlist access
[melted] / src / framework / mlt_playlist.c
index e5875ca..ddcc28f 100644 (file)
@@ -286,52 +286,59 @@ static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source,
        return mlt_playlist_virtual_refresh( this );
 }
 
-/** Seek in the virtual playlist.
-*/
-
-static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
+static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
 {
-       // Default producer to blank
+       // Default producer to NULL
        mlt_producer producer = NULL;
 
-       // Map playlist position to real producer in virtual playlist
-       mlt_position position = mlt_producer_frame( &this->parent );
-
-       mlt_position original = position;
-
-       // Total number of frames
-       int64_t total = 0;
-
-       // Get the properties
-       mlt_properties properties = mlt_playlist_properties( this );
-
-       // Get the eof handling
-       char *eof = mlt_properties_get( properties, "eof" );
-
-       // Index for the main loop
-       int i = 0;
-
        // Loop for each producer until found
-       for ( i = 0; i < this->count; i ++ )
+       for ( *clip = 0; *clip < this->count; *clip += 1 )
        {
                // Increment the total
-               total += this->list[ i ]->frame_count;
+               *total += this->list[ *clip ]->frame_count;
 
                // Check if the position indicates that we have found the clip
                // Note that 0 length clips get skipped automatically
-               if ( position < this->list[ i ]->frame_count )
+               if ( *position < this->list[ *clip ]->frame_count )
                {
                        // Found it, now break
-                       producer = this->list[ i ]->producer;
+                       producer = this->list[ *clip ]->producer;
                        break;
                }
                else
                {
                        // Decrement position by length of this entry
-                       position -= this->list[ i ]->frame_count;
+                       *position -= this->list[ *clip ]->frame_count;
                }
        }
 
+       return producer;
+}
+
+/** Seek in the virtual playlist.
+*/
+
+static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
+{
+       // Map playlist position to real producer in virtual playlist
+       mlt_position position = mlt_producer_frame( &this->parent );
+
+       // Keep the original position since we change it while iterating through the list
+       mlt_position original = position;
+
+       // Clip index and total
+       int i = 0;
+       int total = 0;
+
+       // Locate the producer for the position
+       mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
+
+       // Get the properties
+       mlt_properties properties = mlt_playlist_properties( this );
+
+       // Get the eof handling
+       char *eof = mlt_properties_get( properties, "eof" );
+
        // Seek in real producer to relative position
        if ( producer != NULL )
        {
@@ -828,16 +835,25 @@ int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition tr
        {
                playlist_entry *clip_a = this->list[ clip ];
                playlist_entry *clip_b = this->list[ clip + 1 ];
-               mlt_producer track_a;
-               mlt_producer track_b;
+               mlt_producer track_a = NULL;
+               mlt_producer track_b = NULL;
                mlt_tractor tractor = mlt_tractor_new( );
                mlt_events_block( mlt_playlist_properties( this ), this );
 
-               // TODO: Check length is valid for both clips and resize if necessary.
+               // Check length is valid for both clips and resize if necessary.
+               int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
+               length = length > max_size ? max_size : length;
 
-               // Create the a and b tracks/cuts
-               track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
-               track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
+               // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
+               if ( length != clip_a->frame_count )
+                       track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
+               else
+                       track_a = clip_a->producer;
+
+               if ( length != clip_b->frame_count )
+                       track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
+               else
+                       track_b = clip_b->producer;
 
                // Set the tracks on the tractor
                mlt_tractor_set_track( tractor, track_a, 0 );
@@ -855,8 +871,19 @@ int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition tr
                        mlt_transition_set_in_and_out( transition, 0, length - 1 );
                }
 
+               // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
+               if ( track_a != clip_a->producer )
+                       mlt_producer_close( track_a );
+               if ( track_b != clip_b->producer )
+                       mlt_producer_close( track_b );
+
                // Check if we have anything left on the right hand clip
-               if ( clip_b->frame_out - clip_b->frame_in > length )
+               if ( track_b == clip_b->producer )
+               {
+                       clip_b->preservation_hack = 1;
+                       mlt_playlist_remove( this, clip + 2 );
+               }
+               else if ( clip_b->frame_out - clip_b->frame_in > length )
                {
                        mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
                        mlt_properties_set_data( mlt_producer_properties( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
@@ -869,7 +896,12 @@ int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition tr
                }
 
                // Check if we have anything left on the left hand clip
-               if ( clip_a->frame_out - clip_a->frame_in > length )
+               if ( track_a == clip_a->producer )
+               {
+                       clip_a->preservation_hack = 1;
+                       mlt_playlist_remove( this, clip );
+               }
+               else if ( clip_a->frame_out - clip_a->frame_in > length )
                {
                        mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
                        mlt_properties_set_data( mlt_producer_properties( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
@@ -881,10 +913,9 @@ int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition tr
                        mlt_playlist_remove( this, clip );
                }
 
+               // Unblock and force a fire off of change events to listeners
                mlt_events_unblock( mlt_playlist_properties( this ), this );
                mlt_playlist_virtual_refresh( this );
-               mlt_producer_close( track_a );
-               mlt_producer_close( track_b );
                mlt_tractor_close( tractor );
        }
        return error;
@@ -908,7 +939,7 @@ int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition
        return error;
 }
 
-/** Return the clip at the position.
+/** Return the clip at the clip index.
 */
 
 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
@@ -918,6 +949,25 @@ mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
        return NULL;
 }
 
+/** Return the clip at the specified position.
+*/
+
+mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, int position )
+{
+       int index = 0, total = 0;
+       return mlt_playlist_locate( this, &position, &index, &total );
+}
+
+/** Return the clip index of the specified position.
+*/
+
+int mlt_playlist_get_clip_index_at( mlt_playlist this, int position )
+{
+       int index = 0, total = 0;
+       mlt_playlist_locate( this, &position, &index, &total );
+       return index;
+}
+
 /** Determine if the clip is a mix.
 */
 
@@ -1054,7 +1104,7 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
        // Set the consumer progressive property
        if ( progressive )
        {
-               mlt_properties_set_int( properties, "consumer_progressive", progressive );
+               mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
                mlt_properties_set_int( properties, "test_audio", 1 );
        }