Consumer deinterlace_method property added
[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_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
32 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
33
34 /** Public final methods
35 */
36
37 int mlt_consumer_init( mlt_consumer this, void *child )
38 {
39 int error = 0;
40 memset( this, 0, sizeof( struct mlt_consumer_s ) );
41 this->child = child;
42 error = mlt_service_init( &this->parent, this );
43 if ( error == 0 )
44 {
45 // Get the properties from the service
46 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
47
48 // Get the normalisation preference
49 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
50
51 // Deal with normalisation
52 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
53 {
54 mlt_properties_set( properties, "normalisation", "PAL" );
55 mlt_properties_set_double( properties, "fps", 25.0 );
56 mlt_properties_set_int( properties, "frame_rate_den", 25 );
57 mlt_properties_set_int( properties, "frame_rate_num", 1 );
58 mlt_properties_set_int( properties, "width", 720 );
59 mlt_properties_set_int( properties, "height", 576 );
60 mlt_properties_set_int( properties, "progressive", 0 );
61 mlt_properties_set_double( properties, "aspect_ratio", 59.0 / 54.0 );
62 }
63 else
64 {
65 mlt_properties_set( properties, "normalisation", "NTSC" );
66 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
67 mlt_properties_set_int( properties, "frame_rate_den", 30000 );
68 mlt_properties_set_int( properties, "frame_rate_num", 1001 );
69 mlt_properties_set_int( properties, "width", 720 );
70 mlt_properties_set_int( properties, "height", 480 );
71 mlt_properties_set_int( properties, "progressive", 0 );
72 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
73 }
74
75 // Default rescaler for all consumers
76 mlt_properties_set( properties, "rescale", "bilinear" );
77
78 // Default read ahead buffer size
79 mlt_properties_set_int( properties, "buffer", 25 );
80
81 // Default audio frequency and channels
82 mlt_properties_set_int( properties, "frequency", 48000 );
83 mlt_properties_set_int( properties, "channels", 2 );
84
85 // Default of all consumers is real time
86 mlt_properties_set_int( properties, "real_time", 1 );
87
88 // Default to environment test card
89 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
90
91 // Hmm - default all consumers to yuv422 :-/
92 this->format = mlt_image_yuv422;
93
94 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
95 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
96 mlt_events_register( properties, "consumer-stopped", NULL );
97
98 // Create the push mutex and condition
99 pthread_mutex_init( &this->put_mutex, NULL );
100 pthread_cond_init( &this->put_cond, NULL );
101
102 }
103 return error;
104 }
105
106 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
107 {
108 if ( listener != NULL )
109 listener( owner, this, ( mlt_frame )args[ 0 ] );
110 }
111
112 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
113 {
114 if ( listener != NULL )
115 listener( owner, this, ( mlt_frame )args[ 0 ] );
116 }
117
118 /** Create a new consumer.
119 */
120
121 mlt_consumer mlt_consumer_new( )
122 {
123 // Create the memory for the structure
124 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
125
126 // Initialise it
127 if ( this != NULL )
128 mlt_consumer_init( this, NULL );
129
130 // Return it
131 return this;
132 }
133
134 /** Get the parent service object.
135 */
136
137 mlt_service mlt_consumer_service( mlt_consumer this )
138 {
139 return this != NULL ? &this->parent : NULL;
140 }
141
142 /** Get the consumer properties.
143 */
144
145 mlt_properties mlt_consumer_properties( mlt_consumer this )
146 {
147 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
148 }
149
150 /** Connect the consumer to the producer.
151 */
152
153 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
154 {
155 return mlt_service_connect_producer( &this->parent, producer, 0 );
156 }
157
158 /** Start the consumer.
159 */
160
161 int mlt_consumer_start( mlt_consumer this )
162 {
163 // Get the properies
164 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
165
166 // Determine if there's a test card producer
167 char *test_card = mlt_properties_get( properties, "test_card" );
168
169 // Just to make sure nothing is hanging around...
170 mlt_frame_close( this->put );
171 this->put = NULL;
172 this->put_active = 1;
173
174 // Deal with it now.
175 if ( test_card != NULL )
176 {
177 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
178 {
179 // Create a test card producer
180 mlt_producer producer = mlt_factory_producer( NULL, test_card );
181
182 // Do we have a producer
183 if ( producer != NULL )
184 {
185 // Test card should loop I guess...
186 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
187 //mlt_producer_set_speed( producer, 0 );
188 //mlt_producer_set_in_and_out( producer, 0, 0 );
189
190 // Set the test card on the consumer
191 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
192 }
193 }
194 }
195 else
196 {
197 // Allow the hash table to speed things up
198 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
199 }
200
201 // Check and run an ante command
202 if ( mlt_properties_get( properties, "ante" ) )
203 system( mlt_properties_get( properties, "ante" ) );
204
205 // Set the real_time preference
206 this->real_time = mlt_properties_get_int( properties, "real_time" );
207
208 // Start the service
209 if ( this->start != NULL )
210 return this->start( this );
211
212 return 0;
213 }
214
215 /** An alternative method to feed frames into the consumer - only valid if
216 the consumer itself is not connected.
217 */
218
219 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
220 {
221 int error = 1;
222
223 // Get the service assoicated to the consumer
224 mlt_service service = MLT_CONSUMER_SERVICE( this );
225
226 if ( mlt_service_producer( service ) == NULL )
227 {
228 struct timeval now;
229 struct timespec tm;
230 pthread_mutex_lock( &this->put_mutex );
231 while ( this->put_active && this->put != NULL )
232 {
233 gettimeofday( &now, NULL );
234 tm.tv_sec = now.tv_sec + 1;
235 tm.tv_nsec = now.tv_usec * 1000;
236 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
237 }
238 if ( this->put_active && this->put == NULL )
239 this->put = frame;
240 else
241 mlt_frame_close( frame );
242 pthread_cond_broadcast( &this->put_cond );
243 pthread_mutex_unlock( &this->put_mutex );
244 }
245 else
246 {
247 mlt_frame_close( frame );
248 }
249
250 return error;
251 }
252
253 /** Protected method for consumer to get frames from connected service
254 */
255
256 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
257 {
258 // Frame to return
259 mlt_frame frame = NULL;
260
261 // Get the service assoicated to the consumer
262 mlt_service service = MLT_CONSUMER_SERVICE( this );
263
264 // Get the consumer properties
265 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
266
267 // Get the frame
268 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
269 {
270 struct timeval now;
271 struct timespec tm;
272 pthread_mutex_lock( &this->put_mutex );
273 while ( this->put_active && this->put == NULL )
274 {
275 gettimeofday( &now, NULL );
276 tm.tv_sec = now.tv_sec + 1;
277 tm.tv_nsec = now.tv_usec * 1000;
278 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
279 }
280 frame = this->put;
281 this->put = NULL;
282 pthread_cond_broadcast( &this->put_cond );
283 pthread_mutex_unlock( &this->put_mutex );
284 if ( frame != NULL )
285 mlt_service_apply_filters( service, frame, 0 );
286 }
287 else if ( mlt_service_producer( service ) != NULL )
288 {
289 mlt_service_get_frame( service, &frame, 0 );
290 }
291 else
292 {
293 frame = mlt_frame_init( );
294 }
295
296 if ( frame != NULL )
297 {
298 // Get the frame properties
299 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
300
301 // Get the test card producer
302 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
303
304 // Attach the test frame producer to it.
305 if ( test_card != NULL )
306 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
307
308 // Attach the rescale property
309 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
310
311 // Aspect ratio and other jiggery pokery
312 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
313 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
314 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
315 }
316
317 // Return the frame
318 return frame;
319 }
320
321 static inline long time_difference( struct timeval *time1 )
322 {
323 struct timeval time2;
324 time2.tv_sec = time1->tv_sec;
325 time2.tv_usec = time1->tv_usec;
326 gettimeofday( time1, NULL );
327 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
328 }
329
330 static void *consumer_read_ahead_thread( void *arg )
331 {
332 // The argument is the consumer
333 mlt_consumer this = arg;
334
335 // Get the properties of the consumer
336 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
337
338 // Get the width and height
339 int width = mlt_properties_get_int( properties, "width" );
340 int height = mlt_properties_get_int( properties, "height" );
341
342 // See if video is turned off
343 int video_off = mlt_properties_get_int( properties, "video_off" );
344
345 // Get the audio settings
346 mlt_audio_format afmt = mlt_audio_pcm;
347 int counter = 0;
348 double fps = mlt_properties_get_double( properties, "fps" );
349 int channels = mlt_properties_get_int( properties, "channels" );
350 int frequency = mlt_properties_get_int( properties, "frequency" );
351 int samples = 0;
352 int16_t *pcm = NULL;
353
354 // See if audio is turned off
355 int audio_off = mlt_properties_get_int( properties, "audio_off" );
356
357 // Get the maximum size of the buffer
358 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
359
360 // General frame variable
361 mlt_frame frame = NULL;
362 uint8_t *image = NULL;
363
364 // Time structures
365 struct timeval ante;
366
367 // Average time for get_frame and get_image
368 int count = 1;
369 int skipped = 0;
370 int64_t time_wait = 0;
371 int64_t time_frame = 0;
372 int64_t time_process = 0;
373 int skip_next = 0;
374 mlt_service lock_object = NULL;
375
376 // Get the first frame
377 frame = mlt_consumer_get_frame( this );
378
379 // Get the lock object
380 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
381
382 // Lock it
383 if ( lock_object ) mlt_service_lock( lock_object );
384
385 // Get the image of the first frame
386 if ( !video_off )
387 {
388 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
389 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
390 }
391
392 if ( !audio_off )
393 {
394 samples = mlt_sample_calculator( fps, frequency, counter++ );
395 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
396 }
397
398 // Unlock the lock object
399 if ( lock_object ) mlt_service_unlock( lock_object );
400
401 // Mark as rendered
402 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
403
404 // Get the starting time (can ignore the times above)
405 gettimeofday( &ante, NULL );
406
407 // Continue to read ahead
408 while ( this->ahead )
409 {
410 // Put the current frame into the queue
411 pthread_mutex_lock( &this->mutex );
412 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
413 pthread_cond_wait( &this->cond, &this->mutex );
414 mlt_deque_push_back( this->queue, frame );
415 pthread_cond_broadcast( &this->cond );
416 pthread_mutex_unlock( &this->mutex );
417
418 time_wait += time_difference( &ante );
419
420 // Get the next frame
421 frame = mlt_consumer_get_frame( this );
422 time_frame += time_difference( &ante );
423
424 // If there's no frame, we're probably stopped...
425 if ( frame == NULL )
426 continue;
427
428 // Attempt to fetch the lock object
429 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
430
431 // Increment the count
432 count ++;
433
434 // Lock if there's a lock object
435 if ( lock_object ) mlt_service_lock( lock_object );
436
437 // All non normal playback frames should be shown
438 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
439 {
440 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
441 skipped = 0;
442 time_frame = 0;
443 time_process = 0;
444 time_wait = 0;
445 count = 1;
446 skip_next = 0;
447 }
448
449 // Get the image
450 if ( !skip_next )
451 {
452 // Get the image, mark as rendered and time it
453 if ( !video_off )
454 {
455 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
456 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
457 }
458 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
459 }
460 else
461 {
462 // Increment the number of sequentially skipped frames
463 skipped ++;
464 skip_next = 0;
465
466 // If we've reached an unacceptable level, reset everything
467 if ( skipped > 5 )
468 {
469 skipped = 0;
470 time_frame = 0;
471 time_process = 0;
472 time_wait = 0;
473 count = 1;
474 }
475 }
476
477 // Always process audio
478 if ( !audio_off )
479 {
480 samples = mlt_sample_calculator( fps, frequency, counter++ );
481 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
482 }
483
484 // Increment the time take for this frame
485 time_process += time_difference( &ante );
486
487 // Determine if the next frame should be skipped
488 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
489 skip_next = 1;
490
491 // Unlock if there's a lock object
492 if ( lock_object ) mlt_service_unlock( lock_object );
493 }
494
495 // Remove the last frame
496 mlt_frame_close( frame );
497
498 return NULL;
499 }
500
501 static void consumer_read_ahead_start( mlt_consumer this )
502 {
503 // We're running now
504 this->ahead = 1;
505
506 // Create the frame queue
507 this->queue = mlt_deque_init( );
508
509 // Create the mutex
510 pthread_mutex_init( &this->mutex, NULL );
511
512 // Create the condition
513 pthread_cond_init( &this->cond, NULL );
514
515 // Create the read ahead
516 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
517 }
518
519 static void consumer_read_ahead_stop( mlt_consumer this )
520 {
521 // Make sure we're running
522 if ( this->ahead )
523 {
524 // Inform thread to stop
525 this->ahead = 0;
526
527 // Broadcast to the condition in case it's waiting
528 pthread_mutex_lock( &this->mutex );
529 pthread_cond_broadcast( &this->cond );
530 pthread_mutex_unlock( &this->mutex );
531
532 // Broadcast to the put condition in case it's waiting
533 pthread_mutex_lock( &this->put_mutex );
534 pthread_cond_broadcast( &this->put_cond );
535 pthread_mutex_unlock( &this->put_mutex );
536
537 // Join the thread
538 pthread_join( this->ahead_thread, NULL );
539
540 // Destroy the mutex
541 pthread_mutex_destroy( &this->mutex );
542
543 // Destroy the condition
544 pthread_cond_destroy( &this->cond );
545
546 // Wipe the queue
547 while ( mlt_deque_count( this->queue ) )
548 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
549
550 // Close the queue
551 mlt_deque_close( this->queue );
552 }
553 }
554
555 void mlt_consumer_purge( mlt_consumer this )
556 {
557 if ( this->ahead )
558 {
559 pthread_mutex_lock( &this->mutex );
560 while ( mlt_deque_count( this->queue ) )
561 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
562 pthread_cond_broadcast( &this->cond );
563 pthread_mutex_unlock( &this->mutex );
564 }
565 }
566
567 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
568 {
569 // Frame to return
570 mlt_frame frame = NULL;
571
572 // Get the properties
573 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
574
575 // Check if the user has requested real time or not
576 if ( this->real_time )
577 {
578 int size = 1;
579
580 // Is the read ahead running?
581 if ( this->ahead == 0 )
582 {
583 int buffer = mlt_properties_get_int( properties, "buffer" );
584 int prefill = mlt_properties_get_int( properties, "prefill" );
585 consumer_read_ahead_start( this );
586 if ( buffer > 1 )
587 size = prefill > 0 && prefill < buffer ? prefill : buffer;
588 }
589
590 // Get frame from queue
591 pthread_mutex_lock( &this->mutex );
592 while( this->ahead && mlt_deque_count( this->queue ) < size )
593 pthread_cond_wait( &this->cond, &this->mutex );
594 frame = mlt_deque_pop_front( this->queue );
595 pthread_cond_broadcast( &this->cond );
596 pthread_mutex_unlock( &this->mutex );
597 }
598 else
599 {
600 // Get the frame in non real time
601 frame = mlt_consumer_get_frame( this );
602
603 // This isn't true, but from the consumers perspective it is
604 if ( frame != NULL )
605 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
606 }
607
608 return frame;
609 }
610
611 /** Callback for the implementation to indicate a stopped condition.
612 */
613
614 void mlt_consumer_stopped( mlt_consumer this )
615 {
616 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
617 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
618 }
619
620 /** Stop the consumer.
621 */
622
623 int mlt_consumer_stop( mlt_consumer this )
624 {
625 // Get the properies
626 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
627 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
628
629 // Just in case...
630 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
631 pthread_mutex_lock( &this->put_mutex );
632 this->put_active = 0;
633 pthread_cond_broadcast( &this->put_cond );
634 pthread_mutex_unlock( &this->put_mutex );
635
636 // Stop the consumer
637 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
638 if ( this->stop != NULL )
639 this->stop( this );
640
641 // Check if the user has requested real time or not and stop if necessary
642 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
643 if ( mlt_properties_get_int( properties, "real_time" ) )
644 consumer_read_ahead_stop( this );
645
646 // Kill the test card
647 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
648
649 // Check and run a post command
650 if ( mlt_properties_get( properties, "post" ) )
651 system( mlt_properties_get( properties, "post" ) );
652
653 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
654
655 return 0;
656 }
657
658 /** Determine if the consumer is stopped.
659 */
660
661 int mlt_consumer_is_stopped( mlt_consumer this )
662 {
663 // Check if the consumer is stopped
664 if ( this->is_stopped != NULL )
665 return this->is_stopped( this );
666
667 return 0;
668 }
669
670 /** Close the consumer.
671 */
672
673 void mlt_consumer_close( mlt_consumer this )
674 {
675 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
676 {
677 // Get the childs close function
678 void ( *consumer_close )( ) = this->close;
679
680 if ( consumer_close )
681 {
682 // Just in case...
683 //mlt_consumer_stop( this );
684
685 this->close = NULL;
686 consumer_close( this );
687 }
688 else
689 {
690 // Make sure it only gets called once
691 this->parent.close = NULL;
692
693 // Destroy the push mutex and condition
694 pthread_mutex_destroy( &this->put_mutex );
695 pthread_cond_destroy( &this->put_cond );
696
697 mlt_service_close( &this->parent );
698 }
699 }
700 }