mlt_log.[hc], mlt_transition.c, mlt_tractor.c, mlt_repository.c, mlt_properties.c,
[melted] / src / framework / mlt_tractor.c
1 /**
2 * \file mlt_tractor.c
3 * \brief tractor service class
4 *
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "mlt_tractor.h"
24 #include "mlt_frame.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
27 #include "mlt_log.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 /* Forward references to static methods.
35 */
36
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 );
39
40 /** Construct a tractor without a field or multitrack.
41 *
42 * Sets the resource property to "<tractor>", the mlt_type to "mlt_producer",
43 * and mlt_service to "tractor".
44 *
45 * \public \memberof mlt_tractor_s
46 * \return the new tractor
47 */
48
49 mlt_tractor mlt_tractor_init( )
50 {
51 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
52 if ( this != NULL )
53 {
54 mlt_producer producer = &this->parent;
55 if ( mlt_producer_init( producer, this ) == 0 )
56 {
57 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
58
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 );
65
66 producer->get_frame = producer_get_frame;
67 producer->close = ( mlt_destructor )mlt_tractor_close;
68 producer->close_object = this;
69 }
70 else
71 {
72 free( this );
73 this = NULL;
74 }
75 }
76 return this;
77 }
78
79 /** Construct a tractor as well as a field and multitrack.
80 *
81 * Sets the resource property to "<tractor>", the mlt_type to "mlt_producer",
82 * and mlt_service to "tractor".
83 *
84 * \public \memberof mlt_tractor_s
85 * \return the new tractor
86 */
87
88 mlt_tractor mlt_tractor_new( )
89 {
90 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
91 if ( this != NULL )
92 {
93 mlt_producer producer = &this->parent;
94 if ( mlt_producer_init( producer, this ) == 0 )
95 {
96 mlt_multitrack multitrack = mlt_multitrack_init( );
97 mlt_field field = mlt_field_new( multitrack, this );
98 mlt_properties props = MLT_PRODUCER_PROPERTIES( producer );
99
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 );
108
109 mlt_events_listen( MLT_MULTITRACK_PROPERTIES( multitrack ), this, "producer-changed", ( mlt_listener )mlt_tractor_listener );
110
111 producer->get_frame = producer_get_frame;
112 producer->close = ( mlt_destructor )mlt_tractor_close;
113 producer->close_object = this;
114 }
115 else
116 {
117 free( this );
118 this = NULL;
119 }
120 }
121 return this;
122 }
123
124 /** Get the service object associated to the tractor.
125 *
126 * \public \memberof mlt_tractor_s
127 * \param this a tractor
128 * \return the parent service object
129 * \see MLT_TRACTOR_SERVICE
130 */
131
132 mlt_service mlt_tractor_service( mlt_tractor this )
133 {
134 return MLT_PRODUCER_SERVICE( &this->parent );
135 }
136
137 /** Get the producer object associated to the tractor.
138 *
139 * \public \memberof mlt_tractor_s
140 * \param this a tractor
141 * \return the parent producer object
142 * \see MLT_TRACTOR_PRODUCER
143 */
144
145 mlt_producer mlt_tractor_producer( mlt_tractor this )
146 {
147 return this != NULL ? &this->parent : NULL;
148 }
149
150 /** Get the properties object associated to the tractor.
151 *
152 * \public \memberof mlt_tractor_s
153 * \param this a tractor
154 * \return the tractor's property list
155 * \see MLT_TRACTOR_PROPERTIES
156 */
157
158 mlt_properties mlt_tractor_properties( mlt_tractor this )
159 {
160 return MLT_PRODUCER_PROPERTIES( &this->parent );
161 }
162
163 /** Get the field this tractor is harvesting.
164 *
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
168 */
169
170 mlt_field mlt_tractor_field( mlt_tractor this )
171 {
172 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "field", NULL );
173 }
174
175 /** Get the multitrack this tractor is pulling.
176 *
177 * \public \memberof mlt_tractor_s
178 * \param this a tractor
179 * \return a multitrack or NULL if there is none
180 */
181
182 mlt_multitrack mlt_tractor_multitrack( mlt_tractor this )
183 {
184 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "multitrack", NULL );
185 }
186
187 /** Ensure the tractors in/out points match the multitrack.
188 *
189 * \public \memberof mlt_tractor_s
190 * \param this a tractor
191 */
192
193 void mlt_tractor_refresh( mlt_tractor this )
194 {
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" ) );
206 }
207
208 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this )
209 {
210 mlt_tractor_refresh( this );
211 }
212
213 /** Connect the tractor.
214 *
215 * \public \memberof mlt_tractor_s
216 * \param this a tractor
217 * \param producer a producer
218 * \return true on error
219 */
220
221 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
222 {
223 int ret = mlt_service_connect_producer( MLT_TRACTOR_SERVICE( this ), producer, 0 );
224
225 // This is the producer we're going to connect to
226 if ( ret == 0 )
227 this->producer = producer;
228
229 return ret;
230 }
231
232 /** Set the producer for a specific track.
233 *
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
239 */
240
241 int mlt_tractor_set_track( mlt_tractor this, mlt_producer producer, int index )
242 {
243 return mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer, index );
244 }
245
246 /** Get the producer for a specific track.
247 *
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
252 */
253
254 mlt_producer mlt_tractor_get_track( mlt_tractor this, int index )
255 {
256 return mlt_multitrack_track( mlt_tractor_multitrack( this ), index );
257 }
258
259 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
260 {
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 );
283 return 0;
284 }
285
286 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
287 {
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 );
294 return 0;
295 }
296
297 static void destroy_data_queue( void *arg )
298 {
299 if ( arg != NULL )
300 {
301 // Assign the correct type
302 mlt_deque queue = arg;
303
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 ) );
307
308 // Close the deque
309 mlt_deque_close( queue );
310 }
311 }
312
313 /** Get the next frame.
314 *
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
320 */
321
322 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
323 {
324 mlt_tractor this = parent->child;
325
326 // We only respond to the first track requests
327 if ( track == 0 && this->producer != NULL )
328 {
329 int i = 0;
330 int done = 0;
331 mlt_frame temp = NULL;
332 int count = 0;
333 int image_count = 0;
334
335 // Get the properties of the parent producer
336 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
337
338 // Try to obtain the multitrack associated to the tractor
339 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
340
341 // Or a specific producer
342 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
343
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( );
346
347 // Determine whether this tractor feeds to the consumer or stops here
348 int global_feed = mlt_properties_get_int( properties, "global_feed" );
349
350 // If we don't have one, we're in trouble...
351 if ( multitrack != NULL )
352 {
353 // Used to garbage collect all frames
354 char label[ 30 ];
355
356 // Get the id of the tractor
357 char *id = mlt_properties_get( properties, "_unique_id" );
358
359 // Will be used to store the frame properties object
360 mlt_properties frame_properties = NULL;
361
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;
366
367 // Temporary properties
368 mlt_properties temp_properties = NULL;
369
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 ) );
374
375 // We will create one frame and attach everything to it
376 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
377
378 // Get the properties of the frame
379 frame_properties = MLT_FRAME_PROPERTIES( *frame );
380
381 // Loop through each of the tracks we're harvesting
382 for ( i = 0; !done; i ++ )
383 {
384 // Get a frame from the producer
385 mlt_service_get_frame( this->producer, &temp, i );
386
387 // Get the temporary properties
388 temp_properties = MLT_FRAME_PROPERTIES( temp );
389
390 // Check for last track
391 done = mlt_properties_get_int( temp_properties, "last_track" );
392
393 // Handle fx only tracks
394 if ( mlt_properties_get_int( temp_properties, "fx_cut" ) )
395 {
396 int hide = ( video == NULL ? 1 : 0 ) | ( audio == NULL ? 2 : 0 );
397 mlt_properties_set_int( temp_properties, "hide", hide );
398 }
399
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 );
403
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 )
406 {
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 ) )
411 {
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 );
415 else
416 mlt_deque_push_back( temp, p );
417 }
418 while( mlt_deque_count( temp ) )
419 mlt_deque_push_front( sub_queue, mlt_deque_pop_back( temp ) );
420 mlt_deque_close( temp );
421 }
422
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 )
425 {
426 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "global_queue", NULL );
427 while ( mlt_deque_count( sub_queue ) )
428 {
429 mlt_properties p = mlt_deque_pop_back( sub_queue );
430 mlt_deque_push_back( data_queue, p );
431 }
432 }
433
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 ) )
436 {
437 // Order of frame creation is starting to get problematic
438 if ( audio != NULL )
439 {
440 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), producer_get_audio );
441 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), audio );
442 }
443 audio = temp;
444 }
445 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
446 {
447 if ( video != NULL )
448 {
449 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), producer_get_image );
450 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), video );
451 }
452 video = temp;
453 if ( first_video == NULL )
454 first_video = temp;
455
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" ) );
459
460 mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp ), "image_count", ++ image_count );
461 image_count = 1;
462 }
463 }
464
465 // Now stack callbacks
466 if ( audio != NULL )
467 {
468 mlt_frame_push_audio( *frame, audio );
469 mlt_frame_push_audio( *frame, producer_get_audio );
470 }
471
472 if ( video != NULL )
473 {
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 );
477 if ( global_feed )
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 );
487 }
488 else
489 {
490 destroy_data_queue( data_queue );
491 }
492
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 );
497 }
498 else if ( producer != NULL )
499 {
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 );
503 }
504 else
505 {
506 mlt_log( MLT_PRODUCER_SERVICE( parent ), MLT_LOG_ERROR, "tractor without a multitrack!!\n" );
507 mlt_service_get_frame( this->producer, frame, track );
508 }
509
510 // Prepare the next frame
511 mlt_producer_prepare_next( parent );
512
513 // Indicate our found status
514 return 0;
515 }
516 else
517 {
518 // Generate a test card
519 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
520 return 0;
521 }
522 }
523
524 /** Close the tractor and free its resources.
525 *
526 * \public \memberof mlt_tractor_s
527 * \param this a tractor
528 */
529
530 void mlt_tractor_close( mlt_tractor this )
531 {
532 if ( this != NULL && mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( this ) ) <= 0 )
533 {
534 this->parent.close = NULL;
535 mlt_producer_close( &this->parent );
536 free( this );
537 }
538 }
539