]> git.sesse.net Git - mlt/blob - src/modules/dv/consumer_libdv.c
3a9949c277ed7683dbc39ae8d7af9df3c6106887
[mlt] / 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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 // mlt Header files
22 #include <framework/mlt_consumer.h>
23 #include <framework/mlt_frame.h>
24
25 // System header files
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30
31 // libdv header files
32 #include <libdv/dv.h>
33
34 #define FRAME_SIZE_525_60       10 * 150 * 80
35 #define FRAME_SIZE_625_50       12 * 150 * 80
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( mlt_profile profile, mlt_service_type type, const char *id, 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, profile ) == 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                 // Terminate at end of the stream by default
74                 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
75
76                 // Set up start/stop/terminated callbacks
77                 this->start = consumer_start;
78                 this->stop = consumer_stop;
79                 this->is_stopped = consumer_is_stopped;
80         }
81         else
82         {
83                 // Clean up in case of init failure
84                 free( this );
85                 this = NULL;
86         }
87
88         // Return this
89         return this;
90 }
91
92 /** Start the consumer.
93 */
94
95 static int consumer_start( mlt_consumer this )
96 {
97         // Get the properties
98         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
99
100         // Check that we're not already running
101         if ( !mlt_properties_get_int( properties, "running" ) )
102         {
103                 // Allocate a thread
104                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
105
106                 // Assign the thread to properties
107                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
108
109                 // Set the running state
110                 mlt_properties_set_int( properties, "running", 1 );
111
112                 // Create the thread
113                 pthread_create( thread, NULL, consumer_thread, this );
114         }
115         return 0;
116 }
117
118 /** Stop the consumer.
119 */
120
121 static int consumer_stop( mlt_consumer this )
122 {
123         // Get the properties
124         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
125
126         // Check that we're running
127         if ( mlt_properties_get_int( properties, "running" ) )
128         {
129                 // Get the thread
130                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
131
132                 // Stop the thread
133                 mlt_properties_set_int( properties, "running", 0 );
134
135                 // Wait for termination
136                 pthread_join( *thread, NULL );
137
138                 // Close the output file :-) - this is obtuse - doesn't matter if output file
139                 // exists or not - the destructor will kick in if it does
140                 mlt_properties_set_data( properties, "output_file", NULL, 0, NULL, NULL );
141         }
142
143         return 0;
144 }
145
146 /** Determine if the consumer is stopped.
147 */
148
149 static int consumer_is_stopped( mlt_consumer this )
150 {
151         // Get the properties
152         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
153         return !mlt_properties_get_int( properties, "running" );
154 }
155
156 /** Get or create a new libdv encoder.
157 */
158
159 static dv_encoder_t *libdv_get_encoder( mlt_consumer this, mlt_frame frame )
160 {
161         // Get the properties of the consumer
162         mlt_properties this_properties = MLT_CONSUMER_PROPERTIES( this );
163
164         // Obtain the dv_encoder
165         dv_encoder_t *encoder = mlt_properties_get_data( this_properties, "dv_encoder", NULL );
166
167         // Construct one if we don't have one
168         if ( encoder == NULL )
169         {
170                 // Get the fps of the consumer (for now - should be from frame)
171                 double fps = mlt_properties_get_double( this_properties, "fps" );
172
173                 // Create the encoder
174                 encoder = dv_encoder_new( 0, 0, 0 );
175
176                 // Encoder settings
177                 encoder->isPAL = fps == 25;
178                 encoder->is16x9 = 0;
179                 encoder->vlc_encode_passes = 1;
180                 encoder->static_qno = 0;
181                 encoder->force_dct = DV_DCT_AUTO;
182
183                 // Store the encoder on the properties
184                 mlt_properties_set_data( this_properties, "dv_encoder", encoder, 0, ( mlt_destructor )dv_encoder_free, NULL );
185         }
186
187         // Return the encoder
188         return encoder;
189 }
190
191
192 /** The libdv encode video method.
193 */
194
195 static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
196 {
197         // Obtain the dv_encoder
198         dv_encoder_t *encoder = libdv_get_encoder( this, frame );
199
200         // Get the properties of the consumer
201         mlt_properties this_properties = MLT_CONSUMER_PROPERTIES( this );
202
203         // This will hold the size of the dv frame
204         int size = 0;
205
206         // Is the image rendered
207         int rendered = mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" );
208
209         // Get width and height
210         int width = mlt_properties_get_int( this_properties, "width" );
211         int height = mlt_properties_get_int( this_properties, "height" );
212
213         // If we get an encoder, then encode the image
214         if ( rendered && encoder != NULL )
215         {
216                 // Specify desired image properties
217                 mlt_image_format fmt = mlt_image_yuv422;
218                 uint8_t *image = NULL;
219
220                 // Get the image
221                 mlt_frame_get_image( frame, &image, &fmt, &width, &height, 0 );
222
223                 // Check that we get what we expected
224                 if ( fmt != mlt_image_yuv422 || 
225                          width != mlt_properties_get_int( this_properties, "width" ) ||
226                          height != mlt_properties_get_int( this_properties, "height" ) ||
227                          image == NULL )
228                 {
229                         // We should try to recover here
230                         fprintf( stderr, "We have a problem houston...\n" );
231                 }
232                 else
233                 {
234                         // Calculate the size of the dv frame
235                         size = height == 576 ? FRAME_SIZE_625_50 : FRAME_SIZE_525_60;
236                 }
237
238                 // Process the frame
239                 if ( size != 0 )
240                 {
241                         // Encode the image
242                         dv_encode_full_frame( encoder, &image, e_dv_color_yuv, dv_frame );
243                 }
244                 mlt_events_fire( this_properties, "consumer-frame-show", frame, NULL );
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_s16;
277                 int channels = 2;
278                 int frequency = mlt_properties_get_int( this_properties, "frequency" );
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_int( this_properties, "display_aspect_num" ) == 16;
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, (void**) &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                 else
309                 {
310                         for ( j = 0; j < channels; j++ )
311                                 memset( audio_buffers[ j ], 0, 2 * DV_AUDIO_MAX_SAMPLES );
312                 }
313
314                 // Encode audio on frame
315                 dv_encode_full_audio( encoder, audio_buffers, channels, frequency, dv_frame );
316
317                 // Specify meta data on the frame
318                 dv_encode_metadata( dv_frame, is_pal, is_wide, &start, count );
319                 dv_encode_timecode( dv_frame, is_pal, count );
320
321                 // Update properties
322                 mlt_properties_set_int( this_properties, "count", ++ count );
323
324                 // Temporary - free audio buffers
325                 for ( i = 0 ; i < 4; i ++ )
326                         mlt_pool_release( audio_buffers[ i ] );
327         }
328 }
329
330 /** The libdv output method.
331 */
332
333 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame )
334 {
335         // Get the properties
336         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
337
338         FILE *output = stdout;
339         char *target = mlt_properties_get( properties, "target" );
340
341         if ( target != NULL )
342         {
343                 output = mlt_properties_get_data( properties, "output_file", NULL );
344                 if ( output == NULL )
345                 {
346                         output = fopen( target, "w" );
347                         if ( output != NULL )
348                                 mlt_properties_set_data( properties, "output_file", output, 0, ( mlt_destructor )fclose, 0 );
349                 }
350         }
351
352         if ( output != NULL )
353         {
354                 fwrite( dv_frame, size, 1, output );
355                 fflush( output );
356         }
357         else
358         {
359                 fprintf( stderr, "Unable to open %s\n", target );
360         }
361 }
362
363 /** The main thread - the argument is simply the consumer.
364 */
365
366 static void *consumer_thread( void *arg )
367 {
368         // Map the argument to the object
369         mlt_consumer this = arg;
370
371         // Get the properties
372         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
373
374         // Get the terminate_on_pause property
375         int top = mlt_properties_get_int( properties, "terminate_on_pause" );
376
377         // Get the handling methods
378         int ( *video )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "video", NULL );
379         int ( *audio )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "audio", NULL );
380         int ( *output )( mlt_consumer, uint8_t *, int, mlt_frame ) = mlt_properties_get_data( properties, "output", NULL );
381
382         // Allocate a single PAL frame for encoding
383         uint8_t *dv_frame = mlt_pool_alloc( FRAME_SIZE_625_50 );
384
385         // Frame and size
386         mlt_frame frame = NULL;
387         int size = 0;
388
389         // Loop while running
390         while( mlt_properties_get_int( properties, "running" ) )
391         {
392                 // Get the frame
393                 frame = mlt_consumer_rt_frame( this );
394
395                 // Check that we have a frame to work with
396                 if ( frame != NULL )
397                 {
398                         // Terminate on pause
399                         if ( top && mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0 )
400                         {
401                                 mlt_frame_close( frame );
402                                 break;
403                         }
404
405                         // Obtain the dv_encoder
406                         if ( libdv_get_encoder( this, frame ) != NULL )
407                         {
408                                 // Encode the image
409                                 size = video( this, dv_frame, frame );
410
411                                 // Encode the audio
412                                 if ( size > 0 )
413                                         audio( this, dv_frame, frame );
414
415                                 // Output the frame
416                                 output( this, dv_frame, size, frame );
417
418                                 // Close the frame
419                                 mlt_frame_close( frame );
420                         }
421                         else
422                         {
423                                 fprintf( stderr, "Unable to obtain dv encoder.\n" );
424                         }
425                 }
426         }
427
428         // Tidy up
429         mlt_pool_release( dv_frame );
430
431         mlt_consumer_stopped( this );
432
433         return NULL;
434 }
435
436 /** Close the consumer.
437 */
438
439 static void consumer_close( mlt_consumer this )
440 {
441         // Stop the consumer
442         mlt_consumer_stop( this );
443
444         // Close the parent
445         mlt_consumer_close( this );
446
447         // Free the memory
448         free( this );
449 }