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