]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
Mlt Ref Counts and Playlist split/join
[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_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                         // Define the parent close
62                         parent->close = ( mlt_destructor )mlt_producer_close;
63                         parent->close_object = this;
64
65                         // For convenience, we'll assume the close_object is this
66                         this->close_object = this;
67
68                         // Get the properties of the parent
69                         mlt_properties properties = mlt_service_properties( parent );
70         
71                         // Set the default properties
72                         mlt_properties_set( properties, "mlt_type", "mlt_producer" );
73                         mlt_properties_set_position( properties, "_position", 0.0 );
74                         mlt_properties_set_double( properties, "_frame", 0 );
75                         if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
76                         {
77                                 mlt_properties_set_double( properties, "fps", 25.0 );
78                                 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
79                         }
80                         else
81                         {
82                                 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
83                                 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
84                         }
85                         mlt_properties_set_double( properties, "_speed", 1.0 );
86                         mlt_properties_set_position( properties, "in", 0 );
87                         mlt_properties_set_position( properties, "out", 14999 );
88                         mlt_properties_set_position( properties, "length", 15000 );
89                         mlt_properties_set( properties, "eof", "pause" );
90                         mlt_properties_set( properties, "resource", "<producer>" );
91
92                         // Override service get_frame
93                         parent->get_frame = producer_get_frame;
94                 }
95         }
96
97         return error;
98 }
99
100 /** Create a new producer.
101 */
102
103 mlt_producer mlt_producer_new( )
104 {
105         mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
106         mlt_producer_init( this, NULL );
107         return this;
108 }
109
110 /** Get the parent service object.
111 */
112
113 mlt_service mlt_producer_service( mlt_producer this )
114 {
115         return this != NULL ? &this->parent : NULL;
116 }
117
118 /** Get the producer properties.
119 */
120
121 mlt_properties mlt_producer_properties( mlt_producer this )
122 {
123         return mlt_service_properties( &this->parent );
124 }
125
126 /** Seek to a specified position.
127 */
128
129 int mlt_producer_seek( mlt_producer this, mlt_position position )
130 {
131         // Determine eof handling
132         char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
133         int use_points = 1 - mlt_properties_get_int( mlt_producer_properties( this ), "ignore_points" );
134
135         // Check bounds
136         if ( position < 0 )
137         {
138                 position = 0;
139         }
140         else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
141         {
142                 mlt_producer_set_speed( this, 0 );
143                 position = mlt_producer_get_playtime( this ) - 1;
144         }
145         else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
146         {
147                 position = position % mlt_producer_get_playtime( this );
148         }
149
150         // Set the position
151         mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
152
153         // Calculate the absolute frame
154         mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
155
156         return 0;
157 }
158
159 /** Get the current position (relative to in point).
160 */
161
162 mlt_position mlt_producer_position( mlt_producer this )
163 {
164         return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
165 }
166
167 /** Get the current position (relative to start of producer).
168 */
169
170 mlt_position mlt_producer_frame( mlt_producer this )
171 {
172         return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
173 }
174
175 /** Set the playing speed.
176 */
177
178 int mlt_producer_set_speed( mlt_producer this, double speed )
179 {
180         return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
181 }
182
183 /** Get the playing speed.
184 */
185
186 double mlt_producer_get_speed( mlt_producer this )
187 {
188         return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
189 }
190
191 /** Get the frames per second.
192 */
193
194 double mlt_producer_get_fps( mlt_producer this )
195 {
196         return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
197 }
198
199 /** Set the in and out points.
200 */
201
202 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
203 {
204         // Correct ins and outs if necessary
205         if ( in < 0 )
206                 in = 0;
207         else if ( in > mlt_producer_get_length( this ) )
208                 in = mlt_producer_get_length( this );
209
210         if ( out < 0 )
211                 out = 0;
212         else if ( out > mlt_producer_get_length( this ) )
213                 out = mlt_producer_get_length( this );
214
215         // Swap ins and outs if wrong
216         if ( out < in )
217         {
218                 mlt_position t = in;
219                 in = out;
220                 out = t;
221         }
222
223         // Set the values
224         mlt_properties_set_position( mlt_producer_properties( this ), "in", in );
225         mlt_properties_set_position( mlt_producer_properties( this ), "out", out );
226
227         return 0;
228 }
229
230 /** Get the in point.
231 */
232
233 mlt_position mlt_producer_get_in( mlt_producer this )
234 {
235         return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
236 }
237
238 /** Get the out point.
239 */
240
241 mlt_position mlt_producer_get_out( mlt_producer this )
242 {
243         return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
244 }
245
246 /** Get the total play time.
247 */
248
249 mlt_position mlt_producer_get_playtime( mlt_producer this )
250 {
251         return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
252 }
253
254 /** Get the total length of the producer.
255 */
256
257 mlt_position mlt_producer_get_length( mlt_producer this )
258 {
259         return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
260 }
261
262 /** Prepare for next frame.
263 */
264
265 void mlt_producer_prepare_next( mlt_producer this )
266 {
267         mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
268 }
269
270 /** Get a frame.
271 */
272
273 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
274 {
275         int result = 1;
276         mlt_producer this = service->child;
277
278         // Determine eof handling
279         char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
280
281         // A properly instatiated producer will have a get_frame method...
282         if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
283         {
284                 // Generate a test frame
285                 *frame = mlt_frame_init( );
286
287                 // Set the position
288                 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
289
290                 // Mark as a test card
291                 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
292                 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
293
294                 // Calculate the next position
295                 mlt_producer_prepare_next( this );
296         }
297         else
298         {
299                 // Get the frame from the implementation
300                 result = this->get_frame( this, frame, index );
301         }
302
303         // Copy the fps and speed of the producer onto the frame
304         mlt_properties properties = mlt_frame_properties( *frame );
305         mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
306         double speed = mlt_producer_get_speed( this );
307         mlt_properties_set_double( properties, "_speed", speed );
308         mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
309         mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
310
311         return 0;
312 }
313
314 /** Close the producer.
315 */
316
317 void mlt_producer_close( mlt_producer this )
318 {
319         if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
320         {
321                 this->parent.close = NULL;
322
323                 if ( this->close != NULL )
324                         this->close( this->close_object );
325                 else
326                         mlt_service_close( &this->parent );
327         }
328 }