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( 1, sizeof( struct mlt_tractor_s ) );
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( 1, sizeof( struct mlt_tractor_s ) );
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_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "consumer_deinterlace" ) );
271 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
272 mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "consumer_tff" ) );
273 mlt_frame_get_image( frame, buffer, format, width, height, writable );
274 mlt_frame_set_image( self, *buffer, 0, NULL );
275 mlt_properties_set_int( properties, "width", *width );
276 mlt_properties_set_int( properties, "height", *height );
277 mlt_properties_set_int( properties, "format", *format );
278 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( frame ) );
279 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
280 mlt_properties_set_int( properties, "distort", mlt_properties_get_int( frame_properties, "distort" ) );
281 mlt_properties_set_int( properties, "colorspace", mlt_properties_get_int( frame_properties, "colorspace" ) );
282 mlt_properties_set_int( properties, "force_full_luma", mlt_properties_get_int( frame_properties, "force_full_luma" ) );
283 mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( frame_properties, "top_field_first" ) );
284 data = mlt_frame_get_alpha_mask( frame );
285 mlt_properties_get_data( frame_properties, "alpha", &size );
286 mlt_frame_set_alpha( self, data, size, NULL );
287 self->convert_image = frame->convert_image;
288 self->convert_audio = frame->convert_audio;
292 static int producer_get_audio( mlt_frame self, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
294 mlt_properties properties = MLT_FRAME_PROPERTIES( self );
295 mlt_frame frame = mlt_frame_pop_audio( self );
296 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
297 mlt_frame_set_audio( self, *buffer, *format, mlt_audio_format_size( *format, *samples, *channels ), NULL );
298 mlt_properties_set_int( properties, "audio_frequency", *frequency );
299 mlt_properties_set_int( properties, "audio_channels", *channels );
300 mlt_properties_set_int( properties, "audio_samples", *samples );
304 static void destroy_data_queue( void *arg )
308 // Assign the correct type
309 mlt_deque queue = arg;
311 // Iterate through each item and destroy them
312 while ( mlt_deque_peek_front( queue ) != NULL )
313 mlt_properties_close( mlt_deque_pop_back( queue ) );
316 mlt_deque_close( queue );
320 /** Get the next frame.
322 * \private \memberof mlt_tractor_s
323 * \param parent the producer interface to the tractor
324 * \param[out] frame a frame by reference
325 * \param track the 0-based track index
326 * \return true on error
329 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
331 mlt_tractor self = parent->child;
333 // We only respond to the first track requests
334 if ( track == 0 && self->producer != NULL )
338 mlt_frame temp = NULL;
342 // Get the properties of the parent producer
343 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
345 // Try to obtain the multitrack associated to the tractor
346 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
348 // Or a specific producer
349 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
351 // Determine whether this tractor feeds to the consumer or stops here
352 int global_feed = mlt_properties_get_int( properties, "global_feed" );
354 // If we don't have one, we're in trouble...
355 if ( multitrack != NULL )
357 // The output frame will hold the 'global' data feeds (ie: those which are targetted for the final frame)
358 mlt_deque data_queue = mlt_deque_init( );
360 // Used to garbage collect all frames
363 // Get the id of the tractor
364 char *id = mlt_properties_get( properties, "_unique_id" );
366 // Will be used to store the frame properties object
367 mlt_properties frame_properties = NULL;
369 // We'll store audio and video frames to use here
370 mlt_frame audio = NULL;
371 mlt_frame video = NULL;
372 mlt_frame first_video = NULL;
374 // Temporary properties
375 mlt_properties temp_properties = NULL;
377 // Get the multitrack's producer
378 mlt_producer target = MLT_MULTITRACK_PRODUCER( multitrack );
379 mlt_producer_seek( target, mlt_producer_frame( parent ) );
380 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
382 // We will create one frame and attach everything to it
383 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
385 // Get the properties of the frame
386 frame_properties = MLT_FRAME_PROPERTIES( *frame );
388 // Loop through each of the tracks we're harvesting
389 for ( i = 0; !done; i ++ )
391 // Get a frame from the producer
392 mlt_service_get_frame( self->producer, &temp, i );
394 // Get the temporary properties
395 temp_properties = MLT_FRAME_PROPERTIES( temp );
397 // Pass all unique meta properties from the producer's frame to the new frame
398 mlt_properties_lock( temp_properties );
399 int props_count = mlt_properties_count( temp_properties );
401 for ( j = 0; j < props_count; j ++ )
403 char *name = mlt_properties_get_name( temp_properties, j );
404 if ( !strncmp( name, "meta.", 5 ) && !mlt_properties_get( frame_properties, name ) )
405 mlt_properties_set( frame_properties, name, mlt_properties_get_value( temp_properties, j ) );
407 mlt_properties_unlock( temp_properties );
409 // Copy the format conversion virtual functions
410 if ( ! (*frame)->convert_image && temp->convert_image )
411 (*frame)->convert_image = temp->convert_image;
412 if ( ! (*frame)->convert_audio && temp->convert_audio )
413 (*frame)->convert_audio = temp->convert_audio;
415 // Check for last track
416 done = mlt_properties_get_int( temp_properties, "last_track" );
418 // Handle fx only tracks
419 if ( mlt_properties_get_int( temp_properties, "fx_cut" ) )
421 int hide = ( video == NULL ? 1 : 0 ) | ( audio == NULL ? 2 : 0 );
422 mlt_properties_set_int( temp_properties, "hide", hide );
425 // We store all frames with a destructor on the output frame
426 sprintf( label, "_%s_%d", id, count ++ );
427 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
429 // We want to append all 'final' feeds to the global queue
430 if ( !done && mlt_properties_get_data( temp_properties, "data_queue", NULL ) != NULL )
432 // Move the contents of this queue on to the output frames data queue
433 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "data_queue", NULL );
434 mlt_deque temp = mlt_deque_init( );
435 while ( global_feed && mlt_deque_count( sub_queue ) )
437 mlt_properties p = mlt_deque_pop_back( sub_queue );
438 if ( mlt_properties_get_int( p, "final" ) )
439 mlt_deque_push_back( data_queue, p );
441 mlt_deque_push_back( temp, p );
443 while( mlt_deque_count( temp ) )
444 mlt_deque_push_front( sub_queue, mlt_deque_pop_back( temp ) );
445 mlt_deque_close( temp );
448 // Now do the same with the global queue but without the conditional behaviour
449 if ( mlt_properties_get_data( temp_properties, "global_queue", NULL ) != NULL )
451 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "global_queue", NULL );
452 while ( mlt_deque_count( sub_queue ) )
454 mlt_properties p = mlt_deque_pop_back( sub_queue );
455 mlt_deque_push_back( data_queue, p );
459 // Pick up first video and audio frames
460 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 2 ) )
462 // Order of frame creation is starting to get problematic
465 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), producer_get_audio );
466 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), audio );
470 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
474 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), producer_get_image );
475 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), video );
478 if ( first_video == NULL )
481 mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp ), "image_count", ++ image_count );
486 // Now stack callbacks
489 mlt_frame_push_audio( *frame, audio );
490 mlt_frame_push_audio( *frame, producer_get_audio );
495 mlt_properties video_properties = MLT_FRAME_PROPERTIES( first_video );
496 mlt_frame_push_service( *frame, video );
497 mlt_frame_push_service( *frame, producer_get_image );
499 mlt_properties_set_data( frame_properties, "data_queue", data_queue, 0, NULL, NULL );
500 mlt_properties_set_data( video_properties, "global_queue", data_queue, 0, destroy_data_queue, NULL );
501 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( video_properties, "width" ) );
502 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( video_properties, "height" ) );
503 mlt_properties_pass_list( frame_properties, video_properties, "meta.media.width, meta.media.height" );
504 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( video_properties, "progressive" ) );
505 mlt_properties_set_double( frame_properties, "aspect_ratio", mlt_properties_get_double( video_properties, "aspect_ratio" ) );
506 mlt_properties_set_int( frame_properties, "image_count", image_count );
510 destroy_data_queue( data_queue );
513 mlt_frame_set_position( *frame, mlt_producer_frame( parent ) );
514 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", audio == NULL );
515 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", video == NULL );
517 else if ( producer != NULL )
519 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
520 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
521 mlt_service_get_frame( self->producer, frame, track );
525 mlt_log( MLT_PRODUCER_SERVICE( parent ), MLT_LOG_ERROR, "tractor without a multitrack!!\n" );
526 mlt_service_get_frame( self->producer, frame, track );
529 // Prepare the next frame
530 mlt_producer_prepare_next( parent );
532 // Indicate our found status
537 // Generate a test card
538 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
543 /** Close the tractor and free its resources.
545 * \public \memberof mlt_tractor_s
546 * \param self a tractor
549 void mlt_tractor_close( mlt_tractor self )
551 if ( self != NULL && mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( self ) ) <= 0 )
553 self->parent.close = NULL;
554 mlt_producer_close( &self->parent );