]> git.sesse.net Git - mlt/blob - docs/framework.txt
12b38713b85b87270f1dad20ff98586c1909863e
[mlt] / docs / framework.txt
1 MLT FRAMEWORK
2 -------------
3
4 Preamble:
5
6         MLT is a multimedia framework designed for television broadcasting. As such, 
7         it provides a pluggable architecture for the inclusion of new audio/video 
8         sources, filters, transitions and playback devices.
9
10         The MLT framework provides the structure and utility functionality on which
11         all of the MLT applications and services are defined. 
12
13         On its own, the framework provides little more than 'abstract classes' and
14         utilities for managing resources, such as memory, properties, dynamic object
15         loading and service instantiation. 
16
17         This document is split roughly into 3 sections. The first section provides a
18         basic overview of MLT, the second section shows how it's used and the final
19         section shows shows structure and design, with an emphasis on how the system
20         is extended.
21
22
23 Target Audience:
24
25         This document is provided as a 'road map' for the framework and should be
26         considered mandatory reading for anyone wishing to develop code at the MLT
27         level. 
28
29         This includes:
30
31         1. framework maintainers;
32         2. module developers;
33         3. application developers;
34         4. anyone interested in MLT.
35
36         The emphasis of the document is in explaining the public interfaces, as
37         opposed to the implementation details.
38
39         It is not required reading for the MLT client/server integration - please
40         refer to valerie.txt and dvcp.txt for more details on this area.
41
42
43 SECTION 1 - BASIC OVERVIEW
44 --------------------------
45
46 Basic Design Information:
47
48         MLT is written in C. 
49         
50         The framework has no dependencies other than the standard C99 and POSIX 
51         libraries. 
52
53         It follows a basic Object Oriented design paradigm, and as such, much of the
54         design is loosely based on the Producer/Consumer design pattern. 
55         
56         It employs Reverse Polish Notation for the application of audio and video FX.
57
58         The framework is designed to be colour space neutral - the currently
59         implemented modules, however, are very much 8bit YUV422 oriented. In theory,
60         the modules could be entirely replaced.
61
62         A vague understanding of these terms is assumed throughout the remainder of
63         this document.
64
65
66 Structure and Flow:
67
68         The general structure of an MLT 'network' is simply the connection of a
69         'producer' to a 'consumer':
70
71         +--------+   +--------+
72         |Producer|-->|Consumer|
73         +--------+   +--------+
74
75         A typical consumer requests MLT Frame objects from the producer, does 
76         something with them and when finished with a frame, closes it. 
77         
78          /\  A common confusion with the producer/consumer terminoligy used here is 
79         /!!\ that a consumer may 'produce' something. For example, the libdv consumer
80         \!!/ produces DV and the libdv producer seems to consume DV. However, the
81          \/  naming conventions refer only to producers and consumers of MLT Frames.
82
83         To put it another way - a producer produces MLT Frame objects and a consumer 
84         consumes MLT Frame objects.
85
86         An MLT Frame essentially provides an uncompressed image and its associated
87         audio samples.
88
89         Filters may also be placed between the producer and the consumer:
90
91         +--------+   +------+   +--------+
92         |Producer|-->|Filter|-->|Consumer|
93         +--------+   +------+   +--------+
94
95         A service is the collective name for producers, filters, transitions and
96         consumers. 
97
98         The communications between a connected consumer and producer or service are 
99         carried out in 3 phases:
100
101         * get the frame
102         * get the image
103         * get the audio
104
105         MLT employs 'lazy evaluation' - the image and audio need not be extracted
106         from the source until the get image and audio methods are invoked. 
107
108         In essence, the consumer pulls from what it's connected to - this means that
109         threading is typically in the domain of the consumer implementation and some
110         basic functionality is provided on the consumer class to ensure realtime
111         throughput.
112
113
114 SECTION 2 - USAGE
115 -----------------
116
117 Hello World:
118
119         Before we go in to the specifics of the framework architecture, a working
120         example of usage is provided. 
121         
122         The following simply provides a media player:
123
124         #include <stdio.h>
125         #include <unistd.h>
126         #include <framework/mlt.h>
127
128         int main( int argc, char *argv[] )
129         {
130             // Initialise the factory
131             if ( mlt_factory_init( NULL ) == 0 )
132             {
133                 // Create the default consumer
134                 mlt_consumer hello = mlt_factory_consumer( NULL, NULL );
135
136                 // Create via the default producer
137                 mlt_producer world = mlt_factory_producer( NULL, argv[ 1 ] );
138
139                 // Connect the producer to the consumer
140                 mlt_consumer_connect( hello, mlt_producer_service( world ) );
141
142                 // Start the consumer
143                 mlt_consumer_start( hello );
144
145                 // Wait for the consumer to terminate
146                 while( !mlt_consumer_is_stopped( hello ) )
147                     sleep( 1 );
148
149                 // Close the consumer
150                 mlt_consumer_close( hello );
151
152                 // Close the producer
153                 mlt_producer_close( world );
154
155                 // Close the factory
156                 mlt_factory_close( );
157             }
158             else
159             {
160                 // Report an error during initialisation
161                 fprintf( stderr, "Unable to locate factory modules\n" );
162             }
163
164             // End of program
165             return 0;
166         }
167
168         This is a simple example - it doesn't provide any seeking capabilities or
169         runtime configuration options. 
170
171         The first step of any MLT application is the factory initialisation - this
172         ensures that the environment is configured and MLT can function. The factory
173         is covered in more detail below.
174
175         All services are instantiated via the factories, as shown by the
176         mlt_factory_consumer and mlt_factory_producer calls above. There are similar
177         factories for filters and transitions. There are details on all the standard
178         services in services.txt. 
179
180         The defaults requested here are a special case - the NULL usage requests
181         that we use the default producers and consumers. 
182         
183         The default producer is "fezzik". This producer matches file names to 
184         locate a service to use and attaches 'normalising filters' (such as scalers,
185         deinterlacers, resamplers and field normalisers) to the loaded content -
186         these filters ensure that the consumer gets what it asks for.
187
188         The default consumer is "sdl". The combination of fezzik and sdl will
189         provide a media player.
190
191         In this example, we connect the producer and then start the consumer. We
192         then wait until the consumer is stopped (in this case, by the action of the
193         user closing the SDL window) and finally close the consumer, producer and
194         factory before exiting the application.
195
196         Note that the consumer is threaded - waiting for an event of some sort is 
197         always required after starting and before stopping or closing the consumer.
198
199         Also note, you can override the defaults as follows:
200
201         $ MLT_CONSUMER=westley ./hello file.avi
202
203         This will create a westley xml document on stdout.
204
205         $ MLT_CONSUMER=westley MLT_PRODUCER=avformat ./hello file.avi
206
207         This will play the video using the avformat producer directly, thus it will
208         bypass the normalising functions.
209
210         $ MLT_CONSUMER=libdv ./hello file.avi > /dev/dv1394
211
212         This might, if you're lucky, do on the fly, realtime conversions of file.avi
213         to DV and broadcast it to your DV device.
214
215
216 Factories:
217
218         As shown in the 'Hello World' example, factories create service objects.
219
220         The framework itself provides no services - they are provided in the form of
221         a plugin structure. A plugin is organised in the form of a 'module' and a
222         module can provide many services of different types. 
223
224         Once the factory is initialised, all the configured services are available
225         for use.
226
227         The complete set of methods associated to the factory are as follows:
228
229         int mlt_factory_init( char *prefix );
230         const char *mlt_factory_prefix( );
231         char *mlt_environment( char *name );
232         mlt_producer mlt_factory_producer( char *name, void *input );
233         mlt_filter mlt_factory_filter( char *name, void *input );
234         mlt_transition mlt_factory_transition( char *name, void *input );
235         mlt_consumer mlt_factory_consumer( char *name, void *input );
236         void mlt_factory_close( );
237
238         The mlt_factory_prefix returns the path to the location of the installed
239         modules directory. This can be specified in the mlt_factory_init call
240         itself, or it can be specified via the MLT_REPOSITORY environment variable,
241         or in the absence of either of those, it will default to the install
242         prefix/shared/mlt/modules. 
243
244         The mlt_environment provides read only access to a collection of name=value
245         pairs as shown in the following table:
246
247         +------------------+------------------------------------+------------------+
248         |Name              |Description                         |Values            |
249         +------------------+------------------------------------+------------------+
250         |MLT_NORMALISATION |The normalisation of the system     |PAL or NTSC       |
251         +------------------+------------------------------------+------------------+
252         |MLT_PRODUCER      |The default producer                |"fezzik" or other |
253         +------------------+------------------------------------+------------------+
254         |MLT_CONSUMER      |The default consumer                |"sdl" or other    |
255         +------------------+------------------------------------+------------------+
256
257         These values are initialised from the environment variables of the same
258         name.
259
260         As shown above, a producer can be created using the 'default normalising'
261         producer, and they can also be requested by name. Filters and transitions 
262         are always requested by name - there is no concept of a 'default' for these.
263
264
265 Service Properties:
266
267         As shown in the services.txt document, all services have their own set of
268         properties than can be manipulated to affect their behaviour.
269
270         In order to set properties on a service, we need to retrieve the properties
271         object associated to it. For producers, this is done by invoking:
272
273             mlt_properties properties = mlt_producer_properties( producer );
274
275         All services have a similar method associated to them.
276
277         Once retrieved, setting and getting properties can be done directly on this
278         object, for example:
279
280             mlt_properties_set( properties, "name", "value" );
281         
282         A more complete description of the properties object is found below.
283
284
285 Playlists:
286
287         So far, we've shown a simple producer/consumer configuration - the next
288         phase is to organise producers in playlists.
289
290         Let's assume that we're adapting the Hello World example, and wish to queue
291         a number of files for playout, ie:
292
293                 hello *.avi
294
295         Instead of invoking mlt_factory_producer directly, we'll create a new
296         function called create_playlist. This function is responsible for creating
297         the playlist, creating each producer, appending to the playlist and ensuring
298         that all the producers are cleaned up when the playlist is destroyed. The
299         last point is important - a close on the playlist won't explicitly these 
300         producers. In this example, we use unique "data" properties with destructors
301         to ensure closing.
302
303         mlt_producer create_playlist( int argc, char **argv )
304         {
305             // We're creating a playlist here
306             mlt_playlist playlist = mlt_playlist_init( );
307
308             // We need the playlist properties to ensure clean up
309             mlt_properties properties = mlt_playlist_properties( playlist );
310
311             // Loop through each of the arguments
312             int i = 0;
313             for ( i = 1; i < argc; i ++ )
314             {
315                 // Definie the unique key
316                 char key[ 256 ];
317
318                 // Create the producer
319                 mlt_producer producer = mlt_factory_producer( NULL, argv[ i ] );
320
321                 // Add it to the playlist
322                 mlt_playlist_append( playlist, producer );
323
324                 // Create a unique key for this producer
325                 sprintf( key, "producer%d", i );
326
327                 // Now we need to ensure the producers are destroyed
328                 mlt_properties_set_data( properties, key, producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
329             }
330
331             // Return the playlist as a producer
332             return mlt_playlist_producer( playlist );
333         }
334
335         Now all we need do is to replace these lines in the main function:
336
337             // Create a normalised producer
338             mlt_producer world = mlt_factory_producer( NULL, argv[ 1 ] );
339
340         with:
341
342             // Create a playlist
343             mlt_producer world = create_playlist( argc, argv );
344
345         and we have a means to play multiple clips.
346
347
348 Filters:
349
350         Inserting filters between the producer and consumer is just a case of
351         instantiating the filters, connecting the first to the producer, the next
352         to the previous filter and the last filter to the consumer.
353
354         For example:
355
356             // Create a producer from something
357             mlt_producer producer = mlt_factory_producer( ... );
358
359             // Create a consumer from something
360             mlt_consumer consumer = mlt_factory_consumer( ... );
361
362             // Create a greyscale filter
363             mlt_filter filter = mlt_factory_filter( "greyscale", NULL );
364
365             // Connect the filter to the producer
366             mlt_filter_connect( filter, mlt_producer_service( producer ), 0 );
367
368             // Connect the consumer to filter
369             mlt_consumer_connect( consumer, mlt_filter_service( filter ) );
370
371         As with producers and consumers, filters can be manipulated via their
372         properties object - the mlt_filter_properties method can be invoked and
373         properties can be set as needed.
374
375         The additional argument in the filter connection is an important one as it
376         dicates the 'track' on which the filter operates. For basic producers and
377         playlists, there's only one track (0), and as you will see in the next
378         section, even multiple tracks have a single track output.
379
380
381 Multiple Tracks and Transitions:
382
383         MLT's approach to mutliple tracks is governed by two requirements:
384
385         1) The need for a consumer and producer to communicate with one another via
386         a single frame;
387         2) The desire to be able to serialise and manipulate a 'network' (or filter
388         graph if you prefer).
389
390         We can visualise a multitrack in the way that an NLE presents it:
391
392            +-----------------+                          +-----------------------+
393         0: |a1               |                          |a2                     |
394            +---------------+-+--------------------------+-+---------------------+
395         1:                 |b1                            |
396                            +------------------------------+
397
398         The overlapping areas of track 0 and 1 would (presumably) have some kind of
399         transition - without a transition, the frames from a1 and a2 would be shown 
400         during the areas of overlap.
401
402         MLT has a multitrack object, but it is not a producer in the sense that it
403         can be connected directly to a consumer and everything will work correctly.
404         A consumer would treat it precisely as it would a normal producer, and, in
405         the case of the multitrack above, you would never see anything from track 1
406         other than the transitions between the clips - the gap between a1 and a2 
407         would show test frames.
408
409         This happens because a consumer pulls one frame from the producer it's 
410         connected to while a multitrack will provide one frame per track.
411         Something, somewhere, must ensure that all frames are pulled from the
412         multitrack and elect the correct frame to pass on.
413
414         Hence, MLT provides a wrapper for the multitrack, which is called a
415         'tractor', and its the tractors task to ensure that all tracks are pulled
416         evenly, the correct frame is output and that we have 'producer like'
417         behaviour.
418
419         Thus, a mulitrack is conceptually 'pulled' by a tractor as shown here:
420
421         +----------+
422         |multitrack|
423         | +------+ |    +-------+
424         | |track0|-|--->|tractor|
425         | +------+ |    |\      |
426         |          |    | \     |
427         | +------+ |    |  \    |
428         | |track1|-|--->|---o---|--->
429         | +------+ |    |  /    |
430         |          |    | /     |
431         | +------+ |    |/      |
432         | |track2|-|--->|       |
433         | +------+ |    +-------+
434         +----------+
435
436         With a combination of the two, we can now connect multitracks to consumers.
437         The first non-test card will be retrieved and passed on. 
438
439         The tracks can be producers, playlists, or even other tractors. 
440
441         Now we wish to insert filters and transitions between the mulitrack and the
442         tractor. We can do this directly by inserting filters directly between the
443         tractor and the multitrack, but this involves a lot of connecting and
444         reconnecting left and right producers and consumers, and it seemed only fair
445         that we should be able to automate that process. 
446         
447         So in keeping with our agricultural theme, the concept of the 'field' was 
448         born. We 'plant' filters and transitions in the field and the tractor pulls 
449         the multitrack (think of a combine harvester :-)) over the field and 
450         produces a 'bail' (sorry - kidding - frame :-)).
451
452         Conceptually, we can see it like this:
453
454         +----------+
455         |multitrack|
456         | +------+ |    +-------------+    +-------+
457         | |track0|-|--->|field        |--->|tractor|
458         | +------+ |    |             |    |\      |
459         |          |    |   filters   |    | \     |
460         | +------+ |    |     and     |    |  \    |
461         | |track1|-|--->| transitions |--->|---o---|--->
462         | +------+ |    |             |    |  /    |
463         |          |    |             |    | /     |
464         | +------+ |    |             |    |/      |
465         | |track2|-|--->|             |--->|       |
466         | +------+ |    +-------------+    +-------+
467         +----------+
468
469         In reality, we create a field first, and from that we obtain a multitrack
470         and a tractor. We can then populate the multitrack, field and finally,
471         connect the tractor to the consumer. 
472         
473         The reasoning behind this is possibly flawed - it might have made more 
474         sense to produce the tractor and have it encapsulate the field and the
475         multitrack as that is how it looks to a connected consumer:
476
477         +-----------------------------------------------+
478         |tractor          +--------------------------+  |
479         | +----------+    | +-+    +-+    +-+    +-+ |  |
480         | |multitrack|    | |f|    |f|    |t|    |t| |  |
481         | | +------+ |    | |i|    |i|    |r|    |r| |  |
482         | | |track0|-|--->| |l|- ->|l|- ->|a|--->|a|\|  |
483         | | +------+ |    | |t|    |t|    |n|    |n| |  |
484         | |          |    | |e|    |e|    |s|    |s| |\ |
485         | | +------+ |    | |r|    |r|    |i|    |i| | \|
486         | | |track1|-|- ->| |0|--->|1|--->|t|--->|t|-|--o--->
487         | | +------+ |    | | |    | |    |i|    |i| | /|
488         | |          |    | | |    | |    |o|    |o| |/ |
489         | | +------+ |    | | |    | |    |n|    |n| |  |
490         | | |track2|-|- ->| | |- ->| |--->|0|- ->|1|/|  |
491         | | +------+ |    | | |    | |    | |    | | |  |
492         | +----------+    | +-+    +-+    +-+    +-+ |  |
493         |                 +--------------------------+  |
494         +-----------------------------------------------+
495
496         An example will hopefully clarify this. 
497         
498         Let's assume that we want to provide a 'watermark' to our hello world 
499         example. We have already extended the example to play multiple clips,
500         and now we will place a text based watermark, reading 'Hello World' in 
501         the top left hand corner:
502
503         mlt_producer create_tracks( int argc, char **argv )
504         {
505             // Create the field
506             mlt_field field = mlt_field_init( );
507         
508             // Obtain the multitrack
509             mlt_multitrack multitrack = mlt_field_multitrack( field );
510         
511             // Obtain the tractor
512             mlt_tractor tractor = mlt_field_tractor( field );
513         
514             // Obtain a composite transition
515             mlt_transition transition = mlt_factory_transition( "composite", "10%,10%:15%x15%" );
516         
517             // Create track 0
518             mlt_producer track0 = create_playlist( argc, argv );
519         
520             // Create the watermark track - note we NEED fezzik for scaling here
521             mlt_producer track1 = mlt_factory_producer( "fezzik", "pango" );
522         
523             // Get the length of track0
524             mlt_position length = mlt_producer_get_playtime( track0 );
525         
526             // Set the properties of track1
527             mlt_properties properties = mlt_producer_properties( track1 );
528             mlt_properties_set( properties, "text", "Hello\nWorld" );
529             mlt_properties_set_position( properties, "in", 0 );
530             mlt_properties_set_position( properties, "out", length - 1 );
531             mlt_properties_set_position( properties, "length", length );
532         
533             // Now set the properties on the transition
534             properties = mlt_transition_properties( transition );
535             mlt_properties_set_position( properties, "in", 0 );
536             mlt_properties_set_position( properties, "out", length - 1 );
537         
538             // Add our tracks to the multitrack
539             mlt_multitrack_connect( multitrack, track0, 0 );
540             mlt_multitrack_connect( multitrack, track1, 1 );
541         
542             // Now plant the transition
543             mlt_field_plant_transition( field, transition, 0, 1 );
544         
545             // Now set the properties on the tractor
546             properties = mlt_tractor_properties( tractor );
547             mlt_properties_set_data( properties, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
548             mlt_properties_set_data( properties, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
549             mlt_properties_set_data( properties, "track0", track0, 0, ( mlt_destructor )mlt_producer_close, NULL );
550             mlt_properties_set_data( properties, "track1", track1, 0, ( mlt_destructor )mlt_producer_close, NULL );
551             mlt_properties_set_data( properties, "transition", transition, 0, ( mlt_destructor )mlt_transition_close, NULL );
552         
553             // Return the tractor
554             return mlt_tractor_producer( tractor );
555         }
556
557         Now all we need do is to replace these lines in the main function:
558
559             // Create a playlist
560             mlt_producer world = create_playlist( argc, argv );
561
562         with:
563
564             // Create a watermarked playlist
565             mlt_producer world = create_tracks( argc, argv );
566
567         and we have a means to play multiple clips with a horribly obtrusive
568         watermark - just what the world needed, right? ;-)
569
570
571 SECTION 3 - STRUCTURE AND DESIGN
572 --------------------------------
573
574 Class Heirachy:
575
576         The mlt framework consists of an OO class heirachy which consists of the
577         following public classes and abstractions:
578
579         mlt_properties
580           mlt_frame
581           mlt_service
582             mlt_producer
583               mlt_playlist
584               mlt_tractor
585             mlt_filter
586             mlt_transition
587             mlt_consumer
588         mlt_deque
589         mlt_pool
590         mlt_factory
591
592         Each class defined above can be read as extending the classes above and to
593         the left.
594
595         The following sections describe the properties, stacking/queuing and memory 
596         pooling functionality provided by the framework - these are key components 
597         and a basic understanding of these is required for the remainder of the
598         documentation.
599
600
601 mlt_properties:
602
603         The properties class is the base class for the frame and service classes.
604
605         It is designed to provide an efficient lookup table for various types of
606         information, such as strings, integers, floating points values and pointers
607         to data and data structures. 
608
609         All properties are indexed by a unique string. 
610         
611         The most basic use of properties is as follows:
612
613             // 1. Create a new, empty properties set;
614             mlt_properties properties = mlt_properties_new( );
615
616             // 2. Assign the value "world" to the property "hello";
617             mlt_properties_set( properties, "hello", "world" );
618
619             // 3. Retrieve and print the value of "hello";
620             printf( "%s\n", mlt_properties_get( properties, "hello" ) );
621
622             // 4. Reassign "hello" to "world!";
623             mlt_properties_set( properties, "hello", "world!" );
624
625             // 5. Retrieve and print the value of "hello";
626             printf( "%s\n", mlt_properties_get( properties, "hello" ) );
627
628             // 6. Assign the value "0" to "int";
629             mlt_properties_set( properties, "int", "0" );
630
631             // 7. Retrieve and print the integer value of "int";
632             printf( "%d\n", mlt_properties_get_int( properties, "int" ) );
633
634             // 8. Assign the integer value 50 to "int2";
635             mlt_properties_set_int( properties, "int2", 50 );
636
637             // 9. Retrieve and print the double value of "int2";
638             printf( "%s\n", mlt_properties_get( properties, "int2" ) );
639
640         Steps 2 through 5 demonstrate that the "name" is unique - set operations on 
641         an existing "name" change the value. They also free up memory associated to 
642         the previous value. Note that it also possible to change type in this way
643         too.
644
645         Steps 6 and 7 demonstrate that the properties object handles deserialisation
646         from strings. The string value of "50" is set, the integer value of 50 is
647         retrieved.
648
649         Steps 8 and 9 demonstrate that the properties object handles serialisation
650         to strings.
651
652         To show all the name/value pairs in a properties, it is possible to iterate
653         through them:
654
655             for ( i = 0; i < mlt_properties_count( properties ); i ++ )
656                 printf( "%s = %s\n", mlt_properties_get_name( properties, i ),
657                                      mlt_properties_get_value( properties, i ) );
658
659         Note that properties are retrieved in the order in which they are set. 
660
661         Properties are also used to hold pointers to memory. This is done via the
662         set_data call:
663
664         uint8_t *image = malloc( size );
665         mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
666
667         In this example, we specify that the pointer can be retrieved from
668         properties by a subsequent request to get_data:
669
670             image = mlt_properties_get_data( properties, "image", &size );
671
672         or:
673
674             image = mlt_properties_get_data( properties, "image", NULL );
675
676         if we don't wish to retrieve the size.
677
678         Two points here:
679
680         1) The allocated memory remains after the properties object is closed unless
681            you specify a destructor. In the case above, this can be done with:
682
683            mlt_properties_set_data( properties, "image", image, size, free, NULL );
684
685            When the properties are closed, or the value of "image" is changed, the 
686            destructor is invoked.
687         
688         2) The string value returned by mlt_properties_get is NULL. Typically, you
689            wouldn't wish to serialise an image as a string, but other structures
690            might need such functionality - you can specify a serialiser as the last
691            argument if required (declaration is char *serialise( void * )).
692
693         Properties also provides some more advanced usage capabilities. 
694         
695         It has the ability to inherit all serialisable values from another properties 
696         object:
697
698             mlt_properties_inherit( this, that );
699
700         It has the ability to mirror properties set on this on another set of
701         properties:
702
703             mlt_properties_mirror( this, that );
704
705         After this call, all serialisable values set on this are passed on to that.
706
707
708 mlt_deque:
709
710         Stacks and queues are essential components in the MLT framework. Being of a
711         lazy disposition, we elected to implement a 'Double Ended Queue' (deque) -
712         this encapsulates the functionality of both.
713
714         The API of the deque is defined as follows:
715
716         mlt_deque mlt_deque_init( );
717         int mlt_deque_count( mlt_deque this );
718         int mlt_deque_push_back( mlt_deque this, void *item );
719         void *mlt_deque_pop_back( mlt_deque this );
720         int mlt_deque_push_front( mlt_deque this, void *item );
721         void *mlt_deque_pop_front( mlt_deque this );
722         void *mlt_deque_peek_back( mlt_deque this );
723         void *mlt_deque_peek_front( mlt_deque this );
724         void mlt_deque_close( mlt_deque this );
725
726         The stacking operations are used in a number of places:
727
728         * Reverse Polish Notation (RPN) image and audio operations
729         * memory pooling
730
731         The queuing operations are used in:
732         
733         * the consumer base class;
734         * consumer implementations may require further queues.
735
736
737 mlt_pool:
738
739         The MLT framework provides memory pooling capabilities through the mlt_pool
740         API. Once initilialised, these can be seen as a straightforward drop in
741         replacement for malloc/realloc/free functionality.
742
743         The background behind this API is that malloc/free operations are
744         notoriously inefficient, especially when dealing with large blocks of memory
745         (such as an image). On linux, malloc is optimised for memory allocations
746         less than 128k - memory blocks allocated of these sizes or less are retained
747         in the process heap for subsequent reuse, thus bypassing the kernel calls
748         for repeated allocation/frees for small blocks of memory. However, blocks of
749         memory larger than that require kernel calls and this has a detrimental
750         impact on performance.
751
752         The mlt_pool design is simply to hold a list of stacks - there is one stack
753         per 2^n bytes (where n is between 8 and 31). When an alloc is called, the
754         requested size is rounded to the next 2^n, the stack is retrieved for that
755         size, and an item is popped or created if the stack is empty. 
756
757         Each item has a 'header', situated immediately before the returned address - 
758         this holds the 'stack' to which the item belongs.
759
760         When an item is released, we retrieve the header, obtain the stack and push
761         it back.
762
763         Thus, from the programmers point of view, the API is the same as the
764         traditional malloc/realloc/free calls:
765
766         void *mlt_pool_alloc( int size );
767         void *mlt_pool_realloc( void *ptr, int size );
768         void mlt_pool_release( void *release );
769
770
771 mlt_frame:
772
773         A frame object is essentially defined as:
774
775         +------------+
776         |frame       |
777         +------------+
778         | properties |
779         | image stack|
780         | audio stack|
781         +------------+
782
783         The life cycle of a frame can be represented as follows:
784
785         +-----+----------------------+-----------------------+---------------------+
786         |Stage|Producer              |Filter                 |Consumer             |
787         +-----+----------------------+-----------------------+---------------------+
788         | 0.0 |                      |                       |Request frame        |
789         +-----+----------------------+-----------------------+---------------------+
790         | 0.1 |                      |Receives request       |                     |
791         |     |                      |Request frame          |                     |
792         +-----+----------------------+-----------------------+---------------------+
793         | 0.2 |Receives request      |                       |                     |
794         |     |Generates frame for   |                       |                     |
795         |     |current position      |                       |                     |
796         |     |Increments position   |                       |                     |
797         +-----+----------------------+-----------------------+---------------------+
798         | 0.3 |                      |Receives frame         |                     |
799         |     |                      |Updates frame          |                     |
800         +-----+----------------------+-----------------------+---------------------+
801         | 0.4 |                      |                       |Receives frame       |
802         +-----+----------------------+-----------------------+---------------------+
803
804         Note that neither the filter nor the consumer have any conception of
805         'position' until they receive a frame. Speed and position are properties of
806         the producer, and they are assigned to the frame object when the producer
807         creates it.
808
809         Step 0.3 is a critical one here - if the filter determines that the frame is
810         of interest to it, then it should manipulate the image and/or audio stacks
811         and properties as required.
812
813         Assuming that the filter deals with both image and audio, then it should
814         push data and methods on to the stacks which will deal with the processing. 
815         This can be done with the mlt_frame_push_image and audio methods. In order for 
816         the filter to register interest in the frame, the stacks should hold:
817
818         image stack:
819         [ producer_get_image ] [ data1 ] [ data2 ] [ filter_get_image ]
820
821         audio stack:
822         [ producer_get_audio ] [ data ] [ filter_get_audio ]
823
824         The filter_get methods are invoked automatically when the consumer invokes a
825         get_image on the frame. 
826
827         +-----+----------------------+-----------------------+---------------------+
828         |Stage|Producer              |Filter                 |Consumer             |
829         +-----+----------------------+-----------------------+---------------------+
830         | 1.0 |                      |                       |frame_get_image      |
831         +-----+----------------------+-----------------------+---------------------+
832         | 1.1 |                      |filter_get_image:      |                     |
833         |     |                      | pop data2 and data1   |                     |
834         |     |                      | frame_get_image       |                     |
835         +-----+----------------------+-----------------------+---------------------+
836         | 1.2 |producer_get_image    |                       |                     |
837         |     |  Generates image     |                       |                     |
838         +-----+----------------------+-----------------------+---------------------+
839         | 1.3 |                      |Receives image         |                     |
840         |     |                      |Updates image          |                     |
841         +-----+----------------------+-----------------------+---------------------+
842         | 1.4 |                      |                       |Receives image       |
843         +-----+----------------------+-----------------------+---------------------+
844
845         Obviously, if the filter isn't interested in the image, then it should leave
846         the stack alone, and then the consumer will retrieve its image directly from
847         the producer.
848
849         Similarly, audio is handled as follows:
850
851         +-----+----------------------+-----------------------+---------------------+
852         |Stage|Producer              |Filter                 |Consumer             |
853         +-----+----------------------+-----------------------+---------------------+
854         | 2.0 |                      |                       |frame_get_audio      |
855         +-----+----------------------+-----------------------+---------------------+
856         | 2.1 |                      |filter_get_audio:      |                     |
857         |     |                      | pop data              |                     |
858         |     |                      | frame_get_audio       |                     |
859         +-----+----------------------+-----------------------+---------------------+
860         | 2.2 |producer_get_audio    |                       |                     |
861         |     |  Generates audio     |                       |                     |
862         +-----+----------------------+-----------------------+---------------------+
863         | 2.3 |                      |Receives audio         |                     |
864         |     |                      |Updates audio          |                     |
865         +-----+----------------------+-----------------------+---------------------+
866         | 2.4 |                      |                       |Receives audio       |
867         +-----+----------------------+-----------------------+---------------------+
868
869         And finally, when the consumer is done with the frame, it should close it.
870
871         Note that a consumer may not evaluate both image and audio for any given
872         frame, especially in a realtime environment. See 'Realtime Considerations'
873         below.
874
875         By default, a frame has the following properties:
876
877         +------------------+------------------------------------+------------------+
878         |Name              |Description                         |Values            |
879         +------------------+------------------------------------+------------------+
880         |_position         |The producers frame position        |0 to n            |
881         +------------------+------------------------------------+------------------+
882         |_speed            |The producers speed                 |double            |
883         +------------------+------------------------------------+------------------+
884         |image             |The generated image                 |NULL or pointer   |
885         +------------------+------------------------------------+------------------+
886         |alpha             |The generated alpha mask            |NULL or pointer   |
887         +------------------+------------------------------------+------------------+
888         |width             |The width of the image              |                  |
889         +------------------+------------------------------------+------------------+
890         |height            |The height of the image             |                  |
891         +------------------+------------------------------------+------------------+
892         |normalised_width  |The normalised width of the image   |720               |
893         +------------------+------------------------------------+------------------+
894         |normalised_height |The normalised height of the image  |576 or 480        |
895         +------------------+------------------------------------+------------------+
896         |progressive       |Indicates progressive/interlaced    |0 or 1            |
897         +------------------+------------------------------------+------------------+
898         |top_field_first   |Indicates top field first           |0 or 1            |
899         +------------------+------------------------------------+------------------+
900         |audio             |The generated audio                 |NULL or pointer   |
901         +------------------+------------------------------------+------------------+
902         |frequency         |The frequency of the audio          |                  |
903         +------------------+------------------------------------+------------------+
904         |channels          |The channels of the audio           |                  |
905         +------------------+------------------------------------+------------------+
906         |samples           |The samples of the audio            |                  |
907         +------------------+------------------------------------+------------------+
908         |aspect_ratio      |The aspect ratio of the image       |double            |
909         +------------------+------------------------------------+------------------+
910         |test_image        |Used to indicate no image available |0 or 1            |
911         +------------------+------------------------------------+------------------+
912         |test_audio        |Used to indicate no audio available |0 or 1            |
913         +------------------+------------------------------------+------------------+
914
915         The consumer can attach the following properties which affect the default
916         behaviour of a frame:
917
918         +------------------+------------------------------------+------------------+
919         |test_card_producer|Synthesise test images from here    |NULL or pointer   |
920         +------------------+------------------------------------+------------------+
921         |consumer_aspect_  |Apply this aspect ratio to the test |double            |
922         |ratio             |card producer                       |                  |
923         +------------------+------------------------------------+------------------+
924         |rescale.interp    |Use this scale method for test image|"string"          |
925         +------------------+------------------------------------+------------------+
926
927         While most of these are mainly self explainatory, the normalised_width and
928         normalised_height values require a little explaination. These are required
929         to ensure that effects are consistently handled as PAL or NTSC, regardless 
930         of the consumers or producers width/height image request. 
931
932         The test_image and audio flags are used to determine when images and audio
933         should be synthesised.
934
935         Additional properties may be provided by the producer implementation, and
936         filters, transitions and consumers may add additional properties to
937         communicate specific requests. These are documented in modules.txt.
938
939         The complete API for the mlt frame is as follows:
940
941         mlt_frame mlt_frame_init( );
942         mlt_properties mlt_frame_properties( mlt_frame this );
943         int mlt_frame_is_test_card( mlt_frame this );
944         int mlt_frame_is_test_audio( mlt_frame this );
945         double mlt_frame_get_aspect_ratio( mlt_frame this );
946         int mlt_frame_set_aspect_ratio( mlt_frame this, double value );
947         mlt_position mlt_frame_get_position( mlt_frame this );
948         int mlt_frame_set_position( mlt_frame this, mlt_position value );
949         int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable );
950         uint8_t *mlt_frame_get_alpha_mask( mlt_frame this );
951         int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples );
952         int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image );
953         mlt_get_image mlt_frame_pop_get_image( mlt_frame this );
954         int mlt_frame_push_frame( mlt_frame this, mlt_frame that );
955         mlt_frame mlt_frame_pop_frame( mlt_frame this );
956         int mlt_frame_push_service( mlt_frame this, void *that );
957         void *mlt_frame_pop_service( mlt_frame this );
958         int mlt_frame_push_audio( mlt_frame this, void *that );
959         void *mlt_frame_pop_audio( mlt_frame this );
960         void mlt_frame_close( mlt_frame this );
961
962         
963 mlt_service:
964
965         The service base class extends properties and allows 0 to m inputs and 0 to
966         n outputs and is represented as follows:
967
968             +-----------+
969         - ->|           |- ->
970         - ->|  Service  |- ->
971         - ->|           |
972             +-----------+
973             | properties|
974             +-----------+
975
976         Descendents of service impose restrictions on how inputs and outputs can be 
977         connected and will provide a basic set of properties. Typically, the service 
978         instance is encapsulated by the descendent in order for it to ensure that
979         its connection rules are followed.
980
981         A service does not define any properties when constructed. It should be
982         noted that producers, filters and transitions my be serialised (say, via the
983         westley consumer), and care should be taken to distinguish between
984         serialisable and transient properties. The convention used is to prefix
985         transient properties with an underscore.
986
987         The public interface is defined by the following functions:
988
989         int mlt_service_init( mlt_service this, void *child );
990         mlt_properties mlt_service_properties( mlt_service this );
991         int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index );
992         int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
993         void mlt_service_close( mlt_service this );
994
995         Typically, only direct descendents of services need invoke these methods and
996         developers are encouraged to use those extensions when definining new
997         services. 
998
999
1000 mlt_producer:
1001
1002         A producer has 0 inputs and 1 output:
1003
1004             +-----------+
1005             |           |
1006             | Producer  |--->
1007             |           |
1008             +-----------+
1009             | service   |
1010             +-----------+
1011
1012         A producer provides an abstraction for file readers, pipes, streams or any
1013         other image or audio input. 
1014
1015         When instantiated, a producer has the following properties:
1016
1017         +------------------+------------------------------------+------------------+
1018         |Name              |Description                         |Values            |
1019         +------------------+------------------------------------+------------------+
1020         |mlt_type          |The producers type                  |mlt_producer      |
1021         +------------------+------------------------------------+------------------+
1022         |_position         |The producers frame position        |0 to n            |
1023         +------------------+------------------------------------+------------------+
1024         |_speed            |The producers speed                 |double            |
1025         +------------------+------------------------------------+------------------+
1026         |fps               |The output frames per second        |25 or 29.97       |
1027         +------------------+------------------------------------+------------------+
1028         |in                |The in point in frames              |0 to length - 1   |
1029         +------------------+------------------------------------+------------------+
1030         |out               |The out point in frames             |in to length - 1  |
1031         +------------------+------------------------------------+------------------+
1032         |length            |The length of the input in frames   |0 to n            |
1033         +------------------+------------------------------------+------------------+
1034         |aspect_ratio      |aspect_ratio of the source          |0 to n            |
1035         +------------------+------------------------------------+------------------+
1036         |eof               |end of clip behaviour               |"pause" or "loop" |
1037         +------------------+------------------------------------+------------------+
1038         |resource          |Constructor argument (ie: file name)|"<resource>"      |
1039         +------------------+------------------------------------+------------------+
1040
1041         Additional properties may be provided by the producer implementation.
1042
1043         The public interface is defined by the following functions:
1044
1045         int mlt_producer_init( mlt_producer this, void *child );
1046         mlt_service mlt_producer_service( mlt_producer this );
1047         mlt_properties mlt_producer_properties( mlt_producer this );
1048         int mlt_producer_seek( mlt_producer this, mlt_position position );
1049         mlt_position mlt_producer_position( mlt_producer this );
1050         mlt_position mlt_producer_frame( mlt_producer this );
1051         int mlt_producer_set_speed( mlt_producer this, double speed );
1052         double mlt_producer_get_speed( mlt_producer this );
1053         double mlt_producer_get_fps( mlt_producer this );
1054         int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out );
1055         mlt_position mlt_producer_get_in( mlt_producer this );
1056         mlt_position mlt_producer_get_out( mlt_producer this );
1057         mlt_position mlt_producer_get_playtime( mlt_producer this );
1058         mlt_position mlt_producer_get_length( mlt_producer this );
1059         void mlt_producer_prepare_next( mlt_producer this );
1060         void mlt_producer_close( mlt_producer this );
1061
1062         For the sake of discussion here, we'll assume that someone wants to provide
1063         a new producer which random noise
1064
1065         // Forward reference
1066         static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index );
1067
1068         mlt_producer producer_green( void *arg )
1069         {
1070                 // Create a new producer
1071                 mlt_producer this = mlt_producer_new( );
1072
1073                 // Check that we were allocated a new producer
1074                 if ( this != NULL )
1075                 {
1076                         // Get the service 
1077                         mlt_service service = mlt_producer_service( this );
1078
1079                         // We need to override the get_frame method
1080                         service->get_frame = producer_get_frame;
1081                 }
1082
1083                 // Return this producer
1084                 return this;
1085         }
1086
1087         static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
1088         {
1089                 // Create a new frame
1090                 *frame = mlt_frame_init( );
1091
1092                 // Specify the get_image
1093                 mlt_frame_push_get_image( *frame, producer_get_image );
1094
1095                 // Specify the get_audio
1096                 mlt_frame_push_audio( *frame, producer_get_audio );
1097
1098                 // Return that all was successful
1099                 return 0;
1100         }
1101
1102 mlt_filter:
1103
1104         The public interface is defined by the following functions:
1105
1106         int mlt_filter_init( mlt_filter this, void *child );
1107         mlt_filter mlt_filter_new( );
1108         mlt_service mlt_filter_service( mlt_filter this );
1109         mlt_properties mlt_filter_properties( mlt_filter this );
1110         mlt_frame mlt_filter_process( mlt_filter this, mlt_frame that );
1111         int mlt_filter_connect( mlt_filter this, mlt_service producer, int index );
1112         void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out );
1113         int mlt_filter_get_track( mlt_filter this );
1114         mlt_position mlt_filter_get_in( mlt_filter this );
1115         mlt_position mlt_filter_get_out( mlt_filter this );
1116         void mlt_filter_close( mlt_filter );
1117
1118
1119 mlt_transition:
1120
1121         The public interface is defined by the following functions:
1122
1123         int mlt_transition_init( mlt_transition this, void *child );
1124         mlt_transition mlt_transition_new( );
1125         mlt_service mlt_transition_service( mlt_transition this );
1126         mlt_properties mlt_transition_properties( mlt_transition this );
1127         int mlt_transition_connect( mlt_transition this, mlt_service producer, int a_track, int b_track );
1128         void mlt_transition_set_in_and_out( mlt_transition this, mlt_position in, mlt_position out );
1129         int mlt_transition_get_a_track( mlt_transition this );
1130         int mlt_transition_get_b_track( mlt_transition this );
1131         mlt_position mlt_transition_get_in( mlt_transition this );
1132         mlt_position mlt_transition_get_out( mlt_transition this );
1133         mlt_frame mlt_transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame );
1134         void mlt_transition_close( mlt_transition this );
1135
1136
1137 mlt_consumer:
1138
1139         The public interface is defined by the following functions:
1140
1141         int mlt_consumer_init( mlt_consumer this, void *child );
1142         mlt_service mlt_consumer_service( mlt_consumer this );
1143         mlt_properties mlt_consumer_properties( mlt_consumer this );
1144         int mlt_consumer_connect( mlt_consumer this, mlt_service producer );
1145         int mlt_consumer_start( mlt_consumer this );
1146         mlt_frame mlt_consumer_get_frame( mlt_consumer this );
1147         mlt_frame mlt_consumer_rt_frame( mlt_consumer this );
1148         int mlt_consumer_stop( mlt_consumer this );
1149         int mlt_consumer_is_stopped( mlt_consumer this );
1150         void mlt_consumer_close( mlt_consumer );
1151
1152
1153 Specialised Producers:
1154
1155         There are two major types of specialised producers - playlists and tractors.
1156
1157         The following sections describe these.
1158
1159
1160 mlt_playlist:
1161
1162         mlt_playlist mlt_playlist_init( );
1163         mlt_producer mlt_playlist_producer( mlt_playlist this );
1164         mlt_service mlt_playlist_service( mlt_playlist this );
1165         mlt_properties mlt_playlist_properties( mlt_playlist this );
1166         int mlt_playlist_count( mlt_playlist this );
1167         int mlt_playlist_clear( mlt_playlist this );
1168         int mlt_playlist_append( mlt_playlist this, mlt_producer producer );
1169         int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out );
1170         int mlt_playlist_blank( mlt_playlist this, mlt_position length );
1171         mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index );
1172         int mlt_playlist_current_clip( mlt_playlist this );
1173         mlt_producer mlt_playlist_current( mlt_playlist this );
1174         int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index );
1175         int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out );
1176         int mlt_playlist_remove( mlt_playlist this, int where );
1177         int mlt_playlist_move( mlt_playlist this, int from, int to );
1178         int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out );
1179         void mlt_playlist_close( mlt_playlist this );
1180
1181 mlt_tractor:
1182
1183
1184 mlt_factory
1185