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