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