move binary modules to libdir - affects MLT_REPOSITORY
[melted] / src / framework / mlt_tractor.c
1 /*
2 * mlt_tractor.c -- tractor service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "mlt_tractor.h"
22 #include "mlt_frame.h"
23 #include "mlt_multitrack.h"
24 #include "mlt_field.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 /** Forward references to static methods.
32 */
33
34 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
35 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this );
36
37 /** Constructor for the tractor.
38 */
39
40 mlt_tractor mlt_tractor_init( )
41 {
42 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
43 if ( this != NULL )
44 {
45 mlt_producer producer = &this->parent;
46 if ( mlt_producer_init( producer, this ) == 0 )
47 {
48 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
49
50 mlt_properties_set( properties, "resource", "<tractor>" );
51 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
52 mlt_properties_set( properties, "mlt_service", "tractor" );
53 mlt_properties_set_int( properties, "in", 0 );
54 mlt_properties_set_int( properties, "out", -1 );
55 mlt_properties_set_int( properties, "length", 0 );
56
57 producer->get_frame = producer_get_frame;
58 producer->close = ( mlt_destructor )mlt_tractor_close;
59 producer->close_object = this;
60 }
61 else
62 {
63 free( this );
64 this = NULL;
65 }
66 }
67 return this;
68 }
69
70 mlt_tractor mlt_tractor_new( )
71 {
72 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
73 if ( this != NULL )
74 {
75 mlt_producer producer = &this->parent;
76 if ( mlt_producer_init( producer, this ) == 0 )
77 {
78 mlt_multitrack multitrack = mlt_multitrack_init( );
79 mlt_field field = mlt_field_new( multitrack, this );
80 mlt_properties props = MLT_PRODUCER_PROPERTIES( producer );
81
82 mlt_properties_set( props, "resource", "<tractor>" );
83 mlt_properties_set( props, "mlt_type", "mlt_producer" );
84 mlt_properties_set( props, "mlt_service", "tractor" );
85 mlt_properties_set_position( props, "in", 0 );
86 mlt_properties_set_position( props, "out", 0 );
87 mlt_properties_set_position( props, "length", 0 );
88 mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
89 mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
90
91 mlt_events_listen( MLT_MULTITRACK_PROPERTIES( multitrack ), this, "producer-changed", ( mlt_listener )mlt_tractor_listener );
92
93 producer->get_frame = producer_get_frame;
94 producer->close = ( mlt_destructor )mlt_tractor_close;
95 producer->close_object = this;
96 }
97 else
98 {
99 free( this );
100 this = NULL;
101 }
102 }
103 return this;
104 }
105
106 /** Get the service object associated to the tractor.
107 */
108
109 mlt_service mlt_tractor_service( mlt_tractor this )
110 {
111 return MLT_PRODUCER_SERVICE( &this->parent );
112 }
113
114 /** Get the producer object associated to the tractor.
115 */
116
117 mlt_producer mlt_tractor_producer( mlt_tractor this )
118 {
119 return this != NULL ? &this->parent : NULL;
120 }
121
122 /** Get the properties object associated to the tractor.
123 */
124
125 mlt_properties mlt_tractor_properties( mlt_tractor this )
126 {
127 return MLT_PRODUCER_PROPERTIES( &this->parent );
128 }
129
130 /** Get the field this tractor is harvesting.
131 */
132
133 mlt_field mlt_tractor_field( mlt_tractor this )
134 {
135 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "field", NULL );
136 }
137
138 /** Get the multitrack this tractor is pulling.
139 */
140
141 mlt_multitrack mlt_tractor_multitrack( mlt_tractor this )
142 {
143 return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( this ), "multitrack", NULL );
144 }
145
146 /** Ensure the tractors in/out points match the multitrack.
147 */
148
149 void mlt_tractor_refresh( mlt_tractor this )
150 {
151 mlt_multitrack multitrack = mlt_tractor_multitrack( this );
152 mlt_properties properties = MLT_MULTITRACK_PROPERTIES( multitrack );
153 mlt_properties self = MLT_TRACTOR_PROPERTIES( this );
154 mlt_events_block( properties, self );
155 mlt_events_block( self, self );
156 mlt_multitrack_refresh( multitrack );
157 mlt_properties_set_position( self, "in", 0 );
158 mlt_properties_set_position( self, "out", mlt_properties_get_position( properties, "out" ) );
159 mlt_events_unblock( self, self );
160 mlt_events_unblock( properties, self );
161 mlt_properties_set_position( self, "length", mlt_properties_get_position( properties, "length" ) );
162 }
163
164 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this )
165 {
166 mlt_tractor_refresh( this );
167 }
168
169 /** Connect the tractor.
170 */
171
172 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
173 {
174 int ret = mlt_service_connect_producer( MLT_TRACTOR_SERVICE( this ), producer, 0 );
175
176 // This is the producer we're going to connect to
177 if ( ret == 0 )
178 this->producer = producer;
179
180 return ret;
181 }
182
183 /** Set the producer for a specific track.
184 */
185
186 int mlt_tractor_set_track( mlt_tractor this, mlt_producer producer, int index )
187 {
188 return mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer, index );
189 }
190
191 /** Get the producer for a specific track.
192 */
193
194 mlt_producer mlt_tractor_get_track( mlt_tractor this, int index )
195 {
196 return mlt_multitrack_track( mlt_tractor_multitrack( this ), index );
197 }
198
199 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
200 {
201 uint8_t *data = NULL;
202 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
203 mlt_frame frame = mlt_frame_pop_service( this );
204 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
205 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
206 mlt_properties_set_int( frame_properties, "resize_alpha", mlt_properties_get_int( properties, "resize_alpha" ) );
207 mlt_properties_set_int( frame_properties, "distort", mlt_properties_get_int( properties, "distort" ) );
208 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
209 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "consumer_deinterlace" ) );
210 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
211 mlt_properties_set_int( frame_properties, "normalised_width", mlt_properties_get_int( properties, "normalised_width" ) );
212 mlt_properties_set_int( frame_properties, "normalised_height", mlt_properties_get_int( properties, "normalised_height" ) );
213 mlt_frame_get_image( frame, buffer, format, width, height, writable );
214 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
215 mlt_properties_set_int( properties, "width", *width );
216 mlt_properties_set_int( properties, "height", *height );
217 mlt_properties_set_int( properties, "format", *format );
218 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( frame ) );
219 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
220 mlt_properties_set_int( properties, "distort", mlt_properties_get_int( frame_properties, "distort" ) );
221 data = mlt_frame_get_alpha_mask( frame );
222 mlt_properties_set_data( properties, "alpha", data, 0, NULL, NULL );
223 return 0;
224 }
225
226 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
227 {
228 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
229 mlt_frame frame = mlt_frame_pop_audio( this );
230 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
231 mlt_properties_set_data( properties, "audio", *buffer, 0, NULL, NULL );
232 mlt_properties_set_int( properties, "frequency", *frequency );
233 mlt_properties_set_int( properties, "channels", *channels );
234 return 0;
235 }
236
237 static void destroy_data_queue( void *arg )
238 {
239 if ( arg != NULL )
240 {
241 // Assign the correct type
242 mlt_deque queue = arg;
243
244 // Iterate through each item and destroy them
245 while ( mlt_deque_peek_front( queue ) != NULL )
246 mlt_properties_close( mlt_deque_pop_back( queue ) );
247
248 // Close the deque
249 mlt_deque_close( queue );
250 }
251 }
252
253 /** Get the next frame.
254
255 TODO: This function needs to be redesigned...
256 */
257
258 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
259 {
260 mlt_tractor this = parent->child;
261
262 // We only respond to the first track requests
263 if ( track == 0 && this->producer != NULL )
264 {
265 int i = 0;
266 int done = 0;
267 mlt_frame temp = NULL;
268 int count = 0;
269 int image_count = 0;
270
271 // Get the properties of the parent producer
272 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
273
274 // Try to obtain the multitrack associated to the tractor
275 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
276
277 // Or a specific producer
278 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
279
280 // The output frame will hold the 'global' data feeds (ie: those which are targetted for the final frame)
281 mlt_deque data_queue = mlt_deque_init( );
282
283 // Determine whether this tractor feeds to the consumer or stops here
284 int global_feed = mlt_properties_get_int( properties, "global_feed" );
285
286 // If we don't have one, we're in trouble...
287 if ( multitrack != NULL )
288 {
289 // Used to garbage collect all frames
290 char label[ 30 ];
291
292 // Get the id of the tractor
293 char *id = mlt_properties_get( properties, "_unique_id" );
294
295 // Will be used to store the frame properties object
296 mlt_properties frame_properties = NULL;
297
298 // We'll store audio and video frames to use here
299 mlt_frame audio = NULL;
300 mlt_frame video = NULL;
301 mlt_frame first_video = NULL;
302
303 // Temporary properties
304 mlt_properties temp_properties = NULL;
305
306 // Get the multitrack's producer
307 mlt_producer target = MLT_MULTITRACK_PRODUCER( multitrack );
308 mlt_producer_seek( target, mlt_producer_frame( parent ) );
309 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
310
311 // We will create one frame and attach everything to it
312 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
313
314 // Get the properties of the frame
315 frame_properties = MLT_FRAME_PROPERTIES( *frame );
316
317 // Loop through each of the tracks we're harvesting
318 for ( i = 0; !done; i ++ )
319 {
320 // Get a frame from the producer
321 mlt_service_get_frame( this->producer, &temp, i );
322
323 // Get the temporary properties
324 temp_properties = MLT_FRAME_PROPERTIES( temp );
325
326 // Check for last track
327 done = mlt_properties_get_int( temp_properties, "last_track" );
328
329 // Handle fx only tracks
330 if ( mlt_properties_get_int( temp_properties, "fx_cut" ) )
331 {
332 int hide = ( video == NULL ? 1 : 0 ) | ( audio == NULL ? 2 : 0 );
333 mlt_properties_set_int( temp_properties, "hide", hide );
334 }
335
336 // We store all frames with a destructor on the output frame
337 sprintf( label, "_%s_%d", id, count ++ );
338 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
339
340 // We want to append all 'final' feeds to the global queue
341 if ( !done && mlt_properties_get_data( temp_properties, "data_queue", NULL ) != NULL )
342 {
343 // Move the contents of this queue on to the output frames data queue
344 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "data_queue", NULL );
345 mlt_deque temp = mlt_deque_init( );
346 while ( global_feed && mlt_deque_count( sub_queue ) )
347 {
348 mlt_properties p = mlt_deque_pop_back( sub_queue );
349 if ( mlt_properties_get_int( p, "final" ) )
350 mlt_deque_push_back( data_queue, p );
351 else
352 mlt_deque_push_back( temp, p );
353 }
354 while( mlt_deque_count( temp ) )
355 mlt_deque_push_front( sub_queue, mlt_deque_pop_back( temp ) );
356 mlt_deque_close( temp );
357 }
358
359 // Now do the same with the global queue but without the conditional behaviour
360 if ( mlt_properties_get_data( temp_properties, "global_queue", NULL ) != NULL )
361 {
362 mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "global_queue", NULL );
363 while ( mlt_deque_count( sub_queue ) )
364 {
365 mlt_properties p = mlt_deque_pop_back( sub_queue );
366 mlt_deque_push_back( data_queue, p );
367 }
368 }
369
370 // Pick up first video and audio frames
371 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 2 ) )
372 {
373 // Order of frame creation is starting to get problematic
374 if ( audio != NULL )
375 {
376 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), producer_get_audio );
377 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), audio );
378 }
379 audio = temp;
380 }
381 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
382 {
383 if ( video != NULL )
384 {
385 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), producer_get_image );
386 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), video );
387 }
388 video = temp;
389 if ( first_video == NULL )
390 first_video = temp;
391
392 // Ensure that all frames know the aspect ratio of the background
393 mlt_properties_set_double( temp_properties, "output_ratio",
394 mlt_properties_get_double( MLT_FRAME_PROPERTIES( first_video ), "aspect_ratio" ) );
395
396 mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp ), "image_count", ++ image_count );
397 image_count = 1;
398 }
399 }
400
401 // Now stack callbacks
402 if ( audio != NULL )
403 {
404 mlt_frame_push_audio( *frame, audio );
405 mlt_frame_push_audio( *frame, producer_get_audio );
406 }
407
408 if ( video != NULL )
409 {
410 mlt_properties video_properties = MLT_FRAME_PROPERTIES( first_video );
411 mlt_frame_push_service( *frame, video );
412 mlt_frame_push_service( *frame, producer_get_image );
413 if ( global_feed )
414 mlt_properties_set_data( frame_properties, "data_queue", data_queue, 0, NULL, NULL );
415 mlt_properties_set_data( video_properties, "global_queue", data_queue, 0, destroy_data_queue, NULL );
416 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( video_properties, "width" ) );
417 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( video_properties, "height" ) );
418 mlt_properties_set_int( frame_properties, "real_width", mlt_properties_get_int( video_properties, "real_width" ) );
419 mlt_properties_set_int( frame_properties, "real_height", mlt_properties_get_int( video_properties, "real_height" ) );
420 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( video_properties, "progressive" ) );
421 mlt_properties_set_double( frame_properties, "aspect_ratio", mlt_properties_get_double( video_properties, "aspect_ratio" ) );
422 mlt_properties_set_int( frame_properties, "image_count", image_count );
423 }
424 else
425 {
426 destroy_data_queue( data_queue );
427 }
428
429 mlt_frame_set_position( *frame, mlt_producer_frame( parent ) );
430 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", audio == NULL );
431 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", video == NULL );
432 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "consumer_lock_service", this, 0, NULL, NULL );
433 }
434 else if ( producer != NULL )
435 {
436 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
437 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
438 mlt_service_get_frame( this->producer, frame, track );
439 }
440 else
441 {
442 fprintf( stderr, "tractor without a multitrack!!\n" );
443 mlt_service_get_frame( this->producer, frame, track );
444 }
445
446 // Prepare the next frame
447 mlt_producer_prepare_next( parent );
448
449 // Indicate our found status
450 return 0;
451 }
452 else
453 {
454 // Generate a test card
455 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
456 return 0;
457 }
458 }
459
460 /** Close the tractor.
461 */
462
463 void mlt_tractor_close( mlt_tractor this )
464 {
465 if ( this != NULL && mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( this ) ) <= 0 )
466 {
467 this->parent.close = NULL;
468 mlt_producer_close( &this->parent );
469 free( this );
470 }
471 }
472