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