inherit scheduling priority on any created thread
[melted] / src / modules / dv / consumer_libdv.c
1 /*
2 * consumer_libdv.c -- a DV encoder based on libdv
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 // Local header files
22 #include "consumer_libdv.h"
23
24 // mlt Header files
25 #include <framework/mlt_frame.h>
26
27 // System header files
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <pthread.h>
32
33 // libdv header files
34 #include <libdv/dv.h>
35
36 // Forward references.
37 static int consumer_start( mlt_consumer this );
38 static int consumer_stop( mlt_consumer this );
39 static int consumer_is_stopped( mlt_consumer this );
40 static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame );
41 static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame );
42 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame );
43 static void *consumer_thread( void *arg );
44 static void consumer_close( mlt_consumer this );
45
46 /** Initialise the dv consumer.
47 */
48
49 mlt_consumer consumer_libdv_init( char *arg )
50 {
51 // Allocate the consumer
52 mlt_consumer this = calloc( 1, sizeof( struct mlt_consumer_s ) );
53
54 // If memory allocated and initialises without error
55 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
56 {
57 // Get properties from the consumer
58 mlt_properties properties = mlt_consumer_properties( this );
59
60 // Assign close callback
61 this->close = consumer_close;
62
63 // Interpret the argument
64 if ( arg != NULL )
65 mlt_properties_set( properties, "target", arg );
66
67 // Set the encode and output handling method
68 mlt_properties_set_data( properties, "video", consumer_encode_video, 0, NULL, NULL );
69 mlt_properties_set_data( properties, "audio", consumer_encode_audio, 0, NULL, NULL );
70 mlt_properties_set_data( properties, "output", consumer_output, 0, NULL, NULL );
71
72 // Set up start/stop/terminated callbacks
73 this->start = consumer_start;
74 this->stop = consumer_stop;
75 this->is_stopped = consumer_is_stopped;
76 }
77 else
78 {
79 // Clean up in case of init failure
80 free( this );
81 this = NULL;
82 }
83
84 // Return this
85 return this;
86 }
87
88 /** Start the consumer.
89 */
90
91 static int consumer_start( mlt_consumer this )
92 {
93 // Get the properties
94 mlt_properties properties = mlt_consumer_properties( this );
95
96 // Check that we're not already running
97 if ( !mlt_properties_get_int( properties, "running" ) )
98 {
99 // Allocate a thread
100 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
101 pthread_attr_t thread_attributes;
102
103 // Assign the thread to properties
104 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
105
106 // Set the running state
107 mlt_properties_set_int( properties, "running", 1 );
108
109 // Inherit the scheduling priority
110 pthread_attr_init( &thread_attributes );
111 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
112
113 // Create the thread
114 pthread_create( thread, &thread_attributes, consumer_thread, this );
115 }
116 return 0;
117 }
118
119 /** Stop the consumer.
120 */
121
122 static int consumer_stop( mlt_consumer this )
123 {
124 // Get the properties
125 mlt_properties properties = mlt_consumer_properties( this );
126
127 // Check that we're running
128 if ( mlt_properties_get_int( properties, "running" ) )
129 {
130 // Get the thread
131 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
132
133 // Stop the thread
134 mlt_properties_set_int( properties, "running", 0 );
135
136 // Wait for termination
137 pthread_join( *thread, NULL );
138
139 // Close the output file :-) - this is obtuse - doesn't matter if output file
140 // exists or not - the destructor will kick in if it does
141 mlt_properties_set_data( properties, "output_file", NULL, 0, NULL, NULL );
142 }
143
144 return 0;
145 }
146
147 /** Determine if the consumer is stopped.
148 */
149
150 static int consumer_is_stopped( mlt_consumer this )
151 {
152 // Get the properties
153 mlt_properties properties = mlt_consumer_properties( this );
154 return !mlt_properties_get_int( properties, "running" );
155 }
156
157 /** Get or create a new libdv encoder.
158 */
159
160 static dv_encoder_t *libdv_get_encoder( mlt_consumer this, mlt_frame frame )
161 {
162 // Get the properties of the consumer
163 mlt_properties this_properties = mlt_consumer_properties( this );
164
165 // Obtain the dv_encoder
166 dv_encoder_t *encoder = mlt_properties_get_data( this_properties, "dv_encoder", NULL );
167
168 // Construct one if we don't have one
169 if ( encoder == NULL )
170 {
171 // Get the fps of the consumer (for now - should be from frame)
172 double fps = mlt_properties_get_double( this_properties, "fps" );
173
174 // Create the encoder
175 encoder = dv_encoder_new( 0, 0, 0 );
176
177 // Encoder settings
178 encoder->isPAL = fps == 25;
179 encoder->is16x9 = 0;
180 encoder->vlc_encode_passes = 1;
181 encoder->static_qno = 0;
182 encoder->force_dct = DV_DCT_AUTO;
183
184 // Store the encoder on the properties
185 mlt_properties_set_data( this_properties, "dv_encoder", encoder, 0, ( mlt_destructor )dv_encoder_free, NULL );
186 }
187
188 // Return the encoder
189 return encoder;
190 }
191
192
193 /** The libdv encode video method.
194 */
195
196 static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
197 {
198 // Obtain the dv_encoder
199 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
200
201 // Get the properties of the consumer
202 mlt_properties this_properties = mlt_consumer_properties( this );
203
204 // This will hold the size of the dv frame
205 int size = 0;
206
207 // Is the image rendered
208 int rendered = mlt_properties_get_int( mlt_frame_properties( frame ), "rendered" );
209
210 // Get width and height
211 int width = mlt_properties_get_int( this_properties, "width" );
212 int height = mlt_properties_get_int( this_properties, "height" );
213
214 // If we get an encoder, then encode the image
215 if ( rendered && encoder != NULL )
216 {
217 // Specify desired image properties
218 mlt_image_format fmt = mlt_image_yuv422;
219 uint8_t *image = NULL;
220
221 // Get the image
222 mlt_frame_get_image( frame, &image, &fmt, &width, &height, 0 );
223
224 // Check that we get what we expected
225 if ( fmt != mlt_image_yuv422 ||
226 width != mlt_properties_get_int( this_properties, "width" ) ||
227 height != mlt_properties_get_int( this_properties, "height" ) ||
228 image == NULL )
229 {
230 // We should try to recover here
231 fprintf( stderr, "We have a problem houston...\n" );
232 }
233 else
234 {
235 // Calculate the size of the dv frame
236 size = height == 576 ? frame_size_625_50 : frame_size_525_60;
237 }
238
239 // Process the frame
240 if ( size != 0 )
241 {
242 // Encode the image
243 dv_encode_full_frame( encoder, &image, e_dv_color_yuv, dv_frame );
244 }
245 }
246 else if ( encoder != NULL )
247 {
248 // Calculate the size of the dv frame (duplicate of previous)
249 size = height == 576 ? frame_size_625_50 : frame_size_525_60;
250 }
251
252 return size;
253 }
254
255 /** The libdv encode audio method.
256 */
257
258 static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
259 {
260 // Get the properties of the consumer
261 mlt_properties this_properties = mlt_consumer_properties( this );
262
263 // Get the properties of the frame
264 mlt_properties frame_properties = mlt_frame_properties( frame );
265
266 // Obtain the dv_encoder
267 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
268
269 // Only continue if we have an encoder
270 if ( encoder != NULL )
271 {
272 // Get the frame count
273 int count = mlt_properties_get_int( this_properties, "count" );
274
275 // Default audio args
276 mlt_audio_format fmt = mlt_audio_pcm;
277 int channels = 2;
278 int frequency = 48000;
279 int samples = mlt_sample_calculator( mlt_properties_get_double( this_properties, "fps" ), frequency, count );
280 int16_t *pcm = NULL;
281
282 // Get the frame number
283 time_t start = time( NULL );
284 int height = mlt_properties_get_int( this_properties, "height" );
285 int is_pal = height == 576;
286 int is_wide = mlt_properties_get_double( frame_properties, "fps" ) == ( ( double ) 16.0 / 9.0 );
287
288 // Temporary - audio buffer allocation
289 int16_t *audio_buffers[ 4 ];
290 int i = 0;
291 int j = 0;
292 for ( i = 0 ; i < 4; i ++ )
293 audio_buffers[ i ] = mlt_pool_alloc( 2 * DV_AUDIO_MAX_SAMPLES );
294
295 // Get the audio
296 mlt_frame_get_audio( frame, &pcm, &fmt, &frequency, &channels, &samples );
297
298 // Inform the encoder of the number of audio samples
299 encoder->samples_this_frame = samples;
300
301 // Fill the audio buffers correctly
302 if ( mlt_properties_get_double( frame_properties, "_speed" ) == 1.0 )
303 {
304 for ( i = 0; i < samples; i ++ )
305 for ( j = 0; j < channels; j++ )
306 audio_buffers[ j ][ i ] = *pcm ++;
307 }
308
309 // Encode audio on frame
310 dv_encode_full_audio( encoder, audio_buffers, channels, frequency, dv_frame );
311
312 // Specify meta data on the frame
313 dv_encode_metadata( dv_frame, is_pal, is_wide, &start, count );
314 dv_encode_timecode( dv_frame, is_pal, count );
315
316 // Update properties
317 mlt_properties_set_int( this_properties, "count", ++ count );
318
319 // Temporary - free audio buffers
320 for ( i = 0 ; i < 4; i ++ )
321 mlt_pool_release( audio_buffers[ i ] );
322 }
323 }
324
325 /** The libdv output method.
326 */
327
328 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame )
329 {
330 // Get the properties
331 mlt_properties properties = mlt_consumer_properties( this );
332
333 FILE *output = stdout;
334 char *target = mlt_properties_get( properties, "target" );
335
336 if ( target != NULL )
337 {
338 output = mlt_properties_get_data( properties, "output_file", NULL );
339 if ( output == NULL )
340 {
341 output = fopen( target, "w" );
342 if ( output != NULL )
343 mlt_properties_set_data( properties, "output_file", output, 0, ( mlt_destructor )fclose, 0 );
344 }
345 }
346
347 if ( output != NULL )
348 {
349 fwrite( dv_frame, size, 1, output );
350 fflush( output );
351 }
352 else
353 {
354 fprintf( stderr, "Unable to open %s\n", target );
355 }
356 }
357
358 /** The main thread - the argument is simply the consumer.
359 */
360
361 static void *consumer_thread( void *arg )
362 {
363 // Map the argument to the object
364 mlt_consumer this = arg;
365
366 // Get the properties
367 mlt_properties properties = mlt_consumer_properties( this );
368
369 // Get the handling methods
370 int ( *video )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "video", NULL );
371 int ( *audio )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "audio", NULL );
372 int ( *output )( mlt_consumer, uint8_t *, int, mlt_frame ) = mlt_properties_get_data( properties, "output", NULL );
373
374 // Allocate a single PAL frame for encoding
375 uint8_t *dv_frame = mlt_pool_alloc( frame_size_625_50 );
376
377 // Frame and size
378 mlt_frame frame = NULL;
379 int size = 0;
380
381 // Loop while running
382 while( mlt_properties_get_int( properties, "running" ) )
383 {
384 // Get the frame
385 frame = mlt_consumer_rt_frame( this );
386
387 // Check that we have a frame to work with
388 if ( frame != NULL )
389 {
390 // Obtain the dv_encoder
391 if ( libdv_get_encoder( this, frame ) != NULL )
392 {
393 // Encode the image
394 size = video( this, dv_frame, frame );
395
396 // Encode the audio
397 if ( size > 0 )
398 audio( this, dv_frame, frame );
399
400 // Output the frame
401 output( this, dv_frame, size, frame );
402
403 // Close the frame
404 mlt_frame_close( frame );
405 }
406 else
407 {
408 fprintf( stderr, "Unable to obtain dv encoder.\n" );
409 }
410 }
411 }
412
413 // Tidy up
414 mlt_pool_release( dv_frame );
415
416 return NULL;
417 }
418
419 /** Close the consumer.
420 */
421
422 static void consumer_close( mlt_consumer this )
423 {
424 // Stop the consumer
425 mlt_consumer_stop( this );
426
427 // Close the parent
428 mlt_consumer_close( this );
429
430 // Free the memory
431 free( this );
432 }