field and playlist enhancements, producer pixbuf reorg
[melted] / mlt / src / framework / mlt_playlist.c
1 /*
2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "config.h"
22
23 #include "mlt_playlist.h"
24 #include "mlt_frame.h"
25
26 #include <stdlib.h>
27
28 /** Virtual playlist entry.
29 */
30
31 typedef struct
32 {
33 mlt_producer producer;
34 mlt_timecode in;
35 mlt_timecode playtime;
36 }
37 playlist_entry;
38
39 /** Private definition.
40 */
41
42 struct mlt_playlist_s
43 {
44 struct mlt_producer_s parent;
45 struct mlt_producer_s blank;
46
47 int size;
48 int count;
49 playlist_entry **list;
50 };
51
52 /** Forward declarations
53 */
54
55 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
56
57 /** Constructor.
58 */
59
60 mlt_playlist mlt_playlist_init( )
61 {
62 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
63 if ( this != NULL )
64 {
65 mlt_producer producer = &this->parent;
66
67 // Construct the producer
68 mlt_producer_init( producer, this );
69
70 // Override the producer get_frame
71 producer->get_frame = producer_get_frame;
72
73 // Initialise blank
74 mlt_producer_init( &this->blank, NULL );
75 }
76
77 return this;
78 }
79
80 /** Get the producer associated to this playlist.
81 */
82
83 mlt_producer mlt_playlist_producer( mlt_playlist this )
84 {
85 return &this->parent;
86 }
87
88 /** Get the service associated to this playlist.
89 */
90
91 mlt_service mlt_playlist_service( mlt_playlist this )
92 {
93 return mlt_producer_service( &this->parent );
94 }
95
96 /** Append to the virtual playlist.
97 */
98
99 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_timecode in, mlt_timecode out )
100 {
101 // Check that we have room
102 if ( this->count >= this->size )
103 {
104 int i;
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;
108 this->size += 10;
109 }
110
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;
115
116 this->count ++;
117
118 return 0;
119 }
120
121 /** Seek in the virtual playlist.
122 */
123
124 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
125 {
126 // Default producer to blank
127 mlt_producer producer = &this->blank;
128
129 // Map playlist position to real producer in virtual playlist
130 mlt_timecode position = mlt_producer_position( &this->parent );
131
132 // Loop through the virtual playlist
133 int i = 0;
134
135 for ( i = 0; i < this->count; i ++ )
136 {
137 if ( position < this->list[ i ]->playtime )
138 {
139 // Found it, now break
140 producer = this->list[ i ]->producer;
141 position += this->list[ i ]->in;
142 break;
143 }
144 else
145 {
146 // Decrement position by length of this entry
147 position -= this->list[ i ]->playtime;
148 }
149 }
150
151 // Seek in real producer to relative position
152 mlt_producer_seek( producer, position );
153
154 return producer;
155 }
156
157 /** Append a producer to the playlist.
158 */
159
160 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
161 {
162 // Append to virtual list
163 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) );
164 }
165
166 /** Append a blank to the playlist of a given length.
167 */
168
169 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
170 {
171 // Append to the virtual list
172 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
173 }
174
175 /** Get the current frame.
176 */
177
178 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
179 {
180 // Get this mlt_playlist
181 mlt_playlist this = producer->child;
182
183 // Get the real producer
184 mlt_producer real = mlt_playlist_virtual_seek( this );
185
186 // Get the frame
187 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
188
189 // Update timecode on the frame we're creating
190 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
191
192 // Position ourselves on the next frame
193 mlt_producer_prepare_next( producer );
194
195 return 0;
196 }
197
198 /** Close the playlist.
199 */
200
201 void mlt_playlist_close( mlt_playlist this )
202 {
203 mlt_producer_close( &this->parent );
204 mlt_producer_close( &this->blank );
205 free( this );
206 }