]> git.sesse.net Git - mlt/blob - src/framework/mlt_tractor.c
Attempt at an aspect ratio clean up
[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 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this );
46
47 /** Constructor for the tractor.
48 */
49
50 mlt_tractor mlt_tractor_init( )
51 {
52         mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
53         if ( this != NULL )
54         {
55                 mlt_producer producer = &this->parent;
56                 if ( mlt_producer_init( producer, this ) == 0 )
57                 {
58                         mlt_properties_set( mlt_producer_properties( producer ), "resource", "<tractor>" );
59                         mlt_properties_set( mlt_producer_properties( producer ), "mlt_type", "mlt_producer" );
60                         mlt_properties_set( mlt_producer_properties( producer ), "mlt_service", "tractor" );
61
62                         producer->get_frame = producer_get_frame;
63                         producer->close = ( mlt_destructor )mlt_tractor_close;
64                         producer->close_object = this;
65                 }
66                 else
67                 {
68                         free( this );
69                         this = NULL;
70                 }
71         }
72         return this;
73 }
74
75 mlt_tractor mlt_tractor_new( )
76 {
77         mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
78         if ( this != NULL )
79         {
80                 mlt_producer producer = &this->parent;
81                 if ( mlt_producer_init( producer, this ) == 0 )
82                 {
83                         mlt_multitrack multitrack = mlt_multitrack_init( );
84                         mlt_field field = mlt_field_new( multitrack, this );
85                         mlt_properties props = mlt_producer_properties( producer );
86
87                         mlt_properties_set( props, "resource", "<tractor>" );
88                         mlt_properties_set( props, "mlt_type", "mlt_producer" );
89                         mlt_properties_set( props, "mlt_service", "tractor" );
90                         mlt_properties_set_position( props, "in", 0 );
91                         mlt_properties_set_position( props, "out", 0 );
92                         mlt_properties_set_position( props, "length", 0 );
93                         mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
94                         mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
95
96                         mlt_events_listen( mlt_multitrack_properties( multitrack ), this, "producer-changed", ( mlt_listener )mlt_tractor_listener );
97
98                         producer->get_frame = producer_get_frame;
99                         producer->close = ( mlt_destructor )mlt_tractor_close;
100                         producer->close_object = this;
101                 }
102                 else
103                 {
104                         free( this );
105                         this = NULL;
106                 }
107         }
108         return this;
109 }
110
111 /** Get the service object associated to the tractor.
112 */
113
114 mlt_service mlt_tractor_service( mlt_tractor this )
115 {
116         return mlt_producer_service( &this->parent );
117 }
118
119 /** Get the producer object associated to the tractor.
120 */
121
122 mlt_producer mlt_tractor_producer( mlt_tractor this )
123 {
124         return this != NULL ? &this->parent : NULL;
125 }
126
127 /** Get the properties object associated to the tractor.
128 */
129
130 mlt_properties mlt_tractor_properties( mlt_tractor this )
131 {
132         return mlt_producer_properties( &this->parent );
133 }
134
135 /** Get the field this tractor is harvesting.
136 */
137
138 mlt_field mlt_tractor_field( mlt_tractor this )
139 {
140         return mlt_properties_get_data( mlt_tractor_properties( this ), "field", NULL );
141 }
142
143 /** Get the multitrack this tractor is pulling.
144 */
145
146 mlt_multitrack mlt_tractor_multitrack( mlt_tractor this )
147 {
148         return mlt_properties_get_data( mlt_tractor_properties( this ), "multitrack", NULL );
149 }
150
151 /** Ensure the tractors in/out points match the multitrack.
152 */
153
154 void mlt_tractor_refresh( mlt_tractor this )
155 {
156         mlt_multitrack multitrack = mlt_tractor_multitrack( this );
157         mlt_properties properties = mlt_multitrack_properties( multitrack );
158         mlt_properties self = mlt_tractor_properties( this );
159         mlt_events_block( properties, self );
160         mlt_events_block( self, self );
161         mlt_multitrack_refresh( multitrack );
162         mlt_properties_set_position( self, "in", 0 );
163         mlt_properties_set_position( self, "out", mlt_properties_get_position( properties, "out" ) );
164         mlt_events_unblock( self, self );
165         mlt_events_unblock( properties, self );
166         mlt_properties_set_position( self, "length", mlt_properties_get_position( properties, "length" ) );
167 }
168
169 static void mlt_tractor_listener( mlt_multitrack tracks, mlt_tractor this )
170 {
171         mlt_tractor_refresh( this );
172 }
173
174 /** Connect the tractor.
175 */
176
177 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
178 {
179         int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
180
181         // This is the producer we're going to connect to
182         if ( ret == 0 )
183                 this->producer = producer;
184
185         return ret;
186 }
187
188 /** Set the producer for a specific track.
189 */
190
191 int mlt_tractor_set_track( mlt_tractor this, mlt_producer producer, int index )
192 {
193         return mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer, index );
194 }
195
196 /** Get the producer for a specific track.
197 */
198
199 mlt_producer mlt_tractor_get_track( mlt_tractor this, int index )
200 {
201         return mlt_multitrack_track( mlt_tractor_multitrack( this ), index );
202 }
203
204 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
205 {
206         uint8_t *data = NULL;
207         mlt_properties properties = mlt_frame_properties( this );
208         mlt_frame frame = mlt_frame_pop_service( this );
209         mlt_properties frame_properties = mlt_frame_properties( frame );
210         mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( properties, "width" ) );
211         mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( properties, "height" ) );
212         mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
213         if ( mlt_properties_get( properties, "distort" ) )
214                 mlt_properties_set( frame_properties, "distort", mlt_properties_get( properties, "distort" ) );
215         mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
216         mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_double( properties, "consumer_deinterlace" ) );
217         mlt_properties_set_int( frame_properties, "normalised_width", mlt_properties_get_double( properties, "normalised_width" ) );
218         mlt_properties_set_int( frame_properties, "normalised_height", mlt_properties_get_double( properties, "normalised_height" ) );
219         mlt_frame_get_image( frame, buffer, format, width, height, writable );
220         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
221         mlt_properties_set_int( properties, "width", *width );
222         mlt_properties_set_int( properties, "height", *height );
223         mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( frame ) );
224         mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
225         if ( mlt_properties_get( frame_properties, "distort" ) )
226                 mlt_properties_set( properties, "distort", mlt_properties_get( frame_properties, "distort" ) );
227         data = mlt_frame_get_alpha_mask( frame );
228         mlt_properties_set_data( properties, "alpha", data, 0, NULL, NULL );
229         return 0;
230 }
231
232 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
233 {
234         mlt_properties properties = mlt_frame_properties( this );
235         mlt_frame frame = mlt_frame_pop_audio( this );
236         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
237         mlt_properties_set_data( properties, "audio", *buffer, 0, NULL, NULL );
238         mlt_properties_set_int( properties, "frequency", *frequency );
239         mlt_properties_set_int( properties, "channels", *channels );
240         return 0;
241 }
242
243 /** Get the next frame.
244 */
245
246 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
247 {
248         mlt_tractor this = parent->child;
249
250         // We only respond to the first track requests
251         if ( track == 0 && this->producer != NULL )
252         {
253                 int i = 0;
254                 int done = 0;
255                 mlt_frame temp = NULL;
256                 int count = 0;
257
258                 // Get the properties of the parent producer
259                 mlt_properties properties = mlt_producer_properties( parent );
260
261                 // Try to obtain the multitrack associated to the tractor
262                 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
263
264                 // Or a specific producer
265                 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
266
267                 // If we don't have one, we're in trouble... 
268                 if ( multitrack != NULL )
269                 {
270                         // Used to garbage collect all frames
271                         char label[ 30 ];
272
273                         // Get the id of the tractor
274                         char *id = mlt_properties_get( properties, "_unique_id" );
275
276                         // Will be used to store the frame properties object
277                         mlt_properties frame_properties = NULL;
278
279                         // We'll store audio and video frames to use here
280                         mlt_frame audio = NULL;
281                         mlt_frame video = NULL;
282
283                         // Determine which data_queue to pass on...
284                         void *data_queue = NULL;
285
286                         // Temporary properties
287                         mlt_properties temp_properties = NULL;
288
289                         // Get the multitrack's producer
290                         mlt_producer target = mlt_multitrack_producer( multitrack );
291                         mlt_producer_seek( target, mlt_producer_frame( parent ) );
292                         mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
293
294                         // We will create one frame and attach everything to it
295                         *frame = mlt_frame_init( );
296
297                         // Get the properties of the frame
298                         frame_properties = mlt_frame_properties( *frame );
299
300                         // Loop through each of the tracks we're harvesting
301                         for ( i = 0; !done; i ++ )
302                         {
303                                 // Get a frame from the producer
304                                 mlt_service_get_frame( this->producer, &temp, i );
305
306                                 // Get the temporary properties
307                                 temp_properties = mlt_frame_properties( temp );
308
309                                 // Check for last track
310                                 done = mlt_properties_get_int( temp_properties, "last_track" );
311
312                                 // We store all frames with a destructor on the output frame
313                                 sprintf( label, "_%s_%d", id, count ++ );
314                                 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
315
316                                 // We want the last data_queue 
317                                 if ( !done && mlt_properties_get_data( temp_properties, "data_queue", NULL ) != NULL )
318                                         data_queue = mlt_properties_get_data( mlt_frame_properties( temp ), "data_queue", NULL );
319
320                                 // Pick up first video and audio frames
321                                 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 2 ) )
322                                         audio = temp;
323                                 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( temp_properties, "hide" ) & 1 ) )
324                                         video = temp;
325                         }
326         
327                         // Now stack callbacks
328                         if ( audio != NULL )
329                         {
330                                 mlt_frame_push_audio( *frame, audio );
331                                 ( *frame )->get_audio = producer_get_audio;
332                         }
333
334                         if ( video != NULL )
335                         {
336                                 mlt_properties video_properties = mlt_frame_properties( video );
337                                 mlt_frame_push_service( *frame, video );
338                                 mlt_frame_push_service( *frame, producer_get_image );
339                                 mlt_properties_set_data( frame_properties, "data_queue", data_queue, 0, NULL, NULL );
340                                 mlt_properties_set_int( frame_properties, "width", mlt_properties_get_int( video_properties, "width" ) );
341                                 mlt_properties_set_int( frame_properties, "height", mlt_properties_get_int( video_properties, "height" ) );
342                                 mlt_properties_set_int( frame_properties, "real_width", mlt_properties_get_int( video_properties, "real_width" ) );
343                                 mlt_properties_set_int( frame_properties, "real_height", mlt_properties_get_int( video_properties, "real_height" ) );
344                                 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( video_properties, "progressive" ) );
345                                 mlt_properties_set_double( frame_properties, "aspect_ratio", mlt_properties_get_double( video_properties, "aspect_ratio" ) );
346                         }
347
348                         mlt_frame_set_position( *frame, mlt_producer_frame( parent ) );
349                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", audio == NULL );
350                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", video == NULL );
351                 }
352                 else if ( producer != NULL )
353                 {
354                         mlt_producer_seek( producer, mlt_producer_frame( parent ) );
355                         mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
356                         mlt_service_get_frame( this->producer, frame, track );
357                 }
358                 else
359                 {
360                         fprintf( stderr, "tractor without a multitrack!!\n" );
361                         mlt_service_get_frame( this->producer, frame, track );
362                 }
363
364                 // Prepare the next frame
365                 mlt_producer_prepare_next( parent );
366
367                 // Indicate our found status
368                 return 0;
369         }
370         else
371         {
372                 // Generate a test card
373                 *frame = mlt_frame_init( );
374                 return 0;
375         }
376 }
377
378 /** Close the tractor.
379 */
380
381 void mlt_tractor_close( mlt_tractor this )
382 {
383         if ( this != NULL && mlt_properties_dec_ref( mlt_tractor_properties( this ) ) <= 0 )
384         {
385                 this->parent.close = NULL;
386                 mlt_producer_close( &this->parent );
387                 free( this );
388         }
389 }
390