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