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