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>
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.
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.
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.
22 #include "consumer_libdv.h"
25 #include <framework/mlt_frame.h>
27 // System header files
36 // Forward references.
37 static int consumer_encode_video( mlt_consumer
this, uint8_t *dv_frame
, mlt_frame frame
);
38 static void consumer_encode_audio( mlt_consumer
this, uint8_t *dv_frame
, mlt_frame frame
);
39 static void consumer_output( mlt_consumer
this, uint8_t *dv_frame
, int size
, mlt_frame frame
);
40 static void *consumer_thread( void *arg
);
41 static void consumer_close( mlt_consumer
this );
43 /** Initialise the dv consumer.
46 mlt_consumer
consumer_libdv_init( char *arg
)
48 // Allocate the consumer
49 mlt_consumer
this = calloc( 1, sizeof( struct mlt_consumer_s
) );
51 // If memory allocated and initialises without error
52 if ( this != NULL
&& mlt_consumer_init( this, NULL
) == 0 )
54 // Get properties from the consumer
55 mlt_properties properties
= mlt_consumer_properties( this );
58 pthread_t
*thread
= calloc( 1, sizeof( pthread_t
) );
60 // Assign close callback
61 this->close
= consumer_close
;
63 // Assign all properties
64 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
65 mlt_properties_set_double( properties
, "fps", 25 );
67 mlt_properties_set_double( properties
, "fps", 29.97 );
69 mlt_properties_set_data( properties
, "thread", thread
, sizeof( pthread_t
), free
, NULL
);
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
);
74 // Create the thread (this should not happen immediately)
75 mlt_properties_set_int( properties
, "running", 1 );
76 pthread_create( thread
, NULL
, consumer_thread
, this );
80 // Clean up in case of init failure
89 /** Get or create a new libdv encoder.
92 static dv_encoder_t
*libdv_get_encoder( mlt_consumer
this, mlt_frame frame
)
94 // Get the properties of the consumer
95 mlt_properties this_properties
= mlt_consumer_properties( this );
97 // Obtain the dv_encoder
98 dv_encoder_t
*encoder
= mlt_properties_get_data( this_properties
, "dv_encoder", NULL
);
100 // Construct one if we don't have one
101 if ( encoder
== NULL
)
103 // Get the fps of the consumer (for now - should be from frame)
104 double fps
= mlt_properties_get_double( this_properties
, "fps" );
106 // Create the encoder
107 encoder
= dv_encoder_new( fps
!= 25, 0, 0 );
110 encoder
->isPAL
= fps
= 25;
112 encoder
->vlc_encode_passes
= 1;
113 encoder
->static_qno
= 0;
114 encoder
->force_dct
= DV_DCT_AUTO
;
116 // Store the encoder on the properties
117 mlt_properties_set_data( this_properties
, "dv_encoder", encoder
, 0, ( mlt_destructor
)dv_encoder_free
, NULL
);
119 // Convenience for image dimensions
120 mlt_properties_set_int( this_properties
, "width", 720 );
121 mlt_properties_set_int( this_properties
, "height", fps
== 25 ?
576 : 480 );
124 // Return the encoder
129 /** The libdv encode video method.
132 static int consumer_encode_video( mlt_consumer
this, uint8_t *dv_frame
, mlt_frame frame
)
134 // Obtain the dv_encoder
135 dv_encoder_t
*encoder
= libdv_get_encoder( this, frame
);
137 // Get the properties of the consumer
138 mlt_properties this_properties
= mlt_consumer_properties( this );
140 // This will hold the size of the dv frame
143 // If we get an encoder, then encode the image
144 if ( encoder
!= NULL
)
146 // Specify desired image properties
147 mlt_image_format fmt
= mlt_image_yuv422
;
148 int width
= mlt_properties_get_int( this_properties
, "width" );
149 int height
= mlt_properties_get_int( this_properties
, "height" );
150 uint8_t *image
= NULL
;
153 mlt_frame_get_image( frame
, &image
, &fmt
, &width
, &height
, 0 );
155 // Check that we get what we expected
156 if ( fmt
!= mlt_image_yuv422
||
157 width
!= mlt_properties_get_int( this_properties
, "width" ) ||
158 height
!= mlt_properties_get_int( this_properties
, "height" ) ||
161 // We should try to recover here
162 fprintf( stderr
, "We have a problem houston...\n" );
166 // Calculate the size of the dv frame
167 size
= height
== 576 ? frame_size_625_50
: frame_size_525_60
;
174 dv_encode_full_frame( encoder
, &image
, e_dv_color_yuv
, dv_frame
);
181 /** The libdv encode audio method.
184 static void consumer_encode_audio( mlt_consumer
this, uint8_t *dv_frame
, mlt_frame frame
)
186 // Get the properties of the consumer
187 mlt_properties this_properties
= mlt_consumer_properties( this );
189 // Obtain the dv_encoder
190 dv_encoder_t
*encoder
= libdv_get_encoder( this, frame
);
192 // Only continue if we have an encoder
193 if ( encoder
!= NULL
)
195 // Get the frame count
196 int count
= mlt_properties_get_int( this_properties
, "count" );
198 // Default audio args
199 mlt_audio_format fmt
= mlt_audio_pcm
;
201 int frequency
= 48000;
202 int samples
= mlt_sample_calculator( mlt_properties_get_double( this_properties
, "fps" ), frequency
, count
);
205 // Get the frame number
206 time_t start
= time( NULL
);
207 int height
= mlt_properties_get_int( this_properties
, "height" );
208 int is_pal
= height
== 576;
211 // Temporary - audio buffer allocation
212 int16_t *audio_buffers
[ 4 ];
215 for ( i
= 0 ; i
< 4; i
++ )
216 audio_buffers
[ i
] = malloc( 2 * DV_AUDIO_MAX_SAMPLES
);
219 mlt_frame_get_audio( frame
, &pcm
, &fmt
, &frequency
, &channels
, &samples
);
221 // Inform the encoder of the number of audio samples
222 encoder
->samples_this_frame
= samples
;
224 // Fill the audio buffers correctly
225 for ( i
= 0; i
< samples
; i
++ )
226 for ( j
= 0; j
< channels
; j
++ )
227 audio_buffers
[ j
][ i
] = *pcm
++;
229 // Encode audio on frame
230 dv_encode_full_audio( encoder
, audio_buffers
, channels
, frequency
, dv_frame
);
232 // Specify meta data on the frame
233 dv_encode_metadata( dv_frame
, is_pal
, is_wide
, &start
, count
);
234 dv_encode_timecode( dv_frame
, is_pal
, count
);
237 mlt_properties_set_int( this_properties
, "count", ++ count
);
239 // Temporary - free audio buffers
240 for ( i
= 0 ; i
< 4; i
++ )
241 free( audio_buffers
[ i
] );
245 /** The libdv output method.
248 static void consumer_output( mlt_consumer
this, uint8_t *dv_frame
, int size
, mlt_frame frame
)
250 fwrite( dv_frame
, size
, 1, stdout
);
254 /** The main thread - the argument is simply the consumer.
257 static void *consumer_thread( void *arg
)
259 // Map the argument to the object
260 mlt_consumer
this = arg
;
262 // Get the properties
263 mlt_properties properties
= mlt_consumer_properties( this );
265 // Get the handling methods
266 int ( *video
)( mlt_consumer
, uint8_t *, mlt_frame
) = mlt_properties_get_data( properties
, "video", NULL
);
267 int ( *audio
)( mlt_consumer
, uint8_t *, mlt_frame
) = mlt_properties_get_data( properties
, "audio", NULL
);
268 int ( *output
)( mlt_consumer
, uint8_t *, int, mlt_frame
) = mlt_properties_get_data( properties
, "output", NULL
);
270 // Allocate a single PAL frame for encoding
271 uint8_t *dv_frame
= malloc( frame_size_625_50
);
273 // Get the service associated to the consumer
274 mlt_service service
= mlt_consumer_service( this );
276 // Define a frame pointer
279 // Loop while running
280 while( mlt_properties_get_int( properties
, "running" ) )
283 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
286 int size
= video( this, dv_frame
, frame
);
290 audio( this, dv_frame
, frame
);
293 output( this, dv_frame
, size
, frame
);
296 mlt_frame_close( frame
);
306 /** Close the consumer.
309 static void consumer_close( mlt_consumer
this )
311 // Get the properties
312 mlt_properties properties
= mlt_consumer_properties( this );
315 pthread_t
*thread
= mlt_properties_get_data( properties
, "thread", NULL
);
318 mlt_properties_set_int( properties
, "running", 0 );
320 // Wait for termination
321 pthread_join( *thread
, NULL
);
324 mlt_consumer_close( this );