21f527746692595142b6e5757227511b021da17d
[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 // 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 );
61 mlt_properties_set_position( properties, "out", 1799999 );
62 mlt_properties_set_position( properties, "length", 1800000 );
63 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
64 mlt_properties_set( properties, "eof", "pause" );
65 mlt_properties_set( properties, "resource", "<producer>" );
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 /** Seek to a specified position.
91 */
92
93 int mlt_producer_seek( mlt_producer this, mlt_position position )
94 {
95 // Determine eof handling
96 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
97
98 // Check bounds
99 if ( position < 0 )
100 position = 0;
101 else if ( !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
102 position = mlt_producer_get_playtime( this ) - 1;
103
104 // Set the position
105 mlt_properties_set_position( mlt_producer_properties( this ), "position", position );
106
107 // Calculate the absolute frame
108 mlt_properties_set_position( mlt_producer_properties( this ), "frame", mlt_producer_get_in( this ) + position );
109
110 return 0;
111 }
112
113 /** Get the current position (relative to in point).
114 */
115
116 mlt_position mlt_producer_position( mlt_producer this )
117 {
118 return mlt_properties_get_position( mlt_producer_properties( this ), "position" );
119 }
120
121 /** Get the current position (relative to start of producer).
122 */
123
124 mlt_position mlt_producer_frame( mlt_producer this )
125 {
126 return mlt_properties_get_position( mlt_producer_properties( this ), "frame" );
127 }
128
129 /** Set the playing speed.
130 */
131
132 int mlt_producer_set_speed( mlt_producer this, double speed )
133 {
134 return mlt_properties_set_double( mlt_producer_properties( this ), "speed", speed );
135 }
136
137 /** Get the playing speed.
138 */
139
140 double mlt_producer_get_speed( mlt_producer this )
141 {
142 return mlt_properties_get_double( mlt_producer_properties( this ), "speed" );
143 }
144
145 /** Get the frames per second.
146 */
147
148 double mlt_producer_get_fps( mlt_producer this )
149 {
150 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
151 }
152
153 /** Set the in and out points.
154 */
155
156 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
157 {
158 // Correct ins and outs if necessary
159 if ( in < 0 )
160 in = 0;
161 else if ( in > mlt_producer_get_length( this ) )
162 in = mlt_producer_get_length( this );
163
164 if ( out < 0 )
165 out = 0;
166 else if ( out > mlt_producer_get_length( this ) )
167 out = mlt_producer_get_length( this );
168
169 // Swap ins and outs if wrong
170 if ( out < in )
171 {
172 mlt_position t = in;
173 in = out;
174 out = t;
175 }
176
177 // Set the values
178 mlt_properties_set_position( mlt_producer_properties( this ), "in", in );
179 mlt_properties_set_position( mlt_producer_properties( this ), "out", out );
180
181 return 0;
182 }
183
184 /** Get the in point.
185 */
186
187 mlt_position mlt_producer_get_in( mlt_producer this )
188 {
189 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
190 }
191
192 /** Get the out point.
193 */
194
195 mlt_position mlt_producer_get_out( mlt_producer this )
196 {
197 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
198 }
199
200 /** Get the total play time.
201 */
202
203 mlt_position mlt_producer_get_playtime( mlt_producer this )
204 {
205 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
206 }
207
208 /** Get the total length of the producer.
209 */
210
211 mlt_position mlt_producer_get_length( mlt_producer this )
212 {
213 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
214 }
215
216 /** Prepare for next frame.
217 */
218
219 void mlt_producer_prepare_next( mlt_producer this )
220 {
221 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
222 }
223
224 /** Get a frame.
225 */
226
227 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
228 {
229 int result = 1;
230 mlt_producer this = service->child;
231
232 // Determine eof handling
233 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
234
235 // A properly instatiated producer will have a get_frame method...
236 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
237 {
238 // Generate a test frame
239 *frame = mlt_frame_init( );
240
241 // Set the position
242 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
243
244 // Calculate the next position
245 mlt_producer_prepare_next( this );
246 }
247 else
248 {
249 // Get the frame from the implementation
250 result = this->get_frame( this, frame, index );
251 }
252
253 // Copy the fps and speed of the producer onto the frame
254 mlt_properties properties = mlt_frame_properties( *frame );
255 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
256 double speed = mlt_producer_get_speed( this );
257 mlt_properties_set_double( properties, "speed", speed );
258
259 return 0;
260 }
261
262 /** Close the producer.
263 */
264
265 void mlt_producer_close( mlt_producer this )
266 {
267 if ( this->close != NULL )
268 this->close( this );
269 else
270 mlt_service_close( &this->parent );
271 }