2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "mlt_playlist.h"
24 #include "mlt_frame.h"
28 /** Virtual playlist entry.
33 mlt_producer producer
;
35 mlt_timecode playtime
;
39 /** Private definition.
44 struct mlt_producer_s parent
;
45 struct mlt_producer_s blank
;
49 playlist_entry
**list
;
52 /** Forward declarations
55 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
60 mlt_playlist
mlt_playlist_init( )
62 mlt_playlist
this = calloc( sizeof( struct mlt_playlist_s
), 1 );
65 mlt_producer producer
= &this->parent
;
67 // Construct the producer
68 mlt_producer_init( producer
, this );
70 // Override the producer get_frame
71 producer
->get_frame
= producer_get_frame
;
74 mlt_producer_init( &this->blank
, NULL
);
80 /** Get the producer associated to this playlist.
83 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
88 /** Get the service associated to this playlist.
91 mlt_service
mlt_playlist_service( mlt_playlist
this )
93 return mlt_producer_service( &this->parent
);
96 /** Append to the virtual playlist.
99 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_timecode in
, mlt_timecode out
)
101 // Check that we have room
102 if ( this->count
>= this->size
)
105 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
106 for ( i
= this->size
; i
< this->size
+ 10; i
++ )
107 this->list
[ i
] = NULL
;
111 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
112 this->list
[ this->count
]->producer
= producer
;
113 this->list
[ this->count
]->in
= in
;
114 this->list
[ this->count
]->playtime
= out
- in
;
121 /** Seek in the virtual playlist.
124 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
126 // Default producer to blank
127 mlt_producer producer
= &this->blank
;
129 // Map playlist position to real producer in virtual playlist
130 mlt_timecode position
= mlt_producer_position( &this->parent
);
132 // Loop through the virtual playlist
135 for ( i
= 0; i
< this->count
; i
++ )
137 if ( position
< this->list
[ i
]->playtime
)
139 // Found it, now break
140 producer
= this->list
[ i
]->producer
;
141 position
+= this->list
[ i
]->in
;
146 // Decrement position by length of this entry
147 position
-= this->list
[ i
]->playtime
;
151 // Seek in real producer to relative position
152 mlt_producer_seek( producer
, position
);
157 /** Append a producer to the playlist.
160 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
162 // Append to virtual list
163 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) );
166 /** Append a blank to the playlist of a given length.
169 int mlt_playlist_blank( mlt_playlist
this, mlt_timecode length
)
171 // Append to the virtual list
172 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
175 /** Get the current frame.
178 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
180 // Get this mlt_playlist
181 mlt_playlist
this = producer
->child
;
183 // Get the real producer
184 mlt_producer real
= mlt_playlist_virtual_seek( this );
187 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
189 // Update timecode on the frame we're creating
190 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
192 // Position ourselves on the next frame
193 mlt_producer_prepare_next( producer
);
198 /** Close the playlist.
201 void mlt_playlist_close( mlt_playlist
this )
203 mlt_producer_close( &this->parent
);
204 mlt_producer_close( &this->blank
);