]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
miracle part 1
[mlt] / src / framework / mlt_producer.c
1 /*
2  * mlt_producer.c -- abstraction for all producer services
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 #include "mlt_producer.h"
23 #include "mlt_frame.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 /** Forward references.
30 */
31
32 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
33
34 /** Constructor
35 */
36
37 int mlt_producer_init( mlt_producer this, void *child )
38 {
39         // Initialise the producer
40         memset( this, 0, sizeof( struct mlt_producer_s ) );
41         
42         // Associate with the child
43         this->child = child;
44
45         // Initialise the service
46         if ( mlt_service_init( &this->parent, this ) == 0 )
47         {
48                 // The parent is the service
49                 mlt_service parent = &this->parent;
50
51                 // Get the properties of the parent
52                 mlt_properties properties = mlt_service_properties( parent );
53
54                 // Set the default properties
55                 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
56                 mlt_properties_set_timecode( properties, "position", 0.0 );
57                 mlt_properties_set_double( properties, "frame", 0 );
58                 mlt_properties_set_double( properties, "fps", 25.0 );
59                 mlt_properties_set_double( properties, "speed", 1.0 );
60                 mlt_properties_set_timecode( properties, "in", 0.0 );
61                 mlt_properties_set_timecode( properties, "out", 36000.0 );
62                 mlt_properties_set_timecode( properties, "length", 36000.0 );
63                 mlt_properties_set_int( properties, "known_length", 1 );
64                 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
65                 mlt_properties_set( properties, "log_id", "multitrack" );
66
67                 // Override service get_frame
68                 parent->get_frame = producer_get_frame;
69         }
70
71         return 0;
72 }
73
74 /** Get the parent service object.
75 */
76
77 mlt_service mlt_producer_service( mlt_producer this )
78 {
79         return &this->parent;
80 }
81
82 /** Get the producer properties.
83 */
84
85 mlt_properties mlt_producer_properties( mlt_producer this )
86 {
87         return mlt_service_properties( &this->parent );
88 }
89
90 /** Seek to a specified time code.
91 */
92
93 int mlt_producer_seek( mlt_producer this, mlt_timecode timecode )
94 {
95         // Check bounds
96         if ( timecode < 0 )
97                 timecode = 0;
98         if ( timecode > mlt_producer_get_playtime( this ) )
99                 timecode = mlt_producer_get_playtime( this );
100
101         // Set the position
102         mlt_properties_set_timecode( mlt_producer_properties( this ), "position", timecode );
103
104         // Calculate the absolute frame
105         double frame = ( mlt_producer_get_in( this ) + timecode ) * mlt_producer_get_fps( this );
106         mlt_properties_set_double( mlt_producer_properties( this ), "frame", floor( frame + 0.5 ) );
107
108         return 0;
109 }
110
111 /** Seek to a specified absolute frame.
112 */
113
114 int mlt_producer_seek_frame( mlt_producer this, uint64_t frame )
115 {
116         // Calculate the time code
117         double timecode = ( frame / mlt_producer_get_fps( this ) ) - mlt_producer_get_in( this );
118
119         // If timecode is invalid, then seek on time
120         if ( timecode < 0 )
121         {
122                 // Seek to the in point
123                 mlt_producer_seek( this, 0 );
124         }
125         else if ( timecode > mlt_producer_get_playtime( this ) )
126         {
127                 // Seek to the out point
128                 mlt_producer_seek( this, mlt_producer_get_playtime( this ) );
129         }
130         else
131         {
132                 // Set the position
133                 mlt_properties_set_timecode( mlt_producer_properties( this ), "position", timecode );
134
135                 // Set the absolute frame
136                 mlt_properties_set_double( mlt_producer_properties( this ), "frame", frame );
137         }
138
139         return 0;
140 }
141
142 /** Get the current time code.
143 */
144
145 mlt_timecode mlt_producer_position( mlt_producer this )
146 {
147         return mlt_properties_get_timecode( mlt_producer_properties( this ), "position" );
148 }
149
150 /** Get the current frame.
151 */
152
153 uint64_t mlt_producer_frame( mlt_producer this )
154 {
155         return mlt_properties_get_double( mlt_producer_properties( this ), "frame" );
156 }
157
158 /** Set the playing speed.
159 */
160
161 int mlt_producer_set_speed( mlt_producer this, double speed )
162 {
163         return mlt_properties_set_double( mlt_producer_properties( this ), "speed", speed );
164 }
165
166 /** Get the playing speed.
167 */
168
169 double mlt_producer_get_speed( mlt_producer this )
170 {
171         return mlt_properties_get_double( mlt_producer_properties( this ), "speed" );
172 }
173
174 /** Get the frames per second.
175 */
176
177 double mlt_producer_get_fps( mlt_producer this )
178 {
179         return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
180 }
181
182 /** Set the in and out points.
183 */
184
185 int mlt_producer_set_in_and_out( mlt_producer this, mlt_timecode in, mlt_timecode out )
186 {
187         // Correct ins and outs if necessary
188         if ( in < 0 )
189                 in = 0;
190         if ( in > mlt_producer_get_length( this ) )
191                 in = mlt_producer_get_length( this );
192         if ( out < 0 )
193                 out = 0;
194         if ( out > mlt_producer_get_length( this ) )
195                 out = mlt_producer_get_length( this );
196
197         // Swap ins and outs if wrong
198         if ( out < in )
199         {
200                 mlt_timecode t = in;
201                 in = out;
202                 out = t;
203         }
204
205         // Set the values
206         mlt_properties_set_timecode( mlt_producer_properties( this ), "in", in );
207         mlt_properties_set_timecode( mlt_producer_properties( this ), "out", out );
208
209         // Seek to the in point
210         mlt_producer_seek( this, 0 );
211
212         return 0;
213 }
214
215 /** Get the in point.
216 */
217
218 mlt_timecode mlt_producer_get_in( mlt_producer this )
219 {
220         return mlt_properties_get_timecode( mlt_producer_properties( this ), "in" );
221 }
222
223 /** Get the out point.
224 */
225
226 mlt_timecode mlt_producer_get_out( mlt_producer this )
227 {
228         return mlt_properties_get_timecode( mlt_producer_properties( this ), "out" );
229 }
230
231 /** Get the total play time.
232 */
233
234 mlt_timecode mlt_producer_get_playtime( mlt_producer this )
235 {
236         return mlt_producer_get_out( this ) - mlt_producer_get_in( this );
237 }
238
239 /** Get the total length of the producer.
240 */
241
242 mlt_timecode mlt_producer_get_length( mlt_producer this )
243 {
244         return mlt_properties_get_timecode( mlt_producer_properties( this ), "length" );
245 }
246
247 /** Prepare for next frame.
248 */
249
250 void mlt_producer_prepare_next( mlt_producer this )
251 {
252         mlt_producer_seek_frame( this, mlt_producer_frame( this ) + mlt_producer_get_speed( this ) );
253 }
254
255 /** Get a frame.
256 */
257
258 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
259 {
260         int result = 1;
261         mlt_producer this = service->child;
262
263         // A properly instatiated producer will have a get_frame method...
264         if ( this->get_frame != NULL )
265         {
266                 // Get the frame from the implementation
267                 result = this->get_frame( this, frame, index );
268
269                 mlt_properties frame_properties = mlt_frame_properties( *frame );
270                 double speed = mlt_producer_get_speed( this );
271                 mlt_properties_set_double( frame_properties, "speed", speed );
272         }
273         else
274         {
275                 // Generate a test frame
276                 *frame = mlt_frame_init( );
277
278                 // Set the timecode
279                 result = mlt_frame_set_timecode( *frame, mlt_producer_position( this ) );
280
281                 // Calculate the next timecode
282                 mlt_producer_prepare_next( this );
283         }
284
285         // Copy the fps of the producer onto the frame
286         mlt_properties properties = mlt_frame_properties( *frame );
287         mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
288
289         return 0;
290 }
291
292 /** Close the producer.
293 */
294
295 void mlt_producer_close( mlt_producer this )
296 {
297         if ( this->close != NULL )
298                 this->close( this );
299         else
300                 mlt_service_close( &this->parent );
301 }