3 * \brief tractor service class
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_tractor.h"
25 #include "mlt_frame.h"
26 #include "mlt_multitrack.h"
27 #include "mlt_field.h"
35 /* Forward references to static methods.
38 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track );
39 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor self );
41 /** Construct a tractor without a field or multitrack.
43 * Sets the resource property to "<tractor>", the mlt_type to "mlt_producer",
44 * and mlt_service to "tractor".
46 * \public \memberof mlt_tractor_s
47 * \return the new tractor
50 mlt_tractor mlt_tractor_init( )
52 mlt_tractor self = calloc( sizeof( struct mlt_tractor_s ), 1 );
55 mlt_producer producer = &self->parent;
56 if ( mlt_producer_init( producer, self ) == 0 )
58 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
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 );
67 producer->get_frame = producer_get_frame;
68 producer->close = ( mlt_destructor )mlt_tractor_close;
69 producer->close_object = self;
80 /** Construct a tractor as well as a field and multitrack.
82 * Sets the resource property to "<tractor>", the mlt_type to "mlt_producer",
83 * and mlt_service to "tractor".
85 * \public \memberof mlt_tractor_s
86 * \return the new tractor
89 mlt_tractor mlt_tractor_new( )
91 mlt_tractor self = calloc( sizeof( struct mlt_tractor_s ), 1 );
94 mlt_producer producer = &self->parent;
95 if ( mlt_producer_init( producer, self ) == 0 )
97 mlt_multitrack multitrack = mlt_multitrack_init( );
98 mlt_field field = mlt_field_new( multitrack, self );
99 mlt_properties props = MLT_PRODUCER_PROPERTIES( producer );
101 mlt_properties_set( props, "resource", "<tractor>" );
102 mlt_properties_set( props, "mlt_type", "mlt_producer" );
103 mlt_properties_set( props, "mlt_service", "tractor" );
104 mlt_properties_set_position( props, "in", 0 );
105 mlt_properties_set_position( props, "out", 0 );
106 mlt_properties_set_position( props, "length", 0 );
107 mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
108 mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
110 mlt_events_listen( MLT_MULTITRACK_PROPERTIES( multitrack ), self, "producer-changed", ( mlt_listener )mlt_tractor_listener );
112 producer->get_frame = producer_get_frame;
113 producer->close = ( mlt_destructor )mlt_tractor_close;
114 producer->close_object = self;
125 /** Get the service object associated to the tractor.
127 * \public \memberof mlt_tractor_s
128 * \param self a tractor
129 * \return the parent service object
130 * \see MLT_TRACTOR_SERVICE
133 mlt_service mlt_tractor_service( mlt_tractor self )
135 return MLT_PRODUCER_SERVICE( &self->parent );
138 /** Get the producer object associated to the tractor.
140 * \public \memberof mlt_tractor_s
141 * \param self a tractor
142 * \return the parent producer object
143 * \see MLT_TRACTOR_PRODUCER
146 mlt_producer mlt_tractor_producer( mlt_tractor self )
148 return self != NULL ? &self->parent : NULL;
151 /** Get the properties object associated to the tractor.
153 * \public \memberof mlt_tractor_s
154 * \param self a tractor
155 * \return the tractor's property list
156 * \see MLT_TRACTOR_PROPERTIES
159 mlt_properties mlt_tractor_properties( mlt_tractor self )
161 return MLT_PRODUCER_PROPERTIES( &self->parent );
164 /** Get the field self tractor is harvesting.
166 * \public \memberof mlt_tractor_s
167 * \param self a tractor
168 * \return a field or NULL if there is no field for this tractor
171 mlt_field mlt_tractor_field( mlt_tractor self )
173 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( self ), "field", NULL );
176 /** Get the multitrack a tractor is pulling.
178 * \public \memberof mlt_tractor_s
179 * \param self a tractor
180 * \return a multitrack or NULL if there is none
183 mlt_multitrack mlt_tractor_multitrack( mlt_tractor self )
185 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( self ), "multitrack", NULL );
188 /** Ensure the tractors in/out points match the multitrack.
190 * \public \memberof mlt_tractor_s
191 * \param self a tractor
194 void mlt_tractor_refresh( mlt_tractor self )
196 mlt_multitrack multitrack = mlt_tractor_multitrack( self );
197 mlt_properties multitrack_props = MLT_MULTITRACK_PROPERTIES( multitrack );
198 mlt_properties properties = MLT_TRACTOR_PROPERTIES( self );
199 mlt_events_block( multitrack_props, properties );
200 mlt_events_block( properties, properties );
201 mlt_multitrack_refresh( multitrack );
202 mlt_properties_set_position( properties, "in", 0 );
203 mlt_properties_set_position( properties, "out", mlt_properties_get_position( multitrack_props, "out" ) );
204 mlt_events_unblock( properties, properties );
205 mlt_events_unblock( multitrack_props, properties );
206 mlt_properties_set_position( properties, "length", mlt_properties_get_position( multitrack_props, "length" ) );
209 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor self )
211 mlt_tractor_refresh( self );
214 /** Connect the tractor.
216 * \public \memberof mlt_tractor_s
217 * \param self a tractor
218 * \param producer a producer
219 * \return true on error
222 int mlt_tractor_connect( mlt_tractor self, mlt_service producer )
224 int ret = mlt_service_connect_producer( MLT_TRACTOR_SERVICE( self ), producer, 0 );
226 // This is the producer we're going to connect to
228 self->producer = producer;
233 /** Set the producer for a specific track.
235 * \public \memberof mlt_tractor_s
236 * \param self a tractor
237 * \param producer a producer
238 * \param index the 0-based track index
239 * \return true on error
242 int mlt_tractor_set_track( mlt_tractor self, mlt_producer producer, int index )
244 return mlt_multitrack_connect( mlt_tractor_multitrack( self ), producer, index );
247 /** Get the producer for a specific track.
249 * \public \memberof mlt_tractor_s
250 * \param self a tractor
251 * \param index the 0-based track index
252 * \return the producer for track \p index
255 mlt_producer mlt_tractor_get_track( mlt_tractor self, int index )
257 return mlt_multitrack_track( mlt_tractor_multitrack( self ), index );
260 static int producer_get_image( mlt_frame self, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
262 uint8_t *data = NULL;
264 mlt_properties properties = MLT_FRAME_PROPERTIES( self );
265 mlt_frame frame = mlt_frame_pop_service( self );
266 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
267 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
268 mlt_properties_set_int( frame_properties, "resize_alpha", mlt_properties_get_int( properties, "resize_alpha" ) );
269 mlt_properties_set_int( frame_properties, "distort", mlt_properties_get_int( properties, "distort" ) );
270 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
271 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "consumer_deinterlace" ) );
272 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
273 mlt_properties_set_int( frame_properties, "normalised_width", mlt_properties_get_int( properties, "normalised_width" ) );
274 mlt_properties_set_int( frame_properties, "normalised_height", mlt_properties_get_int( properties, "normalised_height" ) );
275 mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "consumer_tff" ) );
276 mlt_frame_get_image( frame, buffer, format, width, height, writable );
277 mlt_frame_set_image( self, *buffer, 0, NULL );
278 mlt_properties_set_int( properties, "width", *width );
279 mlt_properties_set_int( properties, "height", *height );
280 mlt_properties_set_int( properties, "format", *format );
281 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( frame ) );
282 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
283 mlt_properties_set_int( properties, "distort", mlt_properties_get_int( frame_properties, "distort" ) );
284 mlt_properties_set_int( properties, "colorspace", mlt_properties_get_int( frame_properties, "colorspace" ) );
285 mlt_properties_set_int( properties, "force_full_luma", mlt_properties_get_int( frame_properties, "force_full_luma" ) );
286 mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( frame_properties, "top_field_first" ) );
287 data = mlt_frame_get_alpha_mask( frame );
288 mlt_properties_get_data( frame_properties, "alpha", &size );
289 mlt_frame_set_alpha( self, data, size, NULL );
290 self->convert_image = frame->convert_image;
291 self->convert_audio = frame->convert_audio;
295 static int producer_get_audio( mlt_frame self, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
297 mlt_properties properties = MLT_FRAME_PROPERTIES( self );
298 mlt_frame frame = mlt_frame_pop_audio( self );
299 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
300 mlt_frame_set_audio( self, *buffer, *format, mlt_audio_format_size( *format, *samples, *channels ), NULL );
301 mlt_properties_set_int( properties, "audio_frequency", *frequency );
302 mlt_properties_set_int( properties, "audio_channels", *channels );
303 mlt_properties_set_int( properties, "audio_samples", *samples );
307 static void destroy_data_queue( void *arg )
311 // Assign the correct type
312 mlt_deque queue = arg;
314 // Iterate through each item and destroy them
315 while ( mlt_deque_peek_front( queue ) != NULL )
316 mlt_properties_close( mlt_deque_pop_back( queue ) );
319 mlt_deque_close( queue );
323 /** Get the next frame.
325 * \private \memberof mlt_tractor_s
326 * \param parent the producer interface to the tractor
327 * \param[out] frame a frame by reference
328 * \param track the 0-based track index
329 * \return true on error
332 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
334 mlt_tractor self = parent->child;
336 // We only respond to the first track requests
337 if ( track == 0 && self->producer != NULL )
341 mlt_frame temp = NULL;
345 // Get the properties of the parent producer
346 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
348 // Try to obtain the multitrack associated to the tractor
349 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
351 // Or a specific producer
352 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
354 // The output frame will hold the 'global' data feeds (ie: those which are targetted for the final frame)
355 mlt_deque data_queue = mlt_deque_init( );
357 // Determine whether this tractor feeds to the consumer or stops here
358 int global_feed = mlt_properties_get_int( properties, "global_feed" );
360 // If we don't have one, we're in trouble...
361 if ( multitrack != NULL )
363 // Used to garbage collect all frames
366 // Get the id of the tractor
367 char *id = mlt_properties_get( properties, "_unique_id" );
369 // Will be used to store the frame properties object
370 mlt_properties frame_properties = NULL;
372 // We'll store audio and video frames to use here
373 mlt_frame audio = NULL;
374 mlt_frame video = NULL;
375 mlt_frame first_video = NULL;
377 // Temporary properties
378 mlt_properties temp_properties = NULL;
380 // Get the multitrack's producer
381 mlt_producer target = MLT_MULTITRACK_PRODUCER( multitrack );
382 mlt_producer_seek( target, mlt_producer_frame( parent ) );
383 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
385 // We will create one frame and attach everything to it
386 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
388 // Get the properties of the frame
389 frame_properties = MLT_FRAME_PROPERTIES( *frame );
391 // Loop through each of the tracks we're harvesting
392 for ( i = 0; !done; i ++ )
394 // Get a frame from the producer
395 mlt_service_get_frame( self->producer, &temp, i );
397 // Get the temporary properties
398 temp_properties = MLT_FRAME_PROPERTIES( temp );
400 // Pass all unique meta properties from the producer's frame to the new frame
401 mlt_properties_lock( temp_properties );
402 int props_count = mlt_properties_count( temp_properties );
404 for ( j = 0; j < props_count; j ++ )
406 char *name = mlt_properties_get_name( temp_properties, j );
407 if ( !strncmp( name, "meta.", 5 ) && !mlt_properties_get( frame_properties, name ) )
408 mlt_properties_set( frame_properties, name, mlt_properties_get_value( temp_properties, j ) );
410 mlt_properties_unlock( temp_properties );
412 // Copy the format conversion virtual functions
413 if ( ! (*frame)->convert_image && temp->convert_image )
414 (*frame)->convert_image = temp->convert_image;
415 if ( ! (*frame)->convert_audio && temp->convert_audio )
416 (*frame)->convert_audio = temp->convert_audio;
418 // Check for last track
419 done = mlt_properties_get_int( temp_properties, "last_track" );
421 // Handle fx only tracks
422 if ( mlt_properties_get_int( temp_properties, "fx_cut" ) )
424 int hide = ( video == NULL ? 1 : 0 ) | ( audio == NULL ? 2 : 0 );
425 mlt_properties_set_int( temp_properties, "hide", hide );
428 // We store all frames with a destructor on the output frame
429 sprintf( label, "_%s_%d", id, count ++ );
430 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
432 // We want to append all 'final' feeds to the global queue
433 if ( !done && mlt_properties_get_data( temp_properties, "data_queue", NULL ) != NULL )
435 // Move the contents of this queue on to the output frames data queue
436 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "data_queue", NULL );
437 mlt_deque temp = mlt_deque_init( );
438 while ( global_feed && mlt_deque_count( sub_queue ) )
440 mlt_properties p = mlt_deque_pop_back( sub_queue );
441 if ( mlt_properties_get_int( p, "final" ) )
442 mlt_deque_push_back( data_queue, p );
444 mlt_deque_push_back( temp, p );
446 while( mlt_deque_count( temp ) )
447 mlt_deque_push_front( sub_queue, mlt_deque_pop_back( temp ) );
448 mlt_deque_close( temp );
451 // Now do the same with the global queue but without the conditional behaviour
452 if ( mlt_properties_get_data( temp_properties, "global_queue", NULL ) != NULL )
454 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "global_queue", NULL );
455 while ( mlt_deque_count( sub_queue ) )
457 mlt_properties p = mlt_deque_pop_back( sub_queue );
458 mlt_deque_push_back( data_queue, p );
462 // Pick up first video and audio frames
463 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 2 ) )
465 // Order of frame creation is starting to get problematic
468 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), producer_get_audio );
469 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), audio );
473 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
477 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), producer_get_image );
478 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), video );
481 if ( first_video == NULL )
484 // Ensure that all frames know the aspect ratio of the background
485 mlt_properties_set_double( temp_properties, "output_ratio",
486 mlt_properties_get_double( MLT_FRAME_PROPERTIES( first_video ), "aspect_ratio" ) );
488 mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp ), "image_count", ++ image_count );
493 // Now stack callbacks
496 mlt_frame_push_audio( *frame, audio );
497 mlt_frame_push_audio( *frame, producer_get_audio );
502 mlt_properties video_properties = MLT_FRAME_PROPERTIES( first_video );
503 mlt_frame_push_service( *frame, video );
504 mlt_frame_push_service( *frame, producer_get_image );
506 mlt_properties_set_data( frame_properties, "data_queue", data_queue, 0, NULL, NULL );
507 mlt_properties_set_data( video_properties, "global_queue", data_queue, 0, destroy_data_queue, NULL );
508 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( video_properties, "width" ) );
509 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( video_properties, "height" ) );
510 mlt_properties_set_int( frame_properties, "real_width", mlt_properties_get_int( video_properties, "real_width" ) );
511 mlt_properties_set_int( frame_properties, "real_height", mlt_properties_get_int( video_properties, "real_height" ) );
512 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( video_properties, "progressive" ) );
513 mlt_properties_set_double( frame_properties, "aspect_ratio", mlt_properties_get_double( video_properties, "aspect_ratio" ) );
514 mlt_properties_set_int( frame_properties, "image_count", image_count );
518 destroy_data_queue( data_queue );
521 mlt_frame_set_position( *frame, mlt_producer_frame( parent ) );
522 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", audio == NULL );
523 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", video == NULL );
525 else if ( producer != NULL )
527 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
528 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
529 mlt_service_get_frame( self->producer, frame, track );
533 mlt_log( MLT_PRODUCER_SERVICE( parent ), MLT_LOG_ERROR, "tractor without a multitrack!!\n" );
534 mlt_service_get_frame( self->producer, frame, track );
537 // Prepare the next frame
538 mlt_producer_prepare_next( parent );
540 // Indicate our found status
545 // Generate a test card
546 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
551 /** Close the tractor and free its resources.
553 * \public \memberof mlt_tractor_s
554 * \param self a tractor
557 void mlt_tractor_close( mlt_tractor self )
559 if ( self != NULL && mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( self ) ) <= 0 )
561 self->parent.close = NULL;
562 mlt_producer_close( &self->parent );