more sdl/consumer tuning and demo updates
[melted] / src / framework / mlt_consumer.c
1 /*
2 * mlt_consumer.c -- abstraction for all consumer services
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "config.h"
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30
31 /** 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 // Hmm - default all consumers to yuv422 :-/
75 this->format = mlt_image_yuv422;
76 }
77 return error;
78 }
79
80 /** Get the parent service object.
81 */
82
83 mlt_service mlt_consumer_service( mlt_consumer this )
84 {
85 return &this->parent;
86 }
87
88 /** Get the consumer properties.
89 */
90
91 mlt_properties mlt_consumer_properties( mlt_consumer this )
92 {
93 return mlt_service_properties( &this->parent );
94 }
95
96 /** Connect the consumer to the producer.
97 */
98
99 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
100 {
101 return mlt_service_connect_producer( &this->parent, producer, 0 );
102 }
103
104 /** Start the consumer.
105 */
106
107 int mlt_consumer_start( mlt_consumer this )
108 {
109 // Get the properies
110 mlt_properties properties = mlt_consumer_properties( this );
111
112 // Determine if there's a test card producer
113 char *test_card = mlt_properties_get( properties, "test_card" );
114
115 // Deal with it now.
116 if ( test_card != NULL )
117 {
118 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
119 {
120 // Create a test card producer
121 // TODO: do we want to use fezzik here?
122 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
123
124 // Do we have a producer
125 if ( producer != NULL )
126 {
127 // Test card should loop I guess...
128 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
129
130 // Set the test card on the consumer
131 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
132 }
133
134 // Check and run an ante command
135 if ( mlt_properties_get( properties, "ante" ) )
136 system( mlt_properties_get( properties, "ante" ) );
137 }
138 }
139 else
140 {
141 // Allow the hash table to speed things up
142 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
143 }
144
145 // Start the service
146 if ( this->start != NULL )
147 return this->start( this );
148
149 return 0;
150 }
151
152 /** Protected method :-/ for consumer to get frames from connected service
153 */
154
155 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
156 {
157 // Frame to return
158 mlt_frame frame = NULL;
159
160 // Get the service assoicated to the consumer
161 mlt_service service = mlt_consumer_service( this );
162
163 // Get the frame
164 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
165 {
166 // Get the consumer properties
167 mlt_properties properties = mlt_consumer_properties( this );
168
169 // Get the frame properties
170 mlt_properties frame_properties = mlt_frame_properties( frame );
171
172 // Get the test card producer
173 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
174
175 // Attach the test frame producer to it.
176 if ( test_card != NULL )
177 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
178
179 // Attach the rescale property
180 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
181
182 // Aspect ratio and other jiggery pokery
183 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
184 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
185 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
186 }
187
188 // Return the frame
189 return frame;
190 }
191
192 static inline long time_difference( struct timeval *time1 )
193 {
194 struct timeval time2;
195 time2.tv_sec = time1->tv_sec;
196 time2.tv_usec = time1->tv_usec;
197 gettimeofday( time1, NULL );
198 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
199 }
200
201 static void *consumer_read_ahead_thread( void *arg )
202 {
203 // The argument is the consumer
204 mlt_consumer this = arg;
205
206 // Get the properties of the consumer
207 mlt_properties properties = mlt_consumer_properties( this );
208
209 // Get the width and height
210 int width = mlt_properties_get_int( properties, "width" );
211 int height = mlt_properties_get_int( properties, "height" );
212
213 // Get the maximum size of the buffer
214 int buffer = mlt_properties_get_int( properties, "buffer" );
215
216 // General frame variable
217 mlt_frame frame = NULL;
218 uint8_t *image = NULL;
219
220 // Time structures
221 struct timeval ante;
222
223 // Average time for get_frame and get_image
224 int count = 1;
225 int skipped = 0;
226 int64_t time_wait = 0;
227 int64_t time_frame = 0;
228 int64_t time_image = 0;
229
230 // Get the first frame
231 frame = mlt_consumer_get_frame( this );
232
233 // Get the image of the first frame
234 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
235 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
236
237 // Get the starting time (can ignore the times above)
238 gettimeofday( &ante, NULL );
239
240 // Continue to read ahead
241 while ( this->ahead )
242 {
243 // Put the current frame into the queue
244 pthread_mutex_lock( &this->mutex );
245 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
246 pthread_cond_wait( &this->cond, &this->mutex );
247 mlt_deque_push_back( this->queue, frame );
248 pthread_cond_broadcast( &this->cond );
249 pthread_mutex_unlock( &this->mutex );
250 time_wait += time_difference( &ante );
251
252 // Get the next frame
253 frame = mlt_consumer_get_frame( this );
254 time_frame += time_difference( &ante );
255
256 // Increment the count
257 count ++;
258
259 // Get the image
260 if ( ( time_frame + time_image ) / count < ( 40000 - ( time_wait / count ) ) )
261 {
262 // Get the image, mark as rendered and time it
263 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
264 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
265 time_image += time_difference( &ante );
266
267 // Reset the skipped count
268 skipped = 0;
269 }
270 else
271 {
272 // Increment the number of sequentially skipped frames
273 skipped ++;
274
275 // If we've reached an unacceptable level, reset everything
276 if ( skipped > 10 )
277 {
278 skipped = 0;
279 time_frame = 0;
280 time_image = 0;
281 time_wait = 0;
282 count = 0;
283 }
284 }
285 }
286
287 // Remove the last frame
288 mlt_frame_close( frame );
289
290 return NULL;
291 }
292
293 static void consumer_read_ahead_start( mlt_consumer this )
294 {
295 // We're running now
296 this->ahead = 1;
297
298 // Create the frame queue
299 this->queue = mlt_deque_init( );
300
301 // Create the mutex
302 pthread_mutex_init( &this->mutex, NULL );
303
304 // Create the condition
305 pthread_cond_init( &this->cond, NULL );
306
307 // Create the read ahead
308 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
309
310 }
311
312 static void consumer_read_ahead_stop( mlt_consumer this )
313 {
314 // Make sure we're running
315 if ( this->ahead )
316 {
317 // Inform thread to stop
318 this->ahead = 0;
319
320 // Broadcast to the condition in case it's waiting
321 pthread_mutex_lock( &this->mutex );
322 pthread_cond_broadcast( &this->cond );
323 pthread_mutex_unlock( &this->mutex );
324
325 // Join the thread
326 pthread_join( this->ahead_thread, NULL );
327
328 // Destroy the mutex
329 pthread_mutex_destroy( &this->mutex );
330
331 // Destroy the condition
332 pthread_cond_destroy( &this->cond );
333
334 // Wipe the queue
335 while ( mlt_deque_count( this->queue ) )
336 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
337 }
338 }
339
340 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
341 {
342 // Frame to return
343 mlt_frame frame = NULL;
344 int size = 1;
345
346 // Is the read ahead running?
347 if ( this->ahead == 0 )
348 {
349 int buffer = mlt_properties_get_int( mlt_consumer_properties( this ), "buffer" );
350 consumer_read_ahead_start( this );
351 if ( buffer > 1 )
352 size = buffer / 2;
353 }
354
355 // Get frame from queue
356 pthread_mutex_lock( &this->mutex );
357 while( this->ahead && mlt_deque_count( this->queue ) < size )
358 pthread_cond_wait( &this->cond, &this->mutex );
359 frame = mlt_deque_pop_front( this->queue );
360 pthread_cond_broadcast( &this->cond );
361 pthread_mutex_unlock( &this->mutex );
362
363 return frame;
364 }
365
366 /** Stop the consumer.
367 */
368
369 int mlt_consumer_stop( mlt_consumer this )
370 {
371 // Get the properies
372 mlt_properties properties = mlt_consumer_properties( this );
373
374 // Stop the consumer
375 if ( this->stop != NULL )
376 this->stop( this );
377
378 // Kill the read ahead
379 consumer_read_ahead_stop( this );
380
381 // Kill the test card
382 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
383
384 // Check and run a post command
385 if ( mlt_properties_get( properties, "post" ) )
386 system( mlt_properties_get( properties, "post" ) );
387
388 return 0;
389 }
390
391 /** Determine if the consumer is stopped.
392 */
393
394 int mlt_consumer_is_stopped( mlt_consumer this )
395 {
396 // Check if the consumer is stopped
397 if ( this->is_stopped != NULL )
398 return this->is_stopped( this );
399
400 return 0;
401 }
402
403 /** Close the consumer.
404 */
405
406 void mlt_consumer_close( mlt_consumer this )
407 {
408 // Get the childs close function
409 void ( *consumer_close )( ) = this->close;
410
411 // Make sure it only gets called once
412 this->close = NULL;
413
414 // Call the childs close if available
415 if ( consumer_close != NULL )
416 consumer_close( this );
417 else
418 mlt_service_close( &this->parent );
419 }