fa1a3260fb41759ab1b768c59736de8a307c8ddc
[melted] / mlt / 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_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 );
42
43 /** Initialise the dv consumer.
44 */
45
46 mlt_consumer consumer_libdv_init( char *arg )
47 {
48 // Allocate the consumer
49 mlt_consumer this = calloc( 1, sizeof( struct mlt_consumer_s ) );
50
51 // If memory allocated and initialises without error
52 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
53 {
54 // Get properties from the consumer
55 mlt_properties properties = mlt_consumer_properties( this );
56
57 // Allocate a thread
58 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
59
60 // Assign close callback
61 this->close = consumer_close;
62
63 // Assign all properties
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 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 );
73
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 );
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 /** Get or create a new libdv encoder.
90 */
91
92 static dv_encoder_t *libdv_get_encoder( mlt_consumer this, mlt_frame frame )
93 {
94 // Get the properties of the consumer
95 mlt_properties this_properties = mlt_consumer_properties( this );
96
97 // Obtain the dv_encoder
98 dv_encoder_t *encoder = mlt_properties_get_data( this_properties, "dv_encoder", NULL );
99
100 // Construct one if we don't have one
101 if ( encoder == NULL )
102 {
103 // Get the fps of the consumer (for now - should be from frame)
104 double fps = mlt_properties_get_double( this_properties, "fps" );
105
106 // Create the encoder
107 encoder = dv_encoder_new( 0, 0, 0 );
108
109 // Encoder settings
110 encoder->isPAL = fps == 25;
111 encoder->is16x9 = 0;
112 encoder->vlc_encode_passes = 1;
113 encoder->static_qno = 0;
114 encoder->force_dct = DV_DCT_AUTO;
115
116 // Store the encoder on the properties
117 mlt_properties_set_data( this_properties, "dv_encoder", encoder, 0, ( mlt_destructor )dv_encoder_free, NULL );
118
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 );
122 }
123
124 // Return the encoder
125 return encoder;
126 }
127
128
129 /** The libdv encode video method.
130 */
131
132 static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
133 {
134 // Obtain the dv_encoder
135 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
136
137 // Get the properties of the consumer
138 mlt_properties this_properties = mlt_consumer_properties( this );
139
140 // This will hold the size of the dv frame
141 int size = 0;
142
143 // If we get an encoder, then encode the image
144 if ( encoder != NULL )
145 {
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;
151
152 // Get the image
153 mlt_frame_get_image( frame, &image, &fmt, &width, &height, 0 );
154
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" ) ||
159 image == NULL )
160 {
161 // We should try to recover here
162 fprintf( stderr, "We have a problem houston...\n" );
163 }
164 else
165 {
166 // Calculate the size of the dv frame
167 size = height == 576 ? frame_size_625_50 : frame_size_525_60;
168 }
169
170 // Process the frame
171 if ( size != 0 )
172 {
173 // Encode the image
174 dv_encode_full_frame( encoder, &image, e_dv_color_yuv, dv_frame );
175 }
176 }
177
178 return size;
179 }
180
181 /** The libdv encode audio method.
182 */
183
184 static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
185 {
186 // Get the properties of the consumer
187 mlt_properties this_properties = mlt_consumer_properties( this );
188
189 // Obtain the dv_encoder
190 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
191
192 // Only continue if we have an encoder
193 if ( encoder != NULL )
194 {
195 // Get the frame count
196 int count = mlt_properties_get_int( this_properties, "count" );
197
198 // Default audio args
199 mlt_audio_format fmt = mlt_audio_pcm;
200 int channels = 2;
201 int frequency = 48000;
202 int samples = mlt_sample_calculator( mlt_properties_get_double( this_properties, "fps" ), frequency, count );
203 int16_t *pcm = NULL;
204
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;
209 int is_wide = 0;
210
211 // Temporary - audio buffer allocation
212 int16_t *audio_buffers[ 4 ];
213 int i = 0;
214 int j = 0;
215 for ( i = 0 ; i < 4; i ++ )
216 audio_buffers[ i ] = malloc( 2 * DV_AUDIO_MAX_SAMPLES );
217
218 // Get the audio
219 mlt_frame_get_audio( frame, &pcm, &fmt, &frequency, &channels, &samples );
220
221 // Inform the encoder of the number of audio samples
222 encoder->samples_this_frame = samples;
223
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 ++;
228
229 // Encode audio on frame
230 dv_encode_full_audio( encoder, audio_buffers, channels, frequency, dv_frame );
231
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 );
235
236 // Update properties
237 mlt_properties_set_int( this_properties, "count", ++ count );
238
239 // Temporary - free audio buffers
240 for ( i = 0 ; i < 4; i ++ )
241 free( audio_buffers[ i ] );
242 }
243 }
244
245 /** The libdv output method.
246 */
247
248 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame )
249 {
250 fwrite( dv_frame, size, 1, stdout );
251 fflush( stdout );
252 }
253
254 /** The main thread - the argument is simply the consumer.
255 */
256
257 static void *consumer_thread( void *arg )
258 {
259 // Map the argument to the object
260 mlt_consumer this = arg;
261
262 // Get the properties
263 mlt_properties properties = mlt_consumer_properties( this );
264
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 );
269
270 // Allocate a single PAL frame for encoding
271 uint8_t *dv_frame = malloc( frame_size_625_50 );
272
273 // Get the service associated to the consumer
274 mlt_service service = mlt_consumer_service( this );
275
276 // Define a frame pointer
277 mlt_frame frame;
278
279 // Loop while running
280 while( mlt_properties_get_int( properties, "running" ) )
281 {
282 // Get the frame
283 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
284 {
285 // Encode the image
286 int size = video( this, dv_frame, frame );
287
288 // Encode the audio
289 if ( size > 0 )
290 audio( this, dv_frame, frame );
291
292 // Output the frame
293 output( this, dv_frame, size, frame );
294
295 // Close the frame
296 mlt_frame_close( frame );
297 }
298 }
299
300 // Tidy up
301 free( dv_frame );
302
303 return NULL;
304 }
305
306 /** Close the consumer.
307 */
308
309 static void consumer_close( mlt_consumer this )
310 {
311 // Get the properties
312 mlt_properties properties = mlt_consumer_properties( this );
313
314 // Get the thread
315 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
316
317 // Stop the thread
318 mlt_properties_set_int( properties, "running", 0 );
319
320 // Wait for termination
321 pthread_join( *thread, NULL );
322
323 // Close the parent
324 mlt_consumer_close( this );
325
326 // Free the memory
327 free( this );
328 }
329