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