Tractor frame handling reworked; fix to composite for key diffs of 1; added mlt_consu...
[melted] / src / framework / mlt_consumer.c
1 /*
2 * mlt_consumer.c -- abstraction for all consumer services
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 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30
31 /** Public final methods
32 */
33
34 int mlt_consumer_init( mlt_consumer this, void *child )
35 {
36 int error = 0;
37 memset( this, 0, sizeof( struct mlt_consumer_s ) );
38 this->child = child;
39 error = mlt_service_init( &this->parent, this );
40 if ( error == 0 )
41 {
42 // Get the properties from the service
43 mlt_properties properties = mlt_service_properties( &this->parent );
44
45 // Get the normalisation preference
46 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
47
48 // Deal with normalisation
49 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
50 {
51 mlt_properties_set( properties, "normalisation", "PAL" );
52 mlt_properties_set_double( properties, "fps", 25.0 );
53 mlt_properties_set_int( properties, "width", 720 );
54 mlt_properties_set_int( properties, "height", 576 );
55 mlt_properties_set_int( properties, "progressive", 0 );
56 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
57 }
58 else
59 {
60 mlt_properties_set( properties, "normalisation", "NTSC" );
61 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
62 mlt_properties_set_int( properties, "width", 720 );
63 mlt_properties_set_int( properties, "height", 480 );
64 mlt_properties_set_int( properties, "progressive", 0 );
65 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
66 }
67
68 // Default rescaler for all consumers
69 mlt_properties_set( properties, "rescale", "bilinear" );
70
71 // Default read ahead buffer size
72 mlt_properties_set_int( properties, "buffer", 25 );
73
74 // Hmm - default all consumers to yuv422 :-/
75 this->format = mlt_image_yuv422;
76 }
77 return error;
78 }
79
80 /** Create a new consumer.
81 */
82
83 mlt_consumer mlt_consumer_new( )
84 {
85 // Create the memory for the structure
86 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
87
88 // Initialise it
89 if ( this != NULL )
90 mlt_consumer_init( this, NULL );
91
92 // Return it
93 return this;
94 }
95
96 /** Get the parent service object.
97 */
98
99 mlt_service mlt_consumer_service( mlt_consumer this )
100 {
101 return &this->parent;
102 }
103
104 /** Get the consumer properties.
105 */
106
107 mlt_properties mlt_consumer_properties( mlt_consumer this )
108 {
109 return mlt_service_properties( &this->parent );
110 }
111
112 /** Connect the consumer to the producer.
113 */
114
115 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
116 {
117 return mlt_service_connect_producer( &this->parent, producer, 0 );
118 }
119
120 /** Start the consumer.
121 */
122
123 int mlt_consumer_start( mlt_consumer this )
124 {
125 // Get the properies
126 mlt_properties properties = mlt_consumer_properties( this );
127
128 // Determine if there's a test card producer
129 char *test_card = mlt_properties_get( properties, "test_card" );
130
131 // Deal with it now.
132 if ( test_card != NULL )
133 {
134 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
135 {
136 // Create a test card producer
137 // TODO: do we want to use fezzik here?
138 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
139
140 // Do we have a producer
141 if ( producer != NULL )
142 {
143 // Test card should loop I guess...
144 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
145
146 // Set the test card on the consumer
147 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
148 }
149
150 // Check and run an ante command
151 if ( mlt_properties_get( properties, "ante" ) )
152 system( mlt_properties_get( properties, "ante" ) );
153 }
154 }
155 else
156 {
157 // Allow the hash table to speed things up
158 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
159 }
160
161 // Start the service
162 if ( this->start != NULL )
163 return this->start( this );
164
165 return 0;
166 }
167
168 /** Protected method :-/ for consumer to get frames from connected service
169 */
170
171 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
172 {
173 // Frame to return
174 mlt_frame frame = NULL;
175
176 // Get the service assoicated to the consumer
177 mlt_service service = mlt_consumer_service( this );
178
179 // Get the frame
180 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
181 {
182 // Get the consumer properties
183 mlt_properties properties = mlt_consumer_properties( this );
184
185 // Get the frame properties
186 mlt_properties frame_properties = mlt_frame_properties( frame );
187
188 // Get the test card producer
189 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
190
191 // Attach the test frame producer to it.
192 if ( test_card != NULL )
193 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
194
195 // Attach the rescale property
196 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
197
198 // Aspect ratio and other jiggery pokery
199 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
200 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
201 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
202 }
203
204 // Return the frame
205 return frame;
206 }
207
208 static inline long time_difference( struct timeval *time1 )
209 {
210 struct timeval time2;
211 time2.tv_sec = time1->tv_sec;
212 time2.tv_usec = time1->tv_usec;
213 gettimeofday( time1, NULL );
214 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
215 }
216
217 static void *consumer_read_ahead_thread( void *arg )
218 {
219 // The argument is the consumer
220 mlt_consumer this = arg;
221
222 // Get the properties of the consumer
223 mlt_properties properties = mlt_consumer_properties( this );
224
225 // Get the width and height
226 int width = mlt_properties_get_int( properties, "width" );
227 int height = mlt_properties_get_int( properties, "height" );
228
229 // Get the maximum size of the buffer
230 int buffer = mlt_properties_get_int( properties, "buffer" );
231
232 // General frame variable
233 mlt_frame frame = NULL;
234 uint8_t *image = NULL;
235
236 // Time structures
237 struct timeval ante;
238
239 // Average time for get_frame and get_image
240 int count = 1;
241 int skipped = 0;
242 int64_t time_wait = 0;
243 int64_t time_frame = 0;
244 int64_t time_image = 0;
245
246 // Get the first frame
247 frame = mlt_consumer_get_frame( this );
248
249 // Get the image of the first frame
250 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
251 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
252
253 // Get the starting time (can ignore the times above)
254 gettimeofday( &ante, NULL );
255
256 // Continue to read ahead
257 while ( this->ahead )
258 {
259 // Put the current frame into the queue
260 pthread_mutex_lock( &this->mutex );
261 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
262 pthread_cond_wait( &this->cond, &this->mutex );
263 mlt_deque_push_back( this->queue, frame );
264 pthread_cond_broadcast( &this->cond );
265 pthread_mutex_unlock( &this->mutex );
266 time_wait += time_difference( &ante );
267
268 // Get the next frame
269 frame = mlt_consumer_get_frame( this );
270 time_frame += time_difference( &ante );
271
272 // Increment the count
273 count ++;
274
275 // Get the image
276 if ( ( time_frame + time_image ) / count < ( 40000 - ( time_wait / count ) ) )
277 {
278 // Get the image, mark as rendered and time it
279 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
280 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
281 time_image += time_difference( &ante );
282
283 // Reset the skipped count
284 skipped = 0;
285 }
286 else
287 {
288 // Increment the number of sequentially skipped frames
289 skipped ++;
290
291 // If we've reached an unacceptable level, reset everything
292 if ( skipped > 10 )
293 {
294 skipped = 0;
295 time_frame = 0;
296 time_image = 0;
297 time_wait = 0;
298 count = 0;
299 }
300 }
301 }
302
303 // Remove the last frame
304 mlt_frame_close( frame );
305
306 return NULL;
307 }
308
309 static void consumer_read_ahead_start( mlt_consumer this )
310 {
311 // We're running now
312 this->ahead = 1;
313
314 // Create the frame queue
315 this->queue = mlt_deque_init( );
316
317 // Create the mutex
318 pthread_mutex_init( &this->mutex, NULL );
319
320 // Create the condition
321 pthread_cond_init( &this->cond, NULL );
322
323 // Create the read ahead
324 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
325
326 }
327
328 static void consumer_read_ahead_stop( mlt_consumer this )
329 {
330 // Make sure we're running
331 if ( this->ahead )
332 {
333 // Inform thread to stop
334 this->ahead = 0;
335
336 // Broadcast to the condition in case it's waiting
337 pthread_mutex_lock( &this->mutex );
338 pthread_cond_broadcast( &this->cond );
339 pthread_mutex_unlock( &this->mutex );
340
341 // Join the thread
342 pthread_join( this->ahead_thread, NULL );
343
344 // Destroy the mutex
345 pthread_mutex_destroy( &this->mutex );
346
347 // Destroy the condition
348 pthread_cond_destroy( &this->cond );
349
350 // Wipe the queue
351 while ( mlt_deque_count( this->queue ) )
352 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
353 }
354 }
355
356 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
357 {
358 // Frame to return
359 mlt_frame frame = NULL;
360 int size = 1;
361
362 // Is the read ahead running?
363 if ( this->ahead == 0 )
364 {
365 int buffer = mlt_properties_get_int( mlt_consumer_properties( this ), "buffer" );
366 consumer_read_ahead_start( this );
367 if ( buffer > 1 )
368 size = buffer / 2;
369 }
370
371 // Get frame from queue
372 pthread_mutex_lock( &this->mutex );
373 while( this->ahead && mlt_deque_count( this->queue ) < size )
374 pthread_cond_wait( &this->cond, &this->mutex );
375 frame = mlt_deque_pop_front( this->queue );
376 pthread_cond_broadcast( &this->cond );
377 pthread_mutex_unlock( &this->mutex );
378
379 return frame;
380 }
381
382 /** Stop the consumer.
383 */
384
385 int mlt_consumer_stop( mlt_consumer this )
386 {
387 // Get the properies
388 mlt_properties properties = mlt_consumer_properties( this );
389
390 // Stop the consumer
391 if ( this->stop != NULL )
392 this->stop( this );
393
394 // Kill the read ahead
395 consumer_read_ahead_stop( this );
396
397 // Kill the test card
398 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
399
400 // Check and run a post command
401 if ( mlt_properties_get( properties, "post" ) )
402 system( mlt_properties_get( properties, "post" ) );
403
404 return 0;
405 }
406
407 /** Determine if the consumer is stopped.
408 */
409
410 int mlt_consumer_is_stopped( mlt_consumer this )
411 {
412 // Check if the consumer is stopped
413 if ( this->is_stopped != NULL )
414 return this->is_stopped( this );
415
416 return 0;
417 }
418
419 /** Close the consumer.
420 */
421
422 void mlt_consumer_close( mlt_consumer this )
423 {
424 // Get the childs close function
425 void ( *consumer_close )( ) = this->close;
426
427 // Make sure it only gets called once
428 this->close = NULL;
429
430 // Call the childs close if available
431 if ( consumer_close != NULL )
432 consumer_close( this );
433 else
434 mlt_service_close( &this->parent );
435 }