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