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