]> git.sesse.net Git - mlt/blob - src/framework/mlt_tractor.c
New tractor constructor
[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
32 /** Private structure.
33 */
34
35 struct mlt_tractor_s
36 {
37         struct mlt_producer_s parent;
38         mlt_service producer;
39 };
40
41 /** Forward references to static methods.
42 */
43
44 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
45
46 /** Constructor for the tractor.
47 */
48
49 mlt_tractor mlt_tractor_init( )
50 {
51         mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
52         if ( this != NULL )
53         {
54                 mlt_producer producer = &this->parent;
55                 if ( mlt_producer_init( producer, this ) == 0 )
56                 {
57                         mlt_properties_set( mlt_producer_properties( producer ), "resource", "<tractor>" );
58                         mlt_properties_set( mlt_producer_properties( producer ), "mlt_type", "mlt_producer" );
59                         mlt_properties_set( mlt_producer_properties( producer ), "mlt_service", "tractor" );
60
61                         producer->get_frame = producer_get_frame;
62                         producer->close = ( mlt_destructor )mlt_tractor_close;
63                         producer->close_object = this;
64                 }
65                 else
66                 {
67                         free( this );
68                         this = NULL;
69                 }
70         }
71         return this;
72 }
73
74 mlt_tractor mlt_tractor_new( )
75 {
76         mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
77         if ( this != NULL )
78         {
79                 mlt_producer producer = &this->parent;
80                 if ( mlt_producer_init( producer, this ) == 0 )
81                 {
82                         mlt_multitrack multitrack = mlt_multitrack_init( );
83                         mlt_field field = mlt_field_new( multitrack, this );
84                         mlt_properties props = mlt_producer_properties( producer );
85
86                         mlt_properties_set( props, "resource", "<tractor>" );
87                         mlt_properties_set( props, "mlt_type", "mlt_producer" );
88                         mlt_properties_set( props, "mlt_service", "tractor" );
89                         mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
90                         mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
91
92                         producer->get_frame = producer_get_frame;
93                         producer->close = ( mlt_destructor )mlt_tractor_close;
94                         producer->close_object = this;
95                 }
96                 else
97                 {
98                         free( this );
99                         this = NULL;
100                 }
101         }
102         return this;
103 }
104
105 /** Get the service object associated to the tractor.
106 */
107
108 mlt_service mlt_tractor_service( mlt_tractor this )
109 {
110         return mlt_producer_service( &this->parent );
111 }
112
113 /** Get the producer object associated to the tractor.
114 */
115
116 mlt_producer mlt_tractor_producer( mlt_tractor this )
117 {
118         return this != NULL ? &this->parent : NULL;
119 }
120
121 /** Get the properties object associated to the tractor.
122 */
123
124 mlt_properties mlt_tractor_properties( mlt_tractor this )
125 {
126         return mlt_producer_properties( &this->parent );
127 }
128
129 /** Get the field this tractor is harvesting.
130 */
131
132 mlt_field mlt_tractor_field( mlt_tractor this )
133 {
134         return mlt_properties_get_data( mlt_tractor_properties( this ), "field", NULL );
135 }
136
137 /** Get the multitrack this tractor is pulling.
138 */
139
140 mlt_multitrack mlt_tractor_multitrack( mlt_tractor this )
141 {
142         return mlt_properties_get_data( mlt_tractor_properties( this ), "multitrack", NULL );
143 }
144
145 /** Connect the tractor.
146 */
147
148 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
149 {
150         int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
151
152         // This is the producer we're going to connect to
153         if ( ret == 0 )
154                 this->producer = producer;
155
156         return ret;
157 }
158
159 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
160 {
161         mlt_properties properties = mlt_frame_properties( this );
162         mlt_frame frame = mlt_frame_pop_service( this );
163         mlt_properties_inherit( mlt_frame_properties( frame ), properties );
164         mlt_frame_get_image( frame, buffer, format, width, height, writable );
165         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
166         mlt_properties_set_int( properties, "width", *width );
167         mlt_properties_set_int( properties, "height", *height );
168         return 0;
169 }
170
171 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
172 {
173         mlt_properties properties = mlt_frame_properties( this );
174         mlt_frame frame = mlt_frame_pop_audio( this );
175         mlt_properties_inherit( mlt_frame_properties( frame ), properties );
176         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
177         mlt_properties_set_data( properties, "audio", *buffer, 0, NULL, NULL );
178         mlt_properties_set_int( properties, "frequency", *frequency );
179         mlt_properties_set_int( properties, "channels", *channels );
180         return 0;
181 }
182
183 /** Get the next frame.
184 */
185
186 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
187 {
188         mlt_tractor this = parent->child;
189
190         // We only respond to the first track requests
191         if ( track == 0 && this->producer != NULL )
192         {
193                 int i = 0;
194                 int done = 0;
195                 mlt_frame temp = NULL;
196                 int count = 0;
197
198                 // Get the properties of the parent producer
199                 mlt_properties properties = mlt_producer_properties( parent );
200
201                 // Try to obtain the multitrack associated to the tractor
202                 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
203
204                 // Or a specific producer
205                 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
206
207                 // If we don't have one, we're in trouble... 
208                 if ( multitrack != NULL )
209                 {
210                         // Used to garbage collect all frames
211                         char label[ 30 ];
212
213                         // Get the id of the tractor
214                         char *id = mlt_properties_get( properties, "_unique_id" );
215
216                         // Will be used to store the frame properties object
217                         mlt_properties frame_properties = NULL;
218
219                         // We'll store audio and video frames to use here
220                         mlt_frame audio = NULL;
221                         mlt_frame video = NULL;
222
223                         // Get the multitrack's producer
224                         mlt_producer target = mlt_multitrack_producer( multitrack );
225                         mlt_producer_seek( target, mlt_producer_frame( parent ) );
226                         mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
227
228                         // We will create one frame and attach everything to it
229                         *frame = mlt_frame_init( );
230
231                         // Get the properties of the frame
232                         frame_properties = mlt_frame_properties( *frame );
233
234                         // Loop through each of the tracks we're harvesting
235                         for ( i = 0; !done; i ++ )
236                         {
237                                 // Get a frame from the producer
238                                 mlt_service_get_frame( this->producer, &temp, i );
239
240                                 // Check for last track
241                                 done = mlt_properties_get_int( mlt_frame_properties( temp ), "last_track" );
242
243                                 // We store all frames with a destructor on the output frame
244                                 sprintf( label, "_%s_%d", id, count ++ );
245                                 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
246
247                                 // Pick up first video and audio frames
248                                 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( mlt_frame_properties( temp ), "hide" ) & 2 ) )
249                                         audio = temp;
250                                 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( mlt_frame_properties( temp ), "hide" ) & 1 ) )
251                                         video = temp;
252                         }
253         
254                         // Now stack callbacks
255                         if ( audio != NULL )
256                         {
257                                 mlt_frame_push_audio( *frame, audio );
258                                 ( *frame )->get_audio = producer_get_audio;
259                                 mlt_properties_inherit( mlt_frame_properties( *frame ), mlt_frame_properties( audio ) );
260                         }
261
262                         if ( video != NULL )
263                         {
264                                 mlt_frame_push_service( *frame, video );
265                                 mlt_frame_push_service( *frame, producer_get_image );
266                                 mlt_properties_inherit( mlt_frame_properties( *frame ), mlt_frame_properties( video ) );
267                         }
268
269                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", audio == NULL );
270                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", video == NULL );
271                 }
272                 else if ( producer != NULL )
273                 {
274                         mlt_producer_seek( producer, mlt_producer_frame( parent ) );
275                         mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
276                         mlt_service_get_frame( this->producer, frame, track );
277                 }
278                 else
279                 {
280                         fprintf( stderr, "tractor without a multitrack!!\n" );
281                         mlt_service_get_frame( this->producer, frame, track );
282                 }
283
284                 // Prepare the next frame
285                 mlt_producer_prepare_next( parent );
286
287                 // Indicate our found status
288                 return 0;
289         }
290         else
291         {
292                 // Generate a test card
293                 *frame = mlt_frame_init( );
294                 return 0;
295         }
296 }
297
298 /** Close the tractor.
299 */
300
301 void mlt_tractor_close( mlt_tractor this )
302 {
303         if ( this != NULL && mlt_properties_dec_ref( mlt_tractor_properties( this ) ) <= 0 )
304         {
305                 this->parent.close = NULL;
306                 mlt_producer_close( &this->parent );
307                 free( this );
308         }
309 }
310