Oops - fix for consumer progressive
[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 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
32
33 /** Public final methods
34 */
35
36 int mlt_consumer_init( mlt_consumer this, void *child )
37 {
38 int error = 0;
39 memset( this, 0, sizeof( struct mlt_consumer_s ) );
40 this->child = child;
41 error = mlt_service_init( &this->parent, this );
42 if ( error == 0 )
43 {
44 // Get the properties from the service
45 mlt_properties properties = mlt_service_properties( &this->parent );
46
47 // Get the normalisation preference
48 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
49
50 // Deal with normalisation
51 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
52 {
53 mlt_properties_set( properties, "normalisation", "PAL" );
54 mlt_properties_set_double( properties, "fps", 25.0 );
55 mlt_properties_set_int( properties, "width", 720 );
56 mlt_properties_set_int( properties, "height", 576 );
57 mlt_properties_set_int( properties, "progressive", 0 );
58 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
59 }
60 else
61 {
62 mlt_properties_set( properties, "normalisation", "NTSC" );
63 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
64 mlt_properties_set_int( properties, "width", 720 );
65 mlt_properties_set_int( properties, "height", 480 );
66 mlt_properties_set_int( properties, "progressive", 0 );
67 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
68 }
69
70 // Default rescaler for all consumers
71 mlt_properties_set( properties, "rescale", "bilinear" );
72
73 // Default read ahead buffer size
74 mlt_properties_set_int( properties, "buffer", 25 );
75
76 // Default audio frequency and channels
77 mlt_properties_set_int( properties, "frequency", 48000 );
78 mlt_properties_set_int( properties, "channels", 2 );
79
80 // Default of all consumers is real time
81 mlt_properties_set_int( properties, "real_time", 1 );
82
83 // Default to environment test card
84 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
85
86 // Hmm - default all consumers to yuv422 :-/
87 this->format = mlt_image_yuv422;
88
89 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
90 mlt_events_register( properties, "consumer-stopped", NULL );
91 }
92 return error;
93 }
94
95 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
96 {
97 if ( listener != NULL )
98 listener( owner, this, ( mlt_frame )args[ 0 ] );
99 }
100
101 /** Create a new consumer.
102 */
103
104 mlt_consumer mlt_consumer_new( )
105 {
106 // Create the memory for the structure
107 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
108
109 // Initialise it
110 if ( this != NULL )
111 mlt_consumer_init( this, NULL );
112
113 // Return it
114 return this;
115 }
116
117 /** Get the parent service object.
118 */
119
120 mlt_service mlt_consumer_service( mlt_consumer this )
121 {
122 return this != NULL ? &this->parent : NULL;
123 }
124
125 /** Get the consumer properties.
126 */
127
128 mlt_properties mlt_consumer_properties( mlt_consumer this )
129 {
130 return this != NULL ? mlt_service_properties( &this->parent ) : NULL;
131 }
132
133 /** Connect the consumer to the producer.
134 */
135
136 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
137 {
138 return mlt_service_connect_producer( &this->parent, producer, 0 );
139 }
140
141 /** Start the consumer.
142 */
143
144 int mlt_consumer_start( mlt_consumer this )
145 {
146 // Get the properies
147 mlt_properties properties = mlt_consumer_properties( this );
148
149 // Determine if there's a test card producer
150 char *test_card = mlt_properties_get( properties, "test_card" );
151
152 // Deal with it now.
153 if ( test_card != NULL )
154 {
155 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
156 {
157 // Create a test card producer
158 mlt_producer producer = mlt_factory_producer( NULL, test_card );
159
160 // Do we have a producer
161 if ( producer != NULL )
162 {
163 // Test card should loop I guess...
164 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
165
166 // Set the test card on the consumer
167 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
168 }
169 }
170 }
171 else
172 {
173 // Allow the hash table to speed things up
174 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
175 }
176
177 // Check and run an ante command
178 if ( mlt_properties_get( properties, "ante" ) )
179 system( mlt_properties_get( properties, "ante" ) );
180
181 // Set the real_time preference
182 this->real_time = mlt_properties_get_int( properties, "real_time" );
183
184 // Start the service
185 if ( this->start != NULL )
186 return this->start( this );
187
188 return 0;
189 }
190
191 /** Protected method for consumer to get frames from connected service
192 */
193
194 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
195 {
196 // Frame to return
197 mlt_frame frame = NULL;
198
199 // Get the service assoicated to the consumer
200 mlt_service service = mlt_consumer_service( this );
201
202 // Get the frame
203 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
204 {
205 // Get the consumer properties
206 mlt_properties properties = mlt_consumer_properties( this );
207
208 // Get the frame properties
209 mlt_properties frame_properties = mlt_frame_properties( frame );
210
211 // Get the test card producer
212 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
213
214 // Attach the test frame producer to it.
215 if ( test_card != NULL )
216 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
217
218 // Attach the rescale property
219 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
220
221 // Aspect ratio and other jiggery pokery
222 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
223 if ( mlt_properties_get_int( properties, "progressive" ) || mlt_properties_get_int( properties, "deinterlace" ) )
224 mlt_properties_set_int( frame_properties, "consumer_deinterlace", 1 );
225 }
226
227 // Return the frame
228 return frame;
229 }
230
231 static inline long time_difference( struct timeval *time1 )
232 {
233 struct timeval time2;
234 time2.tv_sec = time1->tv_sec;
235 time2.tv_usec = time1->tv_usec;
236 gettimeofday( time1, NULL );
237 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
238 }
239
240 static void *consumer_read_ahead_thread( void *arg )
241 {
242 // The argument is the consumer
243 mlt_consumer this = arg;
244
245 // Get the properties of the consumer
246 mlt_properties properties = mlt_consumer_properties( this );
247
248 // Get the width and height
249 int width = mlt_properties_get_int( properties, "width" );
250 int height = mlt_properties_get_int( properties, "height" );
251
252 // See if video is turned off
253 int video_off = mlt_properties_get_int( properties, "video_off" );
254
255 // Get the audio settings
256 mlt_audio_format afmt = mlt_audio_pcm;
257 int counter = 0;
258 double fps = mlt_properties_get_double( properties, "fps" );
259 int channels = mlt_properties_get_int( properties, "channels" );
260 int frequency = mlt_properties_get_int( properties, "frequency" );
261 int samples = 0;
262 int16_t *pcm = NULL;
263
264 // See if audio is turned off
265 int audio_off = mlt_properties_get_int( properties, "audio_off" );
266
267 // Get the maximum size of the buffer
268 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
269
270 // General frame variable
271 mlt_frame frame = NULL;
272 uint8_t *image = NULL;
273
274 // Time structures
275 struct timeval ante;
276
277 // Average time for get_frame and get_image
278 int count = 1;
279 int skipped = 0;
280 int64_t time_wait = 0;
281 int64_t time_frame = 0;
282 int64_t time_process = 0;
283 int skip_next = 0;
284
285 // Get the first frame
286 frame = mlt_consumer_get_frame( this );
287
288 // Get the image of the first frame
289 if ( !video_off )
290 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
291
292 if ( !audio_off )
293 {
294 samples = mlt_sample_calculator( fps, frequency, counter++ );
295 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
296 frame->get_audio = NULL;
297 }
298
299 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
300
301 // Get the starting time (can ignore the times above)
302 gettimeofday( &ante, NULL );
303
304 // Continue to read ahead
305 while ( this->ahead )
306 {
307 // Put the current frame into the queue
308 pthread_mutex_lock( &this->mutex );
309 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
310 pthread_cond_wait( &this->cond, &this->mutex );
311 mlt_deque_push_back( this->queue, frame );
312 pthread_cond_broadcast( &this->cond );
313 pthread_mutex_unlock( &this->mutex );
314 time_wait += time_difference( &ante );
315
316 // Get the next frame
317 frame = mlt_consumer_get_frame( this );
318 time_frame += time_difference( &ante );
319
320 // Increment the count
321 count ++;
322
323 // All non normal playback frames should be shown
324 if ( mlt_properties_get_int( mlt_frame_properties( frame ), "_speed" ) != 1 )
325 {
326 skipped = 0;
327 time_frame = 0;
328 time_process = 0;
329 time_wait = 0;
330 count = 1;
331 skip_next = 0;
332 }
333
334 // Get the image
335 if ( !skip_next )
336 {
337 // Get the image, mark as rendered and time it
338 if ( !video_off )
339 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
340 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
341 }
342 else
343 {
344 // Increment the number of sequentially skipped frames
345 skipped ++;
346 skip_next = 0;
347
348 // If we've reached an unacceptable level, reset everything
349 if ( skipped > 5 )
350 {
351 skipped = 0;
352 time_frame = 0;
353 time_process = 0;
354 time_wait = 0;
355 count = 1;
356 }
357 }
358
359 // Always process audio
360 if ( !audio_off )
361 {
362 samples = mlt_sample_calculator( fps, frequency, counter++ );
363 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
364 frame->get_audio = NULL;
365 }
366
367 // Increment the time take for this frame
368 time_process += time_difference( &ante );
369
370 // Determine if the next frame should be skipped
371 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
372 skip_next = 1;
373 }
374
375 // Remove the last frame
376 mlt_frame_close( frame );
377
378 return NULL;
379 }
380
381 static void consumer_read_ahead_start( mlt_consumer this )
382 {
383 pthread_attr_t thread_attributes;
384
385 // We're running now
386 this->ahead = 1;
387
388 // Create the frame queue
389 this->queue = mlt_deque_init( );
390
391 // Create the mutex
392 pthread_mutex_init( &this->mutex, NULL );
393
394 // Create the condition
395 pthread_cond_init( &this->cond, NULL );
396
397 // Inherit the scheduling priority
398 pthread_attr_init( &thread_attributes );
399 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
400
401 // Create the read ahead
402 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
403 }
404
405 static void consumer_read_ahead_stop( mlt_consumer this )
406 {
407 // Make sure we're running
408 if ( this->ahead )
409 {
410 // Inform thread to stop
411 this->ahead = 0;
412
413 // Broadcast to the condition in case it's waiting
414 pthread_mutex_lock( &this->mutex );
415 pthread_cond_broadcast( &this->cond );
416 pthread_mutex_unlock( &this->mutex );
417
418 // Join the thread
419 pthread_join( this->ahead_thread, NULL );
420
421 // Destroy the mutex
422 pthread_mutex_destroy( &this->mutex );
423
424 // Destroy the condition
425 pthread_cond_destroy( &this->cond );
426
427 // Wipe the queue
428 while ( mlt_deque_count( this->queue ) )
429 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
430
431 // Close the queue
432 mlt_deque_close( this->queue );
433 }
434 }
435
436 void mlt_consumer_purge( mlt_consumer this )
437 {
438 if ( this->ahead )
439 {
440 pthread_mutex_lock( &this->mutex );
441 while ( mlt_deque_count( this->queue ) )
442 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
443 pthread_cond_broadcast( &this->cond );
444 pthread_mutex_unlock( &this->mutex );
445 }
446 }
447
448 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
449 {
450 // Frame to return
451 mlt_frame frame = NULL;
452
453 // Get the properties
454 mlt_properties properties = mlt_consumer_properties( this );
455
456 // Check if the user has requested real time or not
457 if ( this->real_time )
458 {
459 int size = 1;
460
461 // Is the read ahead running?
462 if ( this->ahead == 0 )
463 {
464 int buffer = mlt_properties_get_int( properties, "buffer" );
465 int prefill = mlt_properties_get_int( properties, "prefill" );
466 consumer_read_ahead_start( this );
467 if ( buffer > 1 )
468 size = prefill > 0 && prefill < buffer ? prefill : buffer;
469 }
470
471 // Get frame from queue
472 pthread_mutex_lock( &this->mutex );
473 while( this->ahead && mlt_deque_count( this->queue ) < size )
474 pthread_cond_wait( &this->cond, &this->mutex );
475 frame = mlt_deque_pop_front( this->queue );
476 pthread_cond_broadcast( &this->cond );
477 pthread_mutex_unlock( &this->mutex );
478 }
479 else
480 {
481 // Get the frame in non real time
482 frame = mlt_consumer_get_frame( this );
483
484 // This isn't true, but from the consumers perspective it is
485 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
486 }
487
488 return frame;
489 }
490
491 /** Callback for the implementation to indicate a stopped condition.
492 */
493
494 void mlt_consumer_stopped( mlt_consumer this )
495 {
496 mlt_properties_set_int( mlt_consumer_properties( this ), "running", 0 );
497 mlt_events_fire( mlt_consumer_properties( this ), "consumer-stopped", NULL );
498 }
499
500 /** Stop the consumer.
501 */
502
503 int mlt_consumer_stop( mlt_consumer this )
504 {
505 // Get the properies
506 mlt_properties properties = mlt_consumer_properties( this );
507
508 // Stop the consumer
509 if ( this->stop != NULL )
510 this->stop( this );
511
512 // Check if the user has requested real time or not and stop if necessary
513 if ( mlt_properties_get_int( properties, "real_time" ) )
514 consumer_read_ahead_stop( this );
515
516 // Kill the test card
517 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
518
519 // Check and run a post command
520 if ( mlt_properties_get( properties, "post" ) )
521 system( mlt_properties_get( properties, "post" ) );
522
523 return 0;
524 }
525
526 /** Determine if the consumer is stopped.
527 */
528
529 int mlt_consumer_is_stopped( mlt_consumer this )
530 {
531 // Check if the consumer is stopped
532 if ( this->is_stopped != NULL )
533 return this->is_stopped( this );
534
535 return 0;
536 }
537
538 /** Close the consumer.
539 */
540
541 void mlt_consumer_close( mlt_consumer this )
542 {
543 if ( this != NULL && mlt_properties_dec_ref( mlt_consumer_properties( this ) ) <= 0 )
544 {
545 // Get the childs close function
546 void ( *consumer_close )( ) = this->close;
547
548 // Make sure it only gets called once
549 this->close = NULL;
550 this->parent.close = NULL;
551
552 // Call the childs close if available
553 if ( consumer_close != NULL )
554 consumer_close( this );
555 else
556 mlt_service_close( &this->parent );
557 }
558 }