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