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