]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
int64 based comms and more unit functionality
[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 /** Convert frame position to timecode.
91 */
92
93 mlt_timecode mlt_producer_time( mlt_producer this, int64_t frame )
94 {
95         if ( frame < 0 )
96                 return -1;
97         else
98                 return ( mlt_timecode )frame / mlt_producer_get_fps( this );
99 }
100
101 /** Convert timecode to frame position.
102 */
103
104 int64_t mlt_producer_frame_position( mlt_producer this, mlt_timecode position )
105 {
106         if ( position < 0 )
107                 return -1;
108         else
109                 return ( int64_t )( floor( position * mlt_producer_get_fps( this ) + 0.5 ) );
110 }
111
112 /** Seek to a specified time code.
113 */
114
115 int mlt_producer_seek( mlt_producer this, mlt_timecode timecode )
116 {
117         // Check bounds
118         if ( timecode < 0 )
119                 timecode = 0;
120         if ( timecode > mlt_producer_get_playtime( this ) )
121                 timecode = mlt_producer_get_playtime( this );
122
123         // Set the position
124         mlt_properties_set_timecode( mlt_producer_properties( this ), "position", timecode );
125
126         // Calculate the absolute frame
127         double frame = ( mlt_producer_get_in( this ) + timecode ) * mlt_producer_get_fps( this );
128         mlt_properties_set_double( mlt_producer_properties( this ), "frame", floor( frame + 0.5 ) );
129
130         return 0;
131 }
132
133 /** Seek to a specified absolute frame.
134 */
135
136 int mlt_producer_seek_frame( mlt_producer this, uint64_t frame )
137 {
138         // Calculate the time code
139         double timecode = ( frame / mlt_producer_get_fps( this ) ) - mlt_producer_get_in( this );
140
141         // If timecode is invalid, then seek on time
142         if ( timecode < 0 )
143         {
144                 // Seek to the in point
145                 mlt_producer_seek( this, 0 );
146         }
147         else if ( timecode > mlt_producer_get_playtime( this ) )
148         {
149                 // Seek to the out point
150                 mlt_producer_seek( this, mlt_producer_get_playtime( this ) );
151         }
152         else
153         {
154                 // Set the position
155                 mlt_properties_set_timecode( mlt_producer_properties( this ), "position", timecode );
156
157                 // Set the absolute frame
158                 mlt_properties_set_double( mlt_producer_properties( this ), "frame", frame );
159         }
160
161         return 0;
162 }
163
164 /** Get the current time code.
165 */
166
167 mlt_timecode mlt_producer_position( mlt_producer this )
168 {
169         return mlt_properties_get_timecode( mlt_producer_properties( this ), "position" );
170 }
171
172 /** Get the current frame.
173 */
174
175 uint64_t mlt_producer_frame( mlt_producer this )
176 {
177         return mlt_properties_get_double( mlt_producer_properties( this ), "frame" );
178 }
179
180 /** Set the playing speed.
181 */
182
183 int mlt_producer_set_speed( mlt_producer this, double speed )
184 {
185         return mlt_properties_set_double( mlt_producer_properties( this ), "speed", speed );
186 }
187
188 /** Get the playing speed.
189 */
190
191 double mlt_producer_get_speed( mlt_producer this )
192 {
193         return mlt_properties_get_double( mlt_producer_properties( this ), "speed" );
194 }
195
196 /** Get the frames per second.
197 */
198
199 double mlt_producer_get_fps( mlt_producer this )
200 {
201         return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
202 }
203
204 /** Set the in and out points.
205 */
206
207 int mlt_producer_set_in_and_out( mlt_producer this, mlt_timecode in, mlt_timecode out )
208 {
209         // Correct ins and outs if necessary
210         if ( in < 0 )
211                 in = 0;
212         if ( in > mlt_producer_get_length( this ) )
213                 in = mlt_producer_get_length( this );
214         if ( out < 0 )
215                 out = 0;
216         if ( out > mlt_producer_get_length( this ) )
217                 out = mlt_producer_get_length( this );
218
219         // Swap ins and outs if wrong
220         if ( out < in )
221         {
222                 mlt_timecode t = in;
223                 in = out;
224                 out = t;
225         }
226
227         // Set the values
228         mlt_properties_set_timecode( mlt_producer_properties( this ), "in", in );
229         mlt_properties_set_timecode( mlt_producer_properties( this ), "out", out );
230
231         // Seek to the in point
232         mlt_producer_seek( this, 0 );
233
234         return 0;
235 }
236
237 /** Get the in point.
238 */
239
240 mlt_timecode mlt_producer_get_in( mlt_producer this )
241 {
242         return mlt_properties_get_timecode( mlt_producer_properties( this ), "in" );
243 }
244
245 /** Get the out point.
246 */
247
248 mlt_timecode mlt_producer_get_out( mlt_producer this )
249 {
250         return mlt_properties_get_timecode( mlt_producer_properties( this ), "out" );
251 }
252
253 /** Get the total play time.
254 */
255
256 mlt_timecode mlt_producer_get_playtime( mlt_producer this )
257 {
258         return mlt_producer_get_out( this ) - mlt_producer_get_in( this );
259 }
260
261 /** Get the total length of the producer.
262 */
263
264 mlt_timecode mlt_producer_get_length( mlt_producer this )
265 {
266         return mlt_properties_get_timecode( mlt_producer_properties( this ), "length" );
267 }
268
269 /** Prepare for next frame.
270 */
271
272 void mlt_producer_prepare_next( mlt_producer this )
273 {
274         mlt_producer_seek_frame( this, mlt_producer_frame( this ) + mlt_producer_get_speed( this ) );
275 }
276
277 /** Get a frame.
278 */
279
280 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
281 {
282         int result = 1;
283         mlt_producer this = service->child;
284
285         // A properly instatiated producer will have a get_frame method...
286         if ( this->get_frame != NULL )
287         {
288                 // Get the frame from the implementation
289                 result = this->get_frame( this, frame, index );
290
291                 mlt_properties frame_properties = mlt_frame_properties( *frame );
292                 double speed = mlt_producer_get_speed( this );
293                 mlt_properties_set_double( frame_properties, "speed", speed );
294         }
295         else
296         {
297                 // Generate a test frame
298                 *frame = mlt_frame_init( );
299
300                 // Set the timecode
301                 result = mlt_frame_set_timecode( *frame, mlt_producer_position( this ) );
302
303                 // Calculate the next timecode
304                 mlt_producer_prepare_next( this );
305         }
306
307         // Copy the fps of the producer onto the frame
308         mlt_properties properties = mlt_frame_properties( *frame );
309         mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
310
311         return 0;
312 }
313
314 /** Close the producer.
315 */
316
317 void mlt_producer_close( mlt_producer this )
318 {
319         if ( this->close != NULL )
320                 this->close( this );
321         else
322                 mlt_service_close( &this->parent );
323 }