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