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
this );
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
this = calloc( sizeof( struct mlt_tractor_s
), 1 );
55 mlt_producer producer
= &this->parent
;
56 if ( mlt_producer_init( producer
, this ) == 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
= this;
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
this = calloc( sizeof( struct mlt_tractor_s
), 1 );
94 mlt_producer producer
= &this->parent
;
95 if ( mlt_producer_init( producer
, this ) == 0 )
97 mlt_multitrack multitrack
= mlt_multitrack_init( );
98 mlt_field field
= mlt_field_new( multitrack
, this );
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
), this, "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
= this;
125 /** Get the service object associated to the tractor.
127 * \public \memberof mlt_tractor_s
128 * \param this a tractor
129 * \return the parent service object
130 * \see MLT_TRACTOR_SERVICE
133 mlt_service
mlt_tractor_service( mlt_tractor
this )
135 return MLT_PRODUCER_SERVICE( &this->parent
);
138 /** Get the producer object associated to the tractor.
140 * \public \memberof mlt_tractor_s
141 * \param this a tractor
142 * \return the parent producer object
143 * \see MLT_TRACTOR_PRODUCER
146 mlt_producer
mlt_tractor_producer( mlt_tractor
this )
148 return this != NULL ?
&this->parent
: NULL
;
151 /** Get the properties object associated to the tractor.
153 * \public \memberof mlt_tractor_s
154 * \param this a tractor
155 * \return the tractor's property list
156 * \see MLT_TRACTOR_PROPERTIES
159 mlt_properties
mlt_tractor_properties( mlt_tractor
this )
161 return MLT_PRODUCER_PROPERTIES( &this->parent
);
164 /** Get the field this tractor is harvesting.
166 * \public \memberof mlt_tractor_s
167 * \param this a tractor
168 * \return a field or NULL if there is no field for this tractor
171 mlt_field
mlt_tractor_field( mlt_tractor
this )
173 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "field", NULL
);
176 /** Get the multitrack this tractor is pulling.
178 * \public \memberof mlt_tractor_s
179 * \param this a tractor
180 * \return a multitrack or NULL if there is none
183 mlt_multitrack
mlt_tractor_multitrack( mlt_tractor
this )
185 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "multitrack", NULL
);
188 /** Ensure the tractors in/out points match the multitrack.
190 * \public \memberof mlt_tractor_s
191 * \param this a tractor
194 void mlt_tractor_refresh( mlt_tractor
this )
196 mlt_multitrack multitrack
= mlt_tractor_multitrack( this );
197 mlt_properties properties
= MLT_MULTITRACK_PROPERTIES( multitrack
);
198 mlt_properties self
= MLT_TRACTOR_PROPERTIES( this );
199 mlt_events_block( properties
, self
);
200 mlt_events_block( self
, self
);
201 mlt_multitrack_refresh( multitrack
);
202 mlt_properties_set_position( self
, "in", 0 );
203 mlt_properties_set_position( self
, "out", mlt_properties_get_position( properties
, "out" ) );
204 mlt_events_unblock( self
, self
);
205 mlt_events_unblock( properties
, self
);
206 mlt_properties_set_position( self
, "length", mlt_properties_get_position( properties
, "length" ) );
209 static void mlt_tractor_listener( mlt_multitrack tracks
, mlt_tractor
this )
211 mlt_tractor_refresh( this );
214 /** Connect the tractor.
216 * \public \memberof mlt_tractor_s
217 * \param this a tractor
218 * \param producer a producer
219 * \return true on error
222 int mlt_tractor_connect( mlt_tractor
this, mlt_service producer
)
224 int ret
= mlt_service_connect_producer( MLT_TRACTOR_SERVICE( this ), producer
, 0 );
226 // This is the producer we're going to connect to
228 this->producer
= producer
;
233 /** Set the producer for a specific track.
235 * \public \memberof mlt_tractor_s
236 * \param this 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
this, mlt_producer producer
, int index
)
244 return mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer
, index
);
247 /** Get the producer for a specific track.
249 * \public \memberof mlt_tractor_s
250 * \param this 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
this, int index
)
257 return mlt_multitrack_track( mlt_tractor_multitrack( this ), index
);
260 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
262 uint8_t *data
= NULL
;
263 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
264 mlt_frame frame
= mlt_frame_pop_service( this );
265 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
266 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale.interp" ) );
267 mlt_properties_set_int( frame_properties
, "resize_alpha", mlt_properties_get_int( properties
, "resize_alpha" ) );
268 mlt_properties_set_int( frame_properties
, "distort", mlt_properties_get_int( properties
, "distort" ) );
269 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) );
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
, "normalised_width", mlt_properties_get_int( properties
, "normalised_width" ) );
273 mlt_properties_set_int( frame_properties
, "normalised_height", mlt_properties_get_int( properties
, "normalised_height" ) );
274 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
275 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, NULL
);
276 mlt_properties_set_int( properties
, "width", *width
);
277 mlt_properties_set_int( properties
, "height", *height
);
278 mlt_properties_set_int( properties
, "format", *format
);
279 mlt_properties_set_double( properties
, "aspect_ratio", mlt_frame_get_aspect_ratio( frame
) );
280 mlt_properties_set_int( properties
, "progressive", mlt_properties_get_int( frame_properties
, "progressive" ) );
281 mlt_properties_set_int( properties
, "distort", mlt_properties_get_int( frame_properties
, "distort" ) );
282 data
= mlt_frame_get_alpha_mask( frame
);
283 mlt_properties_set_data( properties
, "alpha", data
, 0, NULL
, NULL
);
287 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
289 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
290 mlt_frame frame
= mlt_frame_pop_audio( this );
291 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
292 mlt_properties_set_data( properties
, "audio", *buffer
, 0, NULL
, NULL
);
293 mlt_properties_set_int( properties
, "frequency", *frequency
);
294 mlt_properties_set_int( properties
, "channels", *channels
);
298 static void destroy_data_queue( void *arg
)
302 // Assign the correct type
303 mlt_deque queue
= arg
;
305 // Iterate through each item and destroy them
306 while ( mlt_deque_peek_front( queue
) != NULL
)
307 mlt_properties_close( mlt_deque_pop_back( queue
) );
310 mlt_deque_close( queue
);
314 /** Get the next frame.
316 * \private \memberof mlt_tractor_s
317 * \param parent the producer interface to the tractor
318 * \param[out] frame a frame by reference
319 * \param track the 0-based track index
320 * \return true on error
323 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int track
)
325 mlt_tractor
this = parent
->child
;
327 // We only respond to the first track requests
328 if ( track
== 0 && this->producer
!= NULL
)
332 mlt_frame temp
= NULL
;
336 // Get the properties of the parent producer
337 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( parent
);
339 // Try to obtain the multitrack associated to the tractor
340 mlt_multitrack multitrack
= mlt_properties_get_data( properties
, "multitrack", NULL
);
342 // Or a specific producer
343 mlt_producer producer
= mlt_properties_get_data( properties
, "producer", NULL
);
345 // The output frame will hold the 'global' data feeds (ie: those which are targetted for the final frame)
346 mlt_deque data_queue
= mlt_deque_init( );
348 // Determine whether this tractor feeds to the consumer or stops here
349 int global_feed
= mlt_properties_get_int( properties
, "global_feed" );
351 // If we don't have one, we're in trouble...
352 if ( multitrack
!= NULL
)
354 // Used to garbage collect all frames
357 // Get the id of the tractor
358 char *id
= mlt_properties_get( properties
, "_unique_id" );
360 // Will be used to store the frame properties object
361 mlt_properties frame_properties
= NULL
;
363 // We'll store audio and video frames to use here
364 mlt_frame audio
= NULL
;
365 mlt_frame video
= NULL
;
366 mlt_frame first_video
= NULL
;
368 // Temporary properties
369 mlt_properties temp_properties
= NULL
;
371 // Get the multitrack's producer
372 mlt_producer target
= MLT_MULTITRACK_PRODUCER( multitrack
);
373 mlt_producer_seek( target
, mlt_producer_frame( parent
) );
374 mlt_producer_set_speed( target
, mlt_producer_get_speed( parent
) );
376 // We will create one frame and attach everything to it
377 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( parent
) );
379 // Get the properties of the frame
380 frame_properties
= MLT_FRAME_PROPERTIES( *frame
);
382 // Loop through each of the tracks we're harvesting
383 for ( i
= 0; !done
; i
++ )
385 // Get a frame from the producer
386 mlt_service_get_frame( this->producer
, &temp
, i
);
388 // Get the temporary properties
389 temp_properties
= MLT_FRAME_PROPERTIES( temp
);
391 // Check for last track
392 done
= mlt_properties_get_int( temp_properties
, "last_track" );
394 // Handle fx only tracks
395 if ( mlt_properties_get_int( temp_properties
, "fx_cut" ) )
397 int hide
= ( video
== NULL ?
1 : 0 ) | ( audio
== NULL ?
2 : 0 );
398 mlt_properties_set_int( temp_properties
, "hide", hide
);
401 // We store all frames with a destructor on the output frame
402 sprintf( label
, "_%s_%d", id
, count
++ );
403 mlt_properties_set_data( frame_properties
, label
, temp
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
405 // We want to append all 'final' feeds to the global queue
406 if ( !done
&& mlt_properties_get_data( temp_properties
, "data_queue", NULL
) != NULL
)
408 // Move the contents of this queue on to the output frames data queue
409 mlt_deque sub_queue
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp
), "data_queue", NULL
);
410 mlt_deque temp
= mlt_deque_init( );
411 while ( global_feed
&& mlt_deque_count( sub_queue
) )
413 mlt_properties p
= mlt_deque_pop_back( sub_queue
);
414 if ( mlt_properties_get_int( p
, "final" ) )
415 mlt_deque_push_back( data_queue
, p
);
417 mlt_deque_push_back( temp
, p
);
419 while( mlt_deque_count( temp
) )
420 mlt_deque_push_front( sub_queue
, mlt_deque_pop_back( temp
) );
421 mlt_deque_close( temp
);
424 // Now do the same with the global queue but without the conditional behaviour
425 if ( mlt_properties_get_data( temp_properties
, "global_queue", NULL
) != NULL
)
427 mlt_deque sub_queue
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp
), "global_queue", NULL
);
428 while ( mlt_deque_count( sub_queue
) )
430 mlt_properties p
= mlt_deque_pop_back( sub_queue
);
431 mlt_deque_push_back( data_queue
, p
);
435 // Pick up first video and audio frames
436 if ( !done
&& !mlt_frame_is_test_audio( temp
) && !( mlt_properties_get_int( temp_properties
, "hide" ) & 2 ) )
438 // Order of frame creation is starting to get problematic
441 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp
), producer_get_audio
);
442 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp
), audio
);
446 if ( !done
&& !mlt_frame_is_test_card( temp
) && !( mlt_properties_get_int( temp_properties
, "hide" ) & 1 ) )
450 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp
), producer_get_image
);
451 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp
), video
);
454 if ( first_video
== NULL
)
457 // Ensure that all frames know the aspect ratio of the background
458 mlt_properties_set_double( temp_properties
, "output_ratio",
459 mlt_properties_get_double( MLT_FRAME_PROPERTIES( first_video
), "aspect_ratio" ) );
461 mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp
), "image_count", ++ image_count
);
466 // Now stack callbacks
469 mlt_frame_push_audio( *frame
, audio
);
470 mlt_frame_push_audio( *frame
, producer_get_audio
);
475 mlt_properties video_properties
= MLT_FRAME_PROPERTIES( first_video
);
476 mlt_frame_push_service( *frame
, video
);
477 mlt_frame_push_service( *frame
, producer_get_image
);
479 mlt_properties_set_data( frame_properties
, "data_queue", data_queue
, 0, NULL
, NULL
);
480 mlt_properties_set_data( video_properties
, "global_queue", data_queue
, 0, destroy_data_queue
, NULL
);
481 mlt_properties_set_int( frame_properties
, "width", mlt_properties_get_int( video_properties
, "width" ) );
482 mlt_properties_set_int( frame_properties
, "height", mlt_properties_get_int( video_properties
, "height" ) );
483 mlt_properties_set_int( frame_properties
, "real_width", mlt_properties_get_int( video_properties
, "real_width" ) );
484 mlt_properties_set_int( frame_properties
, "real_height", mlt_properties_get_int( video_properties
, "real_height" ) );
485 mlt_properties_set_int( frame_properties
, "progressive", mlt_properties_get_int( video_properties
, "progressive" ) );
486 mlt_properties_set_double( frame_properties
, "aspect_ratio", mlt_properties_get_double( video_properties
, "aspect_ratio" ) );
487 mlt_properties_set_int( frame_properties
, "image_count", image_count
);
491 destroy_data_queue( data_queue
);
494 mlt_frame_set_position( *frame
, mlt_producer_frame( parent
) );
495 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame
), "test_audio", audio
== NULL
);
496 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame
), "test_image", video
== NULL
);
497 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame
), "consumer_lock_service", this, 0, NULL
, NULL
);
499 else if ( producer
!= NULL
)
501 mlt_producer_seek( producer
, mlt_producer_frame( parent
) );
502 mlt_producer_set_speed( producer
, mlt_producer_get_speed( parent
) );
503 mlt_service_get_frame( this->producer
, frame
, track
);
507 mlt_log( MLT_PRODUCER_SERVICE( parent
), MLT_LOG_ERROR
, "tractor without a multitrack!!\n" );
508 mlt_service_get_frame( this->producer
, frame
, track
);
511 // Prepare the next frame
512 mlt_producer_prepare_next( parent
);
514 // Indicate our found status
519 // Generate a test card
520 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( parent
) );
525 /** Close the tractor and free its resources.
527 * \public \memberof mlt_tractor_s
528 * \param this a tractor
531 void mlt_tractor_close( mlt_tractor
this )
533 if ( this != NULL
&& mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( this ) ) <= 0 )
535 this->parent
.close
= NULL
;
536 mlt_producer_close( &this->parent
);