3b576387bd4144530253ae4477cf66321903fb29
[melted] / src / modules / dv / consumer_libdv.c
1 /*
2 * producer_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
102 // Assign the thread to properties
103 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
104
105 // Set the running state
106 mlt_properties_set_int( properties, "running", 1 );
107
108 // Create the thread
109 pthread_create( thread, NULL, consumer_thread, this );
110 }
111 return 0;
112 }
113
114 /** Stop the consumer.
115 */
116
117 static int consumer_stop( mlt_consumer this )
118 {
119 // Get the properties
120 mlt_properties properties = mlt_consumer_properties( this );
121
122 // Check that we're running
123 if ( mlt_properties_get_int( properties, "running" ) )
124 {
125 // Get the thread
126 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
127
128 // Stop the thread
129 mlt_properties_set_int( properties, "running", 0 );
130
131 // Wait for termination
132 pthread_join( *thread, NULL );
133
134 // Close the output file :-) - this is obtuse - doesn't matter if output file
135 // exists or not - the destructor will kick in if it does
136 mlt_properties_set_data( properties, "output_file", NULL, 0, NULL, NULL );
137 }
138
139 return 0;
140 }
141
142 /** Determine if the consumer is stopped.
143 */
144
145 static int consumer_is_stopped( mlt_consumer this )
146 {
147 // Get the properties
148 mlt_properties properties = mlt_consumer_properties( this );
149 return !mlt_properties_get_int( properties, "running" );
150 }
151
152 /** Get or create a new libdv encoder.
153 */
154
155 static dv_encoder_t *libdv_get_encoder( mlt_consumer this, mlt_frame frame )
156 {
157 // Get the properties of the consumer
158 mlt_properties this_properties = mlt_consumer_properties( this );
159
160 // Obtain the dv_encoder
161 dv_encoder_t *encoder = mlt_properties_get_data( this_properties, "dv_encoder", NULL );
162
163 // Construct one if we don't have one
164 if ( encoder == NULL )
165 {
166 // Get the fps of the consumer (for now - should be from frame)
167 double fps = mlt_properties_get_double( this_properties, "fps" );
168
169 // Create the encoder
170 encoder = dv_encoder_new( 0, 0, 0 );
171
172 // Encoder settings
173 encoder->isPAL = fps == 25;
174 encoder->is16x9 = 0;
175 encoder->vlc_encode_passes = 1;
176 encoder->static_qno = 0;
177 encoder->force_dct = DV_DCT_AUTO;
178
179 // Store the encoder on the properties
180 mlt_properties_set_data( this_properties, "dv_encoder", encoder, 0, ( mlt_destructor )dv_encoder_free, NULL );
181 }
182
183 // Return the encoder
184 return encoder;
185 }
186
187
188 /** The libdv encode video method.
189 */
190
191 static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
192 {
193 // Obtain the dv_encoder
194 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
195
196 // Get the properties of the consumer
197 mlt_properties this_properties = mlt_consumer_properties( this );
198
199 // This will hold the size of the dv frame
200 int size = 0;
201
202 // If we get an encoder, then encode the image
203 if ( encoder != NULL )
204 {
205 // Specify desired image properties
206 mlt_image_format fmt = mlt_image_yuv422;
207 int width = mlt_properties_get_int( this_properties, "width" );
208 int height = mlt_properties_get_int( this_properties, "height" );
209 uint8_t *image = NULL;
210 int is_test = 0;
211
212 // Get the image
213 mlt_frame_get_image( frame, &image, &fmt, &width, &height, 0 );
214
215 // determine if this a test card
216 is_test = mlt_frame_is_test_card( frame );
217
218 // Check that we get what we expected
219 if ( fmt != mlt_image_yuv422 ||
220 width != mlt_properties_get_int( this_properties, "width" ) ||
221 height != mlt_properties_get_int( this_properties, "height" ) ||
222 image == NULL )
223 {
224 // We should try to recover here
225 fprintf( stderr, "We have a problem houston...\n" );
226 }
227 else
228 {
229 // Calculate the size of the dv frame
230 size = height == 576 ? frame_size_625_50 : frame_size_525_60;
231 }
232
233 // Process the frame
234 if ( size != 0 && !( mlt_properties_get_int( this_properties, "was_test_card" ) && is_test ) )
235 {
236 if ( mlt_properties_get_int( mlt_frame_properties( frame ), "top_field_first" ) )
237 image += width * 2;
238
239 // Encode the image
240 dv_encode_full_frame( encoder, &image, e_dv_color_yuv, dv_frame );
241
242 // Note test card status
243 mlt_properties_set_int( this_properties, "was_test_card", is_test );
244 }
245 }
246
247 return size;
248 }
249
250 /** The libdv encode audio method.
251 */
252
253 static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
254 {
255 // Get the properties of the consumer
256 mlt_properties this_properties = mlt_consumer_properties( this );
257
258 // Get the properties of the frame
259 mlt_properties frame_properties = mlt_frame_properties( frame );
260
261 // Obtain the dv_encoder
262 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
263
264 // Only continue if we have an encoder
265 if ( encoder != NULL )
266 {
267 // Get the frame count
268 int count = mlt_properties_get_int( this_properties, "count" );
269
270 // Default audio args
271 mlt_audio_format fmt = mlt_audio_pcm;
272 int channels = 2;
273 int frequency = 48000;
274 int samples = mlt_sample_calculator( mlt_properties_get_double( this_properties, "fps" ), frequency, count );
275 int16_t *pcm = NULL;
276
277 // Get the frame number
278 time_t start = time( NULL );
279 int height = mlt_properties_get_int( this_properties, "height" );
280 int is_pal = height == 576;
281 int is_wide = mlt_properties_get_double( frame_properties, "fps" ) == ( ( double ) 16.0 / 9.0 );
282
283 // Temporary - audio buffer allocation
284 int16_t *audio_buffers[ 4 ];
285 int i = 0;
286 int j = 0;
287 for ( i = 0 ; i < 4; i ++ )
288 audio_buffers[ i ] = calloc( 1, 2 * DV_AUDIO_MAX_SAMPLES );
289
290 // Get the audio
291 mlt_frame_get_audio( frame, &pcm, &fmt, &frequency, &channels, &samples );
292
293 // Inform the encoder of the number of audio samples
294 encoder->samples_this_frame = samples;
295
296 // Fill the audio buffers correctly
297 if ( mlt_properties_get_double( frame_properties, "_speed" ) == 1.0 )
298 {
299 for ( i = 0; i < samples; i ++ )
300 for ( j = 0; j < channels; j++ )
301 audio_buffers[ j ][ i ] = *pcm ++;
302 }
303
304 // Encode audio on frame
305 dv_encode_full_audio( encoder, audio_buffers, channels, frequency, dv_frame );
306
307 // Specify meta data on the frame
308 dv_encode_metadata( dv_frame, is_pal, is_wide, &start, count );
309 dv_encode_timecode( dv_frame, is_pal, count );
310
311 // Update properties
312 mlt_properties_set_int( this_properties, "count", ++ count );
313
314 // Temporary - free audio buffers
315 for ( i = 0 ; i < 4; i ++ )
316 free( audio_buffers[ i ] );
317 }
318 }
319
320 /** The libdv output method.
321 */
322
323 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame )
324 {
325 // Get the properties
326 mlt_properties properties = mlt_consumer_properties( this );
327
328 FILE *output = stdout;
329 char *target = mlt_properties_get( properties, "target" );
330
331 if ( target != NULL )
332 {
333 output = mlt_properties_get_data( properties, "output_file", NULL );
334 if ( output == NULL )
335 {
336 output = fopen( target, "w" );
337 if ( output != NULL )
338 mlt_properties_set_data( properties, "output_file", output, 0, ( mlt_destructor )fclose, 0 );
339 }
340 }
341
342 if ( output != NULL )
343 {
344 fwrite( dv_frame, size, 1, output );
345 fflush( output );
346 }
347 else
348 {
349 fprintf( stderr, "Unable to open %s\n", target );
350 }
351 }
352
353 /** The main thread - the argument is simply the consumer.
354 */
355
356 static void *consumer_thread( void *arg )
357 {
358 // Map the argument to the object
359 mlt_consumer this = arg;
360
361 // Get the properties
362 mlt_properties properties = mlt_consumer_properties( this );
363
364 // Get the handling methods
365 int ( *video )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "video", NULL );
366 int ( *audio )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "audio", NULL );
367 int ( *output )( mlt_consumer, uint8_t *, int, mlt_frame ) = mlt_properties_get_data( properties, "output", NULL );
368
369 // Allocate a single PAL frame for encoding
370 uint8_t *dv_frame = malloc( frame_size_625_50 );
371
372 // Loop while running
373 while( mlt_properties_get_int( properties, "running" ) )
374 {
375 // Get the frame
376 mlt_frame frame = mlt_consumer_get_frame( this );
377
378 // Check that we have a frame to work with
379 if ( frame != NULL )
380 {
381 // Obtain the dv_encoder
382 if ( libdv_get_encoder( this, frame ) != NULL )
383 {
384 // Encode the image
385 int size = video( this, dv_frame, frame );
386
387 // Encode the audio
388 if ( size > 0 )
389 audio( this, dv_frame, frame );
390
391 // Output the frame
392 output( this, dv_frame, size, frame );
393
394 // Close the frame
395 mlt_frame_close( frame );
396 }
397 else
398 {
399 fprintf( stderr, "Unable to obtain dv encoder.\n" );
400 }
401 }
402 }
403
404 // Tidy up
405 free( dv_frame );
406
407 return NULL;
408 }
409
410 /** Close the consumer.
411 */
412
413 static void consumer_close( mlt_consumer this )
414 {
415 // Stop the consumer
416 mlt_consumer_stop( this );
417
418 // Close the parent
419 mlt_consumer_close( this );
420
421 // Free the memory
422 free( this );
423 }
424