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