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