3 * \brief tractor service class
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "mlt_tractor.h"
24 #include "mlt_frame.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
34 /* Forward references to static methods.
37 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int track
);
38 static void mlt_tractor_listener( mlt_multitrack tracks
, mlt_tractor
this );
40 /** Construct a tractor without a field or multitrack.
42 * Sets the resource property to "<tractor>", the mlt_type to "mlt_producer",
43 * and mlt_service to "tractor".
45 * \public \memberof mlt_tractor_s
46 * \return the new tractor
49 mlt_tractor
mlt_tractor_init( )
51 mlt_tractor
this = calloc( sizeof( struct mlt_tractor_s
), 1 );
54 mlt_producer producer
= &this->parent
;
55 if ( mlt_producer_init( producer
, this ) == 0 )
57 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( producer
);
59 mlt_properties_set( properties
, "resource", "<tractor>" );
60 mlt_properties_set( properties
, "mlt_type", "mlt_producer" );
61 mlt_properties_set( properties
, "mlt_service", "tractor" );
62 mlt_properties_set_int( properties
, "in", 0 );
63 mlt_properties_set_int( properties
, "out", -1 );
64 mlt_properties_set_int( properties
, "length", 0 );
66 producer
->get_frame
= producer_get_frame
;
67 producer
->close
= ( mlt_destructor
)mlt_tractor_close
;
68 producer
->close_object
= this;
79 /** Construct a tractor as well as a field and multitrack.
81 * Sets the resource property to "<tractor>", the mlt_type to "mlt_producer",
82 * and mlt_service to "tractor".
84 * \public \memberof mlt_tractor_s
85 * \return the new tractor
88 mlt_tractor
mlt_tractor_new( )
90 mlt_tractor
this = calloc( sizeof( struct mlt_tractor_s
), 1 );
93 mlt_producer producer
= &this->parent
;
94 if ( mlt_producer_init( producer
, this ) == 0 )
96 mlt_multitrack multitrack
= mlt_multitrack_init( );
97 mlt_field field
= mlt_field_new( multitrack
, this );
98 mlt_properties props
= MLT_PRODUCER_PROPERTIES( producer
);
100 mlt_properties_set( props
, "resource", "<tractor>" );
101 mlt_properties_set( props
, "mlt_type", "mlt_producer" );
102 mlt_properties_set( props
, "mlt_service", "tractor" );
103 mlt_properties_set_position( props
, "in", 0 );
104 mlt_properties_set_position( props
, "out", 0 );
105 mlt_properties_set_position( props
, "length", 0 );
106 mlt_properties_set_data( props
, "multitrack", multitrack
, 0, ( mlt_destructor
)mlt_multitrack_close
, NULL
);
107 mlt_properties_set_data( props
, "field", field
, 0, ( mlt_destructor
)mlt_field_close
, NULL
);
109 mlt_events_listen( MLT_MULTITRACK_PROPERTIES( multitrack
), this, "producer-changed", ( mlt_listener
)mlt_tractor_listener
);
111 producer
->get_frame
= producer_get_frame
;
112 producer
->close
= ( mlt_destructor
)mlt_tractor_close
;
113 producer
->close_object
= this;
124 /** Get the service object associated to the tractor.
126 * \public \memberof mlt_tractor_s
127 * \param this a tractor
128 * \return the parent service object
129 * \see MLT_TRACTOR_SERVICE
132 mlt_service
mlt_tractor_service( mlt_tractor
this )
134 return MLT_PRODUCER_SERVICE( &this->parent
);
137 /** Get the producer object associated to the tractor.
139 * \public \memberof mlt_tractor_s
140 * \param this a tractor
141 * \return the parent producer object
142 * \see MLT_TRACTOR_PRODUCER
145 mlt_producer
mlt_tractor_producer( mlt_tractor
this )
147 return this != NULL ?
&this->parent
: NULL
;
150 /** Get the properties object associated to the tractor.
152 * \public \memberof mlt_tractor_s
153 * \param this a tractor
154 * \return the tractor's property list
155 * \see MLT_TRACTOR_PROPERTIES
158 mlt_properties
mlt_tractor_properties( mlt_tractor
this )
160 return MLT_PRODUCER_PROPERTIES( &this->parent
);
163 /** Get the field this tractor is harvesting.
165 * \public \memberof mlt_tractor_s
166 * \param this a tractor
167 * \return a field or NULL if there is no field for this tractor
170 mlt_field
mlt_tractor_field( mlt_tractor
this )
172 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "field", NULL
);
175 /** Get the multitrack this tractor is pulling.
177 * \public \memberof mlt_tractor_s
178 * \param this a tractor
179 * \return a multitrack or NULL if there is none
182 mlt_multitrack
mlt_tractor_multitrack( mlt_tractor
this )
184 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "multitrack", NULL
);
187 /** Ensure the tractors in/out points match the multitrack.
189 * \public \memberof mlt_tractor_s
190 * \param this a tractor
193 void mlt_tractor_refresh( mlt_tractor
this )
195 mlt_multitrack multitrack
= mlt_tractor_multitrack( this );
196 mlt_properties properties
= MLT_MULTITRACK_PROPERTIES( multitrack
);
197 mlt_properties self
= MLT_TRACTOR_PROPERTIES( this );
198 mlt_events_block( properties
, self
);
199 mlt_events_block( self
, self
);
200 mlt_multitrack_refresh( multitrack
);
201 mlt_properties_set_position( self
, "in", 0 );
202 mlt_properties_set_position( self
, "out", mlt_properties_get_position( properties
, "out" ) );
203 mlt_events_unblock( self
, self
);
204 mlt_events_unblock( properties
, self
);
205 mlt_properties_set_position( self
, "length", mlt_properties_get_position( properties
, "length" ) );
208 static void mlt_tractor_listener( mlt_multitrack tracks
, mlt_tractor
this )
210 mlt_tractor_refresh( this );
213 /** Connect the tractor.
215 * \public \memberof mlt_tractor_s
216 * \param this a tractor
217 * \param producer a producer
218 * \return true on error
221 int mlt_tractor_connect( mlt_tractor
this, mlt_service producer
)
223 int ret
= mlt_service_connect_producer( MLT_TRACTOR_SERVICE( this ), producer
, 0 );
225 // This is the producer we're going to connect to
227 this->producer
= producer
;
232 /** Set the producer for a specific track.
234 * \public \memberof mlt_tractor_s
235 * \param this a tractor
236 * \param producer a producer
237 * \param index the 0-based track index
238 * \return true on error
241 int mlt_tractor_set_track( mlt_tractor
this, mlt_producer producer
, int index
)
243 return mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer
, index
);
246 /** Get the producer for a specific track.
248 * \public \memberof mlt_tractor_s
249 * \param this a tractor
250 * \param index the 0-based track index
251 * \return the producer for track \p index
254 mlt_producer
mlt_tractor_get_track( mlt_tractor
this, int index
)
256 return mlt_multitrack_track( mlt_tractor_multitrack( this ), index
);
259 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
261 uint8_t *data
= NULL
;
262 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
263 mlt_frame frame
= mlt_frame_pop_service( this );
264 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
265 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale.interp" ) );
266 mlt_properties_set_int( frame_properties
, "resize_alpha", mlt_properties_get_int( properties
, "resize_alpha" ) );
267 mlt_properties_set_int( frame_properties
, "distort", mlt_properties_get_int( properties
, "distort" ) );
268 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) );
269 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "consumer_deinterlace" ) );
270 mlt_properties_set( frame_properties
, "deinterlace_method", mlt_properties_get( properties
, "deinterlace_method" ) );
271 mlt_properties_set_int( frame_properties
, "normalised_width", mlt_properties_get_int( properties
, "normalised_width" ) );
272 mlt_properties_set_int( frame_properties
, "normalised_height", mlt_properties_get_int( properties
, "normalised_height" ) );
273 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
274 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, 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 data
= mlt_frame_get_alpha_mask( frame
);
282 mlt_properties_set_data( properties
, "alpha", data
, 0, NULL
, NULL
);
286 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
288 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
289 mlt_frame frame
= mlt_frame_pop_audio( this );
290 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
291 mlt_properties_set_data( properties
, "audio", *buffer
, 0, NULL
, NULL
);
292 mlt_properties_set_int( properties
, "frequency", *frequency
);
293 mlt_properties_set_int( properties
, "channels", *channels
);
297 static void destroy_data_queue( void *arg
)
301 // Assign the correct type
302 mlt_deque queue
= arg
;
304 // Iterate through each item and destroy them
305 while ( mlt_deque_peek_front( queue
) != NULL
)
306 mlt_properties_close( mlt_deque_pop_back( queue
) );
309 mlt_deque_close( queue
);
313 /** Get the next frame.
315 * \private \memberof mlt_tractor_s
316 * \param parent the producer interface to the tractor
317 * \param[out] frame a frame by reference
318 * \param track the 0-based track index
319 * \return true on error
322 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int track
)
324 mlt_tractor
this = parent
->child
;
326 // We only respond to the first track requests
327 if ( track
== 0 && this->producer
!= NULL
)
331 mlt_frame temp
= NULL
;
335 // Get the properties of the parent producer
336 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( parent
);
338 // Try to obtain the multitrack associated to the tractor
339 mlt_multitrack multitrack
= mlt_properties_get_data( properties
, "multitrack", NULL
);
341 // Or a specific producer
342 mlt_producer producer
= mlt_properties_get_data( properties
, "producer", NULL
);
344 // The output frame will hold the 'global' data feeds (ie: those which are targetted for the final frame)
345 mlt_deque data_queue
= mlt_deque_init( );
347 // Determine whether this tractor feeds to the consumer or stops here
348 int global_feed
= mlt_properties_get_int( properties
, "global_feed" );
350 // If we don't have one, we're in trouble...
351 if ( multitrack
!= NULL
)
353 // Used to garbage collect all frames
356 // Get the id of the tractor
357 char *id
= mlt_properties_get( properties
, "_unique_id" );
359 // Will be used to store the frame properties object
360 mlt_properties frame_properties
= NULL
;
362 // We'll store audio and video frames to use here
363 mlt_frame audio
= NULL
;
364 mlt_frame video
= NULL
;
365 mlt_frame first_video
= NULL
;
367 // Temporary properties
368 mlt_properties temp_properties
= NULL
;
370 // Get the multitrack's producer
371 mlt_producer target
= MLT_MULTITRACK_PRODUCER( multitrack
);
372 mlt_producer_seek( target
, mlt_producer_frame( parent
) );
373 mlt_producer_set_speed( target
, mlt_producer_get_speed( parent
) );
375 // We will create one frame and attach everything to it
376 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( parent
) );
378 // Get the properties of the frame
379 frame_properties
= MLT_FRAME_PROPERTIES( *frame
);
381 // Loop through each of the tracks we're harvesting
382 for ( i
= 0; !done
; i
++ )
384 // Get a frame from the producer
385 mlt_service_get_frame( this->producer
, &temp
, i
);
387 // Get the temporary properties
388 temp_properties
= MLT_FRAME_PROPERTIES( temp
);
390 // Check for last track
391 done
= mlt_properties_get_int( temp_properties
, "last_track" );
393 // Handle fx only tracks
394 if ( mlt_properties_get_int( temp_properties
, "fx_cut" ) )
396 int hide
= ( video
== NULL ?
1 : 0 ) | ( audio
== NULL ?
2 : 0 );
397 mlt_properties_set_int( temp_properties
, "hide", hide
);
400 // We store all frames with a destructor on the output frame
401 sprintf( label
, "_%s_%d", id
, count
++ );
402 mlt_properties_set_data( frame_properties
, label
, temp
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
404 // We want to append all 'final' feeds to the global queue
405 if ( !done
&& mlt_properties_get_data( temp_properties
, "data_queue", NULL
) != NULL
)
407 // Move the contents of this queue on to the output frames data queue
408 mlt_deque sub_queue
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp
), "data_queue", NULL
);
409 mlt_deque temp
= mlt_deque_init( );
410 while ( global_feed
&& mlt_deque_count( sub_queue
) )
412 mlt_properties p
= mlt_deque_pop_back( sub_queue
);
413 if ( mlt_properties_get_int( p
, "final" ) )
414 mlt_deque_push_back( data_queue
, p
);
416 mlt_deque_push_back( temp
, p
);
418 while( mlt_deque_count( temp
) )
419 mlt_deque_push_front( sub_queue
, mlt_deque_pop_back( temp
) );
420 mlt_deque_close( temp
);
423 // Now do the same with the global queue but without the conditional behaviour
424 if ( mlt_properties_get_data( temp_properties
, "global_queue", NULL
) != NULL
)
426 mlt_deque sub_queue
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp
), "global_queue", NULL
);
427 while ( mlt_deque_count( sub_queue
) )
429 mlt_properties p
= mlt_deque_pop_back( sub_queue
);
430 mlt_deque_push_back( data_queue
, p
);
434 // Pick up first video and audio frames
435 if ( !done
&& !mlt_frame_is_test_audio( temp
) && !( mlt_properties_get_int( temp_properties
, "hide" ) & 2 ) )
437 // Order of frame creation is starting to get problematic
440 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp
), producer_get_audio
);
441 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp
), audio
);
445 if ( !done
&& !mlt_frame_is_test_card( temp
) && !( mlt_properties_get_int( temp_properties
, "hide" ) & 1 ) )
449 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp
), producer_get_image
);
450 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp
), video
);
453 if ( first_video
== NULL
)
456 // Ensure that all frames know the aspect ratio of the background
457 mlt_properties_set_double( temp_properties
, "output_ratio",
458 mlt_properties_get_double( MLT_FRAME_PROPERTIES( first_video
), "aspect_ratio" ) );
460 mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp
), "image_count", ++ image_count
);
465 // Now stack callbacks
468 mlt_frame_push_audio( *frame
, audio
);
469 mlt_frame_push_audio( *frame
, producer_get_audio
);
474 mlt_properties video_properties
= MLT_FRAME_PROPERTIES( first_video
);
475 mlt_frame_push_service( *frame
, video
);
476 mlt_frame_push_service( *frame
, producer_get_image
);
478 mlt_properties_set_data( frame_properties
, "data_queue", data_queue
, 0, NULL
, NULL
);
479 mlt_properties_set_data( video_properties
, "global_queue", data_queue
, 0, destroy_data_queue
, NULL
);
480 mlt_properties_set_int( frame_properties
, "width", mlt_properties_get_int( video_properties
, "width" ) );
481 mlt_properties_set_int( frame_properties
, "height", mlt_properties_get_int( video_properties
, "height" ) );
482 mlt_properties_set_int( frame_properties
, "real_width", mlt_properties_get_int( video_properties
, "real_width" ) );
483 mlt_properties_set_int( frame_properties
, "real_height", mlt_properties_get_int( video_properties
, "real_height" ) );
484 mlt_properties_set_int( frame_properties
, "progressive", mlt_properties_get_int( video_properties
, "progressive" ) );
485 mlt_properties_set_double( frame_properties
, "aspect_ratio", mlt_properties_get_double( video_properties
, "aspect_ratio" ) );
486 mlt_properties_set_int( frame_properties
, "image_count", image_count
);
490 destroy_data_queue( data_queue
);
493 mlt_frame_set_position( *frame
, mlt_producer_frame( parent
) );
494 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame
), "test_audio", audio
== NULL
);
495 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame
), "test_image", video
== NULL
);
496 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame
), "consumer_lock_service", this, 0, NULL
, NULL
);
498 else if ( producer
!= NULL
)
500 mlt_producer_seek( producer
, mlt_producer_frame( parent
) );
501 mlt_producer_set_speed( producer
, mlt_producer_get_speed( parent
) );
502 mlt_service_get_frame( this->producer
, frame
, track
);
506 mlt_log( MLT_PRODUCER_SERVICE( parent
), MLT_LOG_ERROR
, "tractor without a multitrack!!\n" );
507 mlt_service_get_frame( this->producer
, frame
, track
);
510 // Prepare the next frame
511 mlt_producer_prepare_next( parent
);
513 // Indicate our found status
518 // Generate a test card
519 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( parent
) );
524 /** Close the tractor and free its resources.
526 * \public \memberof mlt_tractor_s
527 * \param this a tractor
530 void mlt_tractor_close( mlt_tractor
this )
532 if ( this != NULL
&& mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( this ) ) <= 0 )
534 this->parent
.close
= NULL
;
535 mlt_producer_close( &this->parent
);