experimental tuning
[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 }
57 else
58 {
59 mlt_properties_set( properties, "normalisation", "NTSC" );
60 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
61 mlt_properties_set_int( properties, "width", 720 );
62 mlt_properties_set_int( properties, "height", 480 );
63 mlt_properties_set_int( properties, "progressive", 0 );
64 }
65
66 // Default aspect ratio
67 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
68
69 // Default rescaler for all consumers
70 mlt_properties_set( properties, "rescale", "bilinear" );
71
72 // Default read ahead buffer size
73 mlt_properties_set_int( properties, "buffer", 25 );
74
75 // Hmm - default all consumers to yuv422 :-/
76 this->format = mlt_image_yuv422;
77 }
78 return error;
79 }
80
81 /** Get the parent service object.
82 */
83
84 mlt_service mlt_consumer_service( mlt_consumer this )
85 {
86 return &this->parent;
87 }
88
89 /** Get the consumer properties.
90 */
91
92 mlt_properties mlt_consumer_properties( mlt_consumer this )
93 {
94 return mlt_service_properties( &this->parent );
95 }
96
97 /** Connect the consumer to the producer.
98 */
99
100 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
101 {
102 return mlt_service_connect_producer( &this->parent, producer, 0 );
103 }
104
105 /** Start the consumer.
106 */
107
108 int mlt_consumer_start( mlt_consumer this )
109 {
110 // Get the properies
111 mlt_properties properties = mlt_consumer_properties( this );
112
113 // Determine if there's a test card producer
114 char *test_card = mlt_properties_get( properties, "test_card" );
115
116 // Deal with it now.
117 if ( test_card != NULL )
118 {
119 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
120 {
121 // Create a test card producer
122 // TODO: do we want to use fezzik here?
123 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
124
125 // Do we have a producer
126 if ( producer != NULL )
127 {
128 // Test card should loop I guess...
129 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
130
131 // Set the test card on the consumer
132 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
133 }
134
135 // Check and run an ante command
136 if ( mlt_properties_get( properties, "ante" ) )
137 system( mlt_properties_get( properties, "ante" ) );
138 }
139 }
140 else
141 {
142 // Allow the hash table to speed things up
143 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
144 }
145
146 // Start the service
147 if ( this->start != NULL )
148 return this->start( this );
149
150 return 0;
151 }
152
153 /** Protected method :-/ for consumer to get frames from connected service
154 */
155
156 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
157 {
158 // Frame to return
159 mlt_frame frame = NULL;
160
161 // Get the service assoicated to the consumer
162 mlt_service service = mlt_consumer_service( this );
163
164 // Get the frame
165 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
166 {
167 // Get the consumer properties
168 mlt_properties properties = mlt_consumer_properties( this );
169
170 // Get the frame properties
171 mlt_properties frame_properties = mlt_frame_properties( frame );
172
173 // Get the test card producer
174 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
175
176 // Attach the test frame producer to it.
177 if ( test_card != NULL )
178 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
179
180 // Attach the rescale property
181 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
182
183 // Aspect ratio and other jiggery pokery
184 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
185 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
186 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
187 }
188
189 // Return the frame
190 return frame;
191 }
192
193 static inline long time_difference( struct timeval *time1 )
194 {
195 struct timeval time2;
196 time2.tv_sec = time1->tv_sec;
197 time2.tv_usec = time1->tv_usec;
198 gettimeofday( time1, NULL );
199 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
200 }
201
202 static void *consumer_read_ahead_thread( void *arg )
203 {
204 // The argument is the consumer
205 mlt_consumer this = arg;
206
207 // Get the properties of the consumer
208 mlt_properties properties = mlt_consumer_properties( this );
209
210 // Get the width and height
211 int width = mlt_properties_get_int( properties, "width" );
212 int height = mlt_properties_get_int( properties, "height" );
213
214 // Get the maximum size of the buffer
215 int buffer = mlt_properties_get_int( properties, "buffer" );
216
217 // General frame variable
218 mlt_frame frame = NULL;
219 uint8_t *image = NULL;
220
221 // Time structures
222 struct timeval ante;
223
224 // Average time for get_frame and get_image
225 int count = 1;
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 }
268
269 // Remove the last frame
270 mlt_frame_close( frame );
271
272 return NULL;
273 }
274
275 static void consumer_read_ahead_start( mlt_consumer this )
276 {
277 // We're running now
278 this->ahead = 1;
279
280 // Create the frame queue
281 this->queue = mlt_deque_init( );
282
283 // Create the mutex
284 pthread_mutex_init( &this->mutex, NULL );
285
286 // Create the condition
287 pthread_cond_init( &this->cond, NULL );
288
289 // Create the read ahead
290 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
291
292 }
293
294 static void consumer_read_ahead_stop( mlt_consumer this )
295 {
296 // Make sure we're running
297 if ( this->ahead )
298 {
299 // Inform thread to stop
300 this->ahead = 0;
301
302 // Broadcast to the condition in case it's waiting
303 pthread_mutex_lock( &this->mutex );
304 pthread_cond_broadcast( &this->cond );
305 pthread_mutex_unlock( &this->mutex );
306
307 // Join the thread
308 pthread_join( this->ahead_thread, NULL );
309
310 // Destroy the mutex
311 pthread_mutex_destroy( &this->mutex );
312
313 // Destroy the condition
314 pthread_cond_destroy( &this->cond );
315
316 // Wipe the queue
317 while ( mlt_deque_count( this->queue ) )
318 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
319 }
320 }
321
322 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
323 {
324 // Frame to return
325 mlt_frame frame = NULL;
326 int size = 1;
327
328 // Is the read ahead running?
329 if ( this->ahead == 0 )
330 {
331 int buffer = mlt_properties_get_int( mlt_consumer_properties( this ), "buffer" );
332 consumer_read_ahead_start( this );
333 if ( buffer > 1 )
334 size = buffer / 2;
335 }
336
337 // Get frame from queue
338 pthread_mutex_lock( &this->mutex );
339 while( this->ahead && mlt_deque_count( this->queue ) < size )
340 pthread_cond_wait( &this->cond, &this->mutex );
341 frame = mlt_deque_pop_front( this->queue );
342 pthread_cond_broadcast( &this->cond );
343 pthread_mutex_unlock( &this->mutex );
344
345 return frame;
346 }
347
348 /** Stop the consumer.
349 */
350
351 int mlt_consumer_stop( mlt_consumer this )
352 {
353 // Get the properies
354 mlt_properties properties = mlt_consumer_properties( this );
355
356 // Stop the consumer
357 if ( this->stop != NULL )
358 this->stop( this );
359
360 // Kill the read ahead
361 consumer_read_ahead_stop( this );
362
363 // Kill the test card
364 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
365
366 // Check and run a post command
367 if ( mlt_properties_get( properties, "post" ) )
368 system( mlt_properties_get( properties, "post" ) );
369
370 return 0;
371 }
372
373 /** Determine if the consumer is stopped.
374 */
375
376 int mlt_consumer_is_stopped( mlt_consumer this )
377 {
378 // Check if the consumer is stopped
379 if ( this->is_stopped != NULL )
380 return this->is_stopped( this );
381
382 return 0;
383 }
384
385 /** Close the consumer.
386 */
387
388 void mlt_consumer_close( mlt_consumer this )
389 {
390 // Get the childs close function
391 void ( *consumer_close )( ) = this->close;
392
393 // Make sure it only gets called once
394 this->close = NULL;
395
396 // Call the childs close if available
397 if ( consumer_close != NULL )
398 consumer_close( this );
399 else
400 mlt_service_close( &this->parent );
401 }
402