Extendable factories; general producer related modifications; westley storage; sdl_st...
[melted] / src / framework / mlt_tractor.c
1 /*
2 * mlt_tractor.c -- tractor service class
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
23 #include "mlt_tractor.h"
24 #include "mlt_frame.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 /** Private structure.
33 */
34
35 struct mlt_tractor_s
36 {
37 struct mlt_producer_s parent;
38 mlt_service producer;
39 };
40
41 /** Forward references to static methods.
42 */
43
44 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
45 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this );
46
47 /** Constructor for the tractor.
48 */
49
50 mlt_tractor mlt_tractor_init( )
51 {
52 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
53 if ( this != NULL )
54 {
55 mlt_producer producer = &this->parent;
56 if ( mlt_producer_init( producer, this ) == 0 )
57 {
58 mlt_properties properties = mlt_producer_properties( producer );
59
60 mlt_properties_set( properties, "resource", "<tractor>" );
61 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
62 mlt_properties_set( properties, "mlt_service", "tractor" );
63 mlt_properties_set_int( properties, "in", 0 );
64 mlt_properties_set_int( properties, "out", -1 );
65 mlt_properties_set_int( properties, "length", 0 );
66
67 producer->get_frame = producer_get_frame;
68 producer->close = ( mlt_destructor )mlt_tractor_close;
69 producer->close_object = this;
70 }
71 else
72 {
73 free( this );
74 this = NULL;
75 }
76 }
77 return this;
78 }
79
80 mlt_tractor mlt_tractor_new( )
81 {
82 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
83 if ( this != NULL )
84 {
85 mlt_producer producer = &this->parent;
86 if ( mlt_producer_init( producer, this ) == 0 )
87 {
88 mlt_multitrack multitrack = mlt_multitrack_init( );
89 mlt_field field = mlt_field_new( multitrack, this );
90 mlt_properties props = mlt_producer_properties( producer );
91
92 mlt_properties_set( props, "resource", "<tractor>" );
93 mlt_properties_set( props, "mlt_type", "mlt_producer" );
94 mlt_properties_set( props, "mlt_service", "tractor" );
95 mlt_properties_set_position( props, "in", 0 );
96 mlt_properties_set_position( props, "out", 0 );
97 mlt_properties_set_position( props, "length", 0 );
98 mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
99 mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
100
101 mlt_events_listen( mlt_multitrack_properties( multitrack ), this, "producer-changed", ( mlt_listener )mlt_tractor_listener );
102
103 producer->get_frame = producer_get_frame;
104 producer->close = ( mlt_destructor )mlt_tractor_close;
105 producer->close_object = this;
106 }
107 else
108 {
109 free( this );
110 this = NULL;
111 }
112 }
113 return this;
114 }
115
116 /** Get the service object associated to the tractor.
117 */
118
119 mlt_service mlt_tractor_service( mlt_tractor this )
120 {
121 return mlt_producer_service( &this->parent );
122 }
123
124 /** Get the producer object associated to the tractor.
125 */
126
127 mlt_producer mlt_tractor_producer( mlt_tractor this )
128 {
129 return this != NULL ? &this->parent : NULL;
130 }
131
132 /** Get the properties object associated to the tractor.
133 */
134
135 mlt_properties mlt_tractor_properties( mlt_tractor this )
136 {
137 return mlt_producer_properties( &this->parent );
138 }
139
140 /** Get the field this tractor is harvesting.
141 */
142
143 mlt_field mlt_tractor_field( mlt_tractor this )
144 {
145 return mlt_properties_get_data( mlt_tractor_properties( this ), "field", NULL );
146 }
147
148 /** Get the multitrack this tractor is pulling.
149 */
150
151 mlt_multitrack mlt_tractor_multitrack( mlt_tractor this )
152 {
153 return mlt_properties_get_data( mlt_tractor_properties( this ), "multitrack", NULL );
154 }
155
156 /** Ensure the tractors in/out points match the multitrack.
157 */
158
159 void mlt_tractor_refresh( mlt_tractor this )
160 {
161 mlt_multitrack multitrack = mlt_tractor_multitrack( this );
162 mlt_properties properties = mlt_multitrack_properties( multitrack );
163 mlt_properties self = mlt_tractor_properties( this );
164 mlt_events_block( properties, self );
165 mlt_events_block( self, self );
166 mlt_multitrack_refresh( multitrack );
167 mlt_properties_set_position( self, "in", 0 );
168 mlt_properties_set_position( self, "out", mlt_properties_get_position( properties, "out" ) );
169 mlt_events_unblock( self, self );
170 mlt_events_unblock( properties, self );
171 mlt_properties_set_position( self, "length", mlt_properties_get_position( properties, "length" ) );
172 }
173
174 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this )
175 {
176 mlt_tractor_refresh( this );
177 }
178
179 /** Connect the tractor.
180 */
181
182 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
183 {
184 int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
185
186 // This is the producer we're going to connect to
187 if ( ret == 0 )
188 this->producer = producer;
189
190 return ret;
191 }
192
193 /** Set the producer for a specific track.
194 */
195
196 int mlt_tractor_set_track( mlt_tractor this, mlt_producer producer, int index )
197 {
198 return mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer, index );
199 }
200
201 /** Get the producer for a specific track.
202 */
203
204 mlt_producer mlt_tractor_get_track( mlt_tractor this, int index )
205 {
206 return mlt_multitrack_track( mlt_tractor_multitrack( this ), index );
207 }
208
209 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
210 {
211 uint8_t *data = NULL;
212 mlt_properties properties = mlt_frame_properties( this );
213 mlt_frame frame = mlt_frame_pop_service( this );
214 mlt_properties frame_properties = mlt_frame_properties( frame );
215 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( properties, "width" ) );
216 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( properties, "height" ) );
217 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
218 if ( mlt_properties_get( properties, "distort" ) )
219 mlt_properties_set( frame_properties, "distort", mlt_properties_get( properties, "distort" ) );
220 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
221 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_double( properties, "consumer_deinterlace" ) );
222 mlt_properties_set_int( frame_properties, "normalised_width", mlt_properties_get_double( properties, "normalised_width" ) );
223 mlt_properties_set_int( frame_properties, "normalised_height", mlt_properties_get_double( properties, "normalised_height" ) );
224 mlt_frame_get_image( frame, buffer, format, width, height, writable );
225 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
226 mlt_properties_set_int( properties, "width", *width );
227 mlt_properties_set_int( properties, "height", *height );
228 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( frame ) );
229 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
230 if ( mlt_properties_get( frame_properties, "distort" ) )
231 mlt_properties_set( properties, "distort", mlt_properties_get( frame_properties, "distort" ) );
232 data = mlt_frame_get_alpha_mask( frame );
233 mlt_properties_set_data( properties, "alpha", data, 0, NULL, NULL );
234 return 0;
235 }
236
237 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
238 {
239 mlt_properties properties = mlt_frame_properties( this );
240 mlt_frame frame = mlt_frame_pop_audio( this );
241 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
242 mlt_properties_set_data( properties, "audio", *buffer, 0, NULL, NULL );
243 mlt_properties_set_int( properties, "frequency", *frequency );
244 mlt_properties_set_int( properties, "channels", *channels );
245 return 0;
246 }
247
248 /** Get the next frame.
249 */
250
251 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
252 {
253 mlt_tractor this = parent->child;
254
255 // We only respond to the first track requests
256 if ( track == 0 && this->producer != NULL )
257 {
258 int i = 0;
259 int done = 0;
260 mlt_frame temp = NULL;
261 int count = 0;
262
263 // Get the properties of the parent producer
264 mlt_properties properties = mlt_producer_properties( parent );
265
266 // Try to obtain the multitrack associated to the tractor
267 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
268
269 // Or a specific producer
270 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
271
272 // If we don't have one, we're in trouble...
273 if ( multitrack != NULL )
274 {
275 // Used to garbage collect all frames
276 char label[ 30 ];
277
278 // Get the id of the tractor
279 char *id = mlt_properties_get( properties, "_unique_id" );
280
281 // Will be used to store the frame properties object
282 mlt_properties frame_properties = NULL;
283
284 // We'll store audio and video frames to use here
285 mlt_frame audio = NULL;
286 mlt_frame video = NULL;
287
288 // Determine which data_queue to pass on...
289 void *data_queue = NULL;
290
291 // Temporary properties
292 mlt_properties temp_properties = NULL;
293
294 // Get the multitrack's producer
295 mlt_producer target = mlt_multitrack_producer( multitrack );
296 mlt_producer_seek( target, mlt_producer_frame( parent ) );
297 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
298
299 // We will create one frame and attach everything to it
300 *frame = mlt_frame_init( );
301
302 // Get the properties of the frame
303 frame_properties = mlt_frame_properties( *frame );
304
305 // Loop through each of the tracks we're harvesting
306 for ( i = 0; !done; i ++ )
307 {
308 // Get a frame from the producer
309 mlt_service_get_frame( this->producer, &temp, i );
310
311 // Get the temporary properties
312 temp_properties = mlt_frame_properties( temp );
313
314 // Check for last track
315 done = mlt_properties_get_int( temp_properties, "last_track" );
316
317 // We store all frames with a destructor on the output frame
318 sprintf( label, "_%s_%d", id, count ++ );
319 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
320
321 // We want the last data_queue
322 if ( !done && mlt_properties_get_data( temp_properties, "data_queue", NULL ) != NULL )
323 data_queue = mlt_properties_get_data( mlt_frame_properties( temp ), "data_queue", NULL );
324
325 // Pick up first video and audio frames
326 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 2 ) )
327 audio = temp;
328 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
329 video = temp;
330 }
331
332 // Now stack callbacks
333 if ( audio != NULL )
334 {
335 mlt_frame_push_audio( *frame, audio );
336 ( *frame )->get_audio = producer_get_audio;
337 }
338
339 if ( video != NULL )
340 {
341 mlt_properties video_properties = mlt_frame_properties( video );
342 mlt_frame_push_service( *frame, video );
343 mlt_frame_push_service( *frame, producer_get_image );
344 mlt_properties_set_data( frame_properties, "data_queue", data_queue, 0, NULL, NULL );
345 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( video_properties, "width" ) );
346 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( video_properties, "height" ) );
347 mlt_properties_set_int( frame_properties, "real_width", mlt_properties_get_int( video_properties, "real_width" ) );
348 mlt_properties_set_int( frame_properties, "real_height", mlt_properties_get_int( video_properties, "real_height" ) );
349 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( video_properties, "progressive" ) );
350 mlt_properties_set_double( frame_properties, "aspect_ratio", mlt_properties_get_double( video_properties, "aspect_ratio" ) );
351 }
352
353 mlt_frame_set_position( *frame, mlt_producer_frame( parent ) );
354 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", audio == NULL );
355 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", video == NULL );
356 }
357 else if ( producer != NULL )
358 {
359 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
360 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
361 mlt_service_get_frame( this->producer, frame, track );
362 }
363 else
364 {
365 fprintf( stderr, "tractor without a multitrack!!\n" );
366 mlt_service_get_frame( this->producer, frame, track );
367 }
368
369 // Prepare the next frame
370 mlt_producer_prepare_next( parent );
371
372 // Indicate our found status
373 return 0;
374 }
375 else
376 {
377 // Generate a test card
378 *frame = mlt_frame_init( );
379 return 0;
380 }
381 }
382
383 /** Close the tractor.
384 */
385
386 void mlt_tractor_close( mlt_tractor this )
387 {
388 if ( this != NULL && mlt_properties_dec_ref( mlt_tractor_properties( this ) ) <= 0 )
389 {
390 this->parent.close = NULL;
391 mlt_producer_close( &this->parent );
392 free( this );
393 }
394 }
395