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