]> git.sesse.net Git - mlt/blob - src/framework/mlt_tractor.c
A little debugging.
[mlt] / 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 self );
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 self = calloc( 1, sizeof( struct mlt_tractor_s ) );
53         if ( self != NULL )
54         {
55                 mlt_producer producer = &self->parent;
56                 if ( mlt_producer_init( producer, self ) == 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 = self;
70                 }
71                 else
72                 {
73                         free( self );
74                         self = NULL;
75                 }
76         }
77         return self;
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 self = calloc( 1, sizeof( struct mlt_tractor_s ) );
92         if ( self != NULL )
93         {
94                 mlt_producer producer = &self->parent;
95                 if ( mlt_producer_init( producer, self ) == 0 )
96                 {
97                         mlt_multitrack multitrack = mlt_multitrack_init( );
98                         mlt_field field = mlt_field_new( multitrack, self );
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 ), self, "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 = self;
115                 }
116                 else
117                 {
118                         free( self );
119                         self = NULL;
120                 }
121         }
122         return self;
123 }
124
125 /** Get the service object associated to the tractor.
126  *
127  * \public \memberof mlt_tractor_s
128  * \param self a tractor
129  * \return the parent service object
130  * \see MLT_TRACTOR_SERVICE
131  */
132
133 mlt_service mlt_tractor_service( mlt_tractor self )
134 {
135         return MLT_PRODUCER_SERVICE( &self->parent );
136 }
137
138 /** Get the producer object associated to the tractor.
139  *
140  * \public \memberof mlt_tractor_s
141  * \param self a tractor
142  * \return the parent producer object
143  * \see MLT_TRACTOR_PRODUCER
144  */
145
146 mlt_producer mlt_tractor_producer( mlt_tractor self )
147 {
148         return self != NULL ? &self->parent : NULL;
149 }
150
151 /** Get the properties object associated to the tractor.
152  *
153  * \public \memberof mlt_tractor_s
154  * \param self a tractor
155  * \return the tractor's property list
156  * \see MLT_TRACTOR_PROPERTIES
157  */
158
159 mlt_properties mlt_tractor_properties( mlt_tractor self )
160 {
161         return MLT_PRODUCER_PROPERTIES( &self->parent );
162 }
163
164 /** Get the field self tractor is harvesting.
165  *
166  * \public \memberof mlt_tractor_s
167  * \param self 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 self )
172 {
173         return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( self ), "field", NULL );
174 }
175
176 /** Get the multitrack a tractor is pulling.
177  *
178  * \public \memberof mlt_tractor_s
179  * \param self a tractor
180  * \return a multitrack or NULL if there is none
181  */
182
183 mlt_multitrack mlt_tractor_multitrack( mlt_tractor self )
184 {
185         return mlt_properties_get_data( MLT_TRACTOR_PROPERTIES( self ), "multitrack", NULL );
186 }
187
188 /** Ensure the tractors in/out points match the multitrack.
189  *
190  * \public \memberof mlt_tractor_s
191  * \param self a tractor
192  */
193
194 void mlt_tractor_refresh( mlt_tractor self )
195 {
196         mlt_multitrack multitrack = mlt_tractor_multitrack( self );
197         mlt_properties multitrack_props = MLT_MULTITRACK_PROPERTIES( multitrack );
198         mlt_properties properties = MLT_TRACTOR_PROPERTIES( self );
199         mlt_events_block( multitrack_props, properties );
200         mlt_events_block( properties, properties );
201         mlt_multitrack_refresh( multitrack );
202         mlt_properties_set_position( properties, "in", 0 );
203         mlt_properties_set_position( properties, "out", mlt_properties_get_position( multitrack_props, "out" ) );
204         mlt_events_unblock( properties, properties );
205         mlt_events_unblock( multitrack_props, properties );
206         mlt_properties_set_position( properties, "length", mlt_properties_get_position( multitrack_props, "length" ) );
207 }
208
209 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor self )
210 {
211         mlt_tractor_refresh( self );
212 }
213
214 /** Connect the tractor.
215  *
216  * \public \memberof mlt_tractor_s
217  * \param self a tractor
218  * \param producer a producer
219  * \return true on error
220  */
221
222 int mlt_tractor_connect( mlt_tractor self, mlt_service producer )
223 {
224         int ret = mlt_service_connect_producer( MLT_TRACTOR_SERVICE( self ), producer, 0 );
225
226         // This is the producer we're going to connect to
227         if ( ret == 0 )
228                 self->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 self 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 self, mlt_producer producer, int index )
243 {
244         return mlt_multitrack_connect( mlt_tractor_multitrack( self ), producer, index );
245 }
246
247 /** Get the producer for a specific track.
248  *
249  * \public \memberof mlt_tractor_s
250  * \param self 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 self, int index )
256 {
257         return mlt_multitrack_track( mlt_tractor_multitrack( self ), index );
258 }
259
260 static int producer_get_image( mlt_frame self, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
261 {
262         uint8_t *data = NULL;
263         int size = 0;
264         mlt_properties properties = MLT_FRAME_PROPERTIES( self );
265         mlt_frame frame = mlt_frame_pop_service( self );
266         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
267         mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
268         mlt_properties_set_int( frame_properties, "resize_alpha", mlt_properties_get_int( properties, "resize_alpha" ) );
269         mlt_properties_set_int( frame_properties, "distort", mlt_properties_get_int( properties, "distort" ) );
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, "consumer_tff", mlt_properties_get_int( properties, "consumer_tff" ) );
273         mlt_frame_get_image( frame, buffer, format, width, height, writable );
274         mlt_frame_set_image( self, *buffer, 0, 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         mlt_properties_set_int( properties, "colorspace", mlt_properties_get_int( frame_properties, "colorspace" ) );
282         mlt_properties_set_int( properties, "force_full_luma", mlt_properties_get_int( frame_properties, "force_full_luma" ) );
283         mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( frame_properties, "top_field_first" ) );
284         mlt_properties_set_data( properties, "movit.convert.fence",
285                 mlt_properties_get_data( frame_properties, "movit.convert.fence", NULL ),
286                 0, NULL, NULL );
287         data = mlt_frame_get_alpha_mask( frame );
288         mlt_properties_get_data( frame_properties, "alpha", &size );
289         mlt_frame_set_alpha( self, data, size, NULL );
290         self->convert_image = frame->convert_image;
291         self->convert_audio = frame->convert_audio;
292         return 0;
293 }
294
295 static int producer_get_audio( mlt_frame self, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
296 {
297         mlt_properties properties = MLT_FRAME_PROPERTIES( self );
298         mlt_frame frame = mlt_frame_pop_audio( self );
299         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
300         mlt_frame_set_audio( self, *buffer, *format, mlt_audio_format_size( *format, *samples, *channels ), NULL );
301         mlt_properties_set_int( properties, "audio_frequency", *frequency );
302         mlt_properties_set_int( properties, "audio_channels", *channels );
303         mlt_properties_set_int( properties, "audio_samples", *samples );
304         return 0;
305 }
306
307 static void destroy_data_queue( void *arg )
308 {
309         if ( arg != NULL )
310         {
311                 // Assign the correct type
312                 mlt_deque queue = arg;
313
314                 // Iterate through each item and destroy them
315                 while ( mlt_deque_peek_front( queue ) != NULL )
316                         mlt_properties_close( mlt_deque_pop_back( queue ) );
317
318                 // Close the deque
319                 mlt_deque_close( queue );
320         }
321 }
322
323 /** Get the next frame.
324  *
325  * \private \memberof mlt_tractor_s
326  * \param parent the producer interface to the tractor
327  * \param[out] frame a frame by reference
328  * \param track the 0-based track index
329  * \return true on error
330  */
331
332 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
333 {
334         mlt_tractor self = parent->child;
335
336         // We only respond to the first track requests
337         if ( track == 0 && self->producer != NULL )
338         {
339                 int i = 0;
340                 int done = 0;
341                 mlt_frame temp = NULL;
342                 int count = 0;
343                 int image_count = 0;
344
345                 // Get the properties of the parent producer
346                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
347
348                 // Try to obtain the multitrack associated to the tractor
349                 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
350
351                 // Or a specific producer
352                 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
353
354                 // Determine whether this tractor feeds to the consumer or stops here
355                 int global_feed = mlt_properties_get_int( properties, "global_feed" );
356
357                 // If we don't have one, we're in trouble...
358                 if ( multitrack != NULL )
359                 {
360                         // The output frame will hold the 'global' data feeds (ie: those which are targetted for the final frame)
361                         mlt_deque data_queue = mlt_deque_init( );
362
363                         // Used to garbage collect all frames
364                         char label[ 30 ];
365
366                         // Get the id of the tractor
367                         char *id = mlt_properties_get( properties, "_unique_id" );
368
369                         // Will be used to store the frame properties object
370                         mlt_properties frame_properties = NULL;
371
372                         // We'll store audio and video frames to use here
373                         mlt_frame audio = NULL;
374                         mlt_frame video = NULL;
375                         mlt_frame first_video = NULL;
376
377                         // Temporary properties
378                         mlt_properties temp_properties = NULL;
379
380                         // Get the multitrack's producer
381                         mlt_producer target = MLT_MULTITRACK_PRODUCER( multitrack );
382                         mlt_producer_seek( target, mlt_producer_frame( parent ) );
383                         mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
384
385                         // We will create one frame and attach everything to it
386                         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
387
388                         // Get the properties of the frame
389                         frame_properties = MLT_FRAME_PROPERTIES( *frame );
390
391                         // Loop through each of the tracks we're harvesting
392                         for ( i = 0; !done; i ++ )
393                         {
394                                 // Get a frame from the producer
395                                 mlt_service_get_frame( self->producer, &temp, i );
396
397                                 // Get the temporary properties
398                                 temp_properties = MLT_FRAME_PROPERTIES( temp );
399
400                                 // Pass all unique meta properties from the producer's frame to the new frame
401                                 mlt_properties_lock( temp_properties );
402                                 int props_count = mlt_properties_count( temp_properties );
403                                 int j;
404                                 for ( j = 0; j < props_count; j ++ )
405                                 {
406                                         char *name = mlt_properties_get_name( temp_properties, j );
407                                         if ( !strncmp( name, "meta.", 5 ) && !mlt_properties_get( frame_properties, name ) )
408                                                 mlt_properties_set( frame_properties, name, mlt_properties_get_value( temp_properties, j ) );
409                                 }
410                                 mlt_properties_unlock( temp_properties );
411
412                                 // Copy the format conversion virtual functions
413                                 if ( ! (*frame)->convert_image && temp->convert_image )
414                                         (*frame)->convert_image = temp->convert_image;
415                                 if ( ! (*frame)->convert_audio && temp->convert_audio )
416                                         (*frame)->convert_audio = temp->convert_audio;
417
418                                 // Check for last track
419                                 done = mlt_properties_get_int( temp_properties, "last_track" );
420
421                                 // Handle fx only tracks
422                                 if ( mlt_properties_get_int( temp_properties, "fx_cut" ) )
423                                 {
424                                         int hide = ( video == NULL ? 1 : 0 ) | ( audio == NULL ? 2 : 0 );
425                                         mlt_properties_set_int( temp_properties, "hide", hide );
426                                 }
427
428                                 // We store all frames with a destructor on the output frame
429                                 sprintf( label, "_%s_%d", id, count ++ );
430                                 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
431
432                                 // We want to append all 'final' feeds to the global queue
433                                 if ( !done && mlt_properties_get_data( temp_properties, "data_queue", NULL ) != NULL )
434                                 {
435                                         // Move the contents of this queue on to the output frames data queue
436                                         mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "data_queue", NULL );
437                                         mlt_deque temp = mlt_deque_init( );
438                                         while ( global_feed && mlt_deque_count( sub_queue ) )
439                                         {
440                                                 mlt_properties p = mlt_deque_pop_back( sub_queue );
441                                                 if ( mlt_properties_get_int( p, "final" ) )
442                                                         mlt_deque_push_back( data_queue, p );
443                                                 else
444                                                         mlt_deque_push_back( temp, p );
445                                         }
446                                         while( mlt_deque_count( temp ) )
447                                                 mlt_deque_push_front( sub_queue, mlt_deque_pop_back( temp ) );
448                                         mlt_deque_close( temp );
449                                 }
450
451                                 // Now do the same with the global queue but without the conditional behaviour
452                                 if ( mlt_properties_get_data( temp_properties, "global_queue", NULL ) != NULL )
453                                 {
454                                         mlt_deque sub_queue = mlt_properties_get_data( MLT_FRAME_PROPERTIES( temp ), "global_queue", NULL );
455                                         while ( mlt_deque_count( sub_queue ) )
456                                         {
457                                                 mlt_properties p = mlt_deque_pop_back( sub_queue );
458                                                 mlt_deque_push_back( data_queue, p );
459                                         }
460                                 }
461
462                                 // Pick up first video and audio frames
463                                 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 2 ) )
464                                 {
465                                         // Order of frame creation is starting to get problematic
466                                         if ( audio != NULL )
467                                         {
468                                                 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), producer_get_audio );
469                                                 mlt_deque_push_front( MLT_FRAME_AUDIO_STACK( temp ), audio );
470                                         }
471                                         audio = temp;
472                                 }
473                                 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
474                                 {
475                                         if ( video != NULL )
476                                         {
477                                                 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), producer_get_image );
478                                                 mlt_deque_push_front( MLT_FRAME_IMAGE_STACK( temp ), video );
479                                         }
480                                         video = temp;
481                                         if ( first_video == NULL )
482                                                 first_video = temp;
483
484                                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( temp ), "image_count", ++ image_count );
485                                         image_count = 1;
486                                 }
487                         }
488
489                         // Now stack callbacks
490                         if ( audio != NULL )
491                         {
492                                 mlt_frame_push_audio( *frame, audio );
493                                 mlt_frame_push_audio( *frame, producer_get_audio );
494                         }
495
496                         if ( video != NULL )
497                         {
498                                 mlt_properties video_properties = MLT_FRAME_PROPERTIES( first_video );
499                                 mlt_frame_push_service( *frame, video );
500                                 mlt_frame_push_service( *frame, producer_get_image );
501                                 if ( global_feed )
502                                         mlt_properties_set_data( frame_properties, "data_queue", data_queue, 0, NULL, NULL );
503                                 mlt_properties_set_data( video_properties, "global_queue", data_queue, 0, destroy_data_queue, NULL );
504                                 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( video_properties, "width" ) );
505                                 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( video_properties, "height" ) );
506                                 mlt_properties_pass_list( frame_properties, video_properties, "meta.media.width, meta.media.height" );
507                                 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( video_properties, "progressive" ) );
508                                 mlt_properties_set_double( frame_properties, "aspect_ratio", mlt_properties_get_double( video_properties, "aspect_ratio" ) );
509                                 mlt_properties_set_int( frame_properties, "image_count", image_count );
510                         }
511                         else
512                         {
513                                 destroy_data_queue( data_queue );
514                         }
515
516                         mlt_frame_set_position( *frame, mlt_producer_frame( parent ) );
517                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", audio == NULL );
518                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", video == NULL );
519                 }
520                 else if ( producer != NULL )
521                 {
522                         mlt_producer_seek( producer, mlt_producer_frame( parent ) );
523                         mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
524                         mlt_service_get_frame( self->producer, frame, track );
525                 }
526                 else
527                 {
528                         mlt_log( MLT_PRODUCER_SERVICE( parent ), MLT_LOG_ERROR, "tractor without a multitrack!!\n" );
529                         mlt_service_get_frame( self->producer, frame, track );
530                 }
531
532                 // Prepare the next frame
533                 mlt_producer_prepare_next( parent );
534
535                 // Indicate our found status
536                 return 0;
537         }
538         else
539         {
540                 // Generate a test card
541                 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
542                 return 0;
543         }
544 }
545
546 /** Close the tractor and free its resources.
547  *
548  * \public \memberof mlt_tractor_s
549  * \param self a tractor
550  */
551
552 void mlt_tractor_close( mlt_tractor self )
553 {
554         if ( self != NULL && mlt_properties_dec_ref( MLT_TRACTOR_PROPERTIES( self ) ) <= 0 )
555         {
556                 self->parent.close = NULL;
557                 mlt_producer_close( &self->parent );
558                 free( self );
559         }
560 }
561