8735ed8c7f31bc0bc055567ff7f1ee22e607243d
[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_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 // determine if this a test card
144 int is_test = mlt_frame_is_test_card( frame );
145
146 // If we get an encoder, then encode the image
147 if ( encoder != NULL )
148 {
149 // Specify desired image properties
150 mlt_image_format fmt = mlt_image_yuv422;
151 int width = mlt_properties_get_int( this_properties, "width" );
152 int height = mlt_properties_get_int( this_properties, "height" );
153 uint8_t *image = NULL;
154
155 // Get the image
156 mlt_frame_get_image( frame, &image, &fmt, &width, &height, 0 );
157
158 // Check that we get what we expected
159 if ( fmt != mlt_image_yuv422 ||
160 width != mlt_properties_get_int( this_properties, "width" ) ||
161 height != mlt_properties_get_int( this_properties, "height" ) ||
162 image == NULL )
163 {
164 // We should try to recover here
165 fprintf( stderr, "We have a problem houston...\n" );
166 }
167 else
168 {
169 // Calculate the size of the dv frame
170 size = height == 576 ? frame_size_625_50 : frame_size_525_60;
171 }
172
173 // Process the frame
174 if ( size != 0 && !( mlt_properties_get_int( this_properties, "was_test_card" ) && is_test ) )
175 {
176 // Encode the image
177 dv_encode_full_frame( encoder, &image, e_dv_color_yuv, dv_frame );
178
179 // Note test card status
180 mlt_properties_set_int( this_properties, "was_test_card", is_test );
181 }
182 }
183
184 return size;
185 }
186
187 /** The libdv encode audio method.
188 */
189
190 static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame )
191 {
192 // Get the properties of the consumer
193 mlt_properties this_properties = mlt_consumer_properties( this );
194
195 // Obtain the dv_encoder
196 dv_encoder_t *encoder = libdv_get_encoder( this, frame );
197
198 // Only continue if we have an encoder
199 if ( encoder != NULL )
200 {
201 // Get the frame count
202 int count = mlt_properties_get_int( this_properties, "count" );
203
204 // Default audio args
205 mlt_audio_format fmt = mlt_audio_pcm;
206 int channels = 2;
207 int frequency = 48000;
208 int samples = mlt_sample_calculator( mlt_properties_get_double( this_properties, "fps" ), frequency, count );
209 int16_t *pcm = NULL;
210
211 // Get the frame number
212 time_t start = time( NULL );
213 int height = mlt_properties_get_int( this_properties, "height" );
214 int is_pal = height == 576;
215 int is_wide = 0;
216
217 // Temporary - audio buffer allocation
218 int16_t *audio_buffers[ 4 ];
219 int i = 0;
220 int j = 0;
221 for ( i = 0 ; i < 4; i ++ )
222 audio_buffers[ i ] = malloc( 2 * DV_AUDIO_MAX_SAMPLES );
223
224 // Get the audio
225 mlt_frame_get_audio( frame, &pcm, &fmt, &frequency, &channels, &samples );
226
227 // Inform the encoder of the number of audio samples
228 encoder->samples_this_frame = samples;
229
230 // Fill the audio buffers correctly
231 for ( i = 0; i < samples; i ++ )
232 for ( j = 0; j < channels; j++ )
233 audio_buffers[ j ][ i ] = *pcm ++;
234
235 // Encode audio on frame
236 dv_encode_full_audio( encoder, audio_buffers, channels, frequency, dv_frame );
237
238 // Specify meta data on the frame
239 dv_encode_metadata( dv_frame, is_pal, is_wide, &start, count );
240 dv_encode_timecode( dv_frame, is_pal, count );
241
242 // Update properties
243 mlt_properties_set_int( this_properties, "count", ++ count );
244
245 // Temporary - free audio buffers
246 for ( i = 0 ; i < 4; i ++ )
247 free( audio_buffers[ i ] );
248 }
249 }
250
251 /** The libdv output method.
252 */
253
254 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame )
255 {
256 fwrite( dv_frame, size, 1, stdout );
257 fflush( stdout );
258 }
259
260 /** The main thread - the argument is simply the consumer.
261 */
262
263 static void *consumer_thread( void *arg )
264 {
265 // Map the argument to the object
266 mlt_consumer this = arg;
267
268 // Get the properties
269 mlt_properties properties = mlt_consumer_properties( this );
270
271 // Get the handling methods
272 int ( *video )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "video", NULL );
273 int ( *audio )( mlt_consumer, uint8_t *, mlt_frame ) = mlt_properties_get_data( properties, "audio", NULL );
274 int ( *output )( mlt_consumer, uint8_t *, int, mlt_frame ) = mlt_properties_get_data( properties, "output", NULL );
275
276 // Allocate a single PAL frame for encoding
277 uint8_t *dv_frame = malloc( frame_size_625_50 );
278
279 // Get the service associated to the consumer
280 mlt_service service = mlt_consumer_service( this );
281
282 // Define a frame pointer
283 mlt_frame frame;
284
285 // Loop while running
286 while( mlt_properties_get_int( properties, "running" ) )
287 {
288 // Get the frame
289 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
290 {
291 // Encode the image
292 int size = video( this, dv_frame, frame );
293
294 // Encode the audio
295 if ( size > 0 )
296 audio( this, dv_frame, frame );
297
298 // Output the frame
299 output( this, dv_frame, size, frame );
300
301 // Close the frame
302 mlt_frame_close( frame );
303 }
304 }
305
306 // Tidy up
307 free( dv_frame );
308
309 return NULL;
310 }
311
312 /** Close the consumer.
313 */
314
315 static void consumer_close( mlt_consumer this )
316 {
317 // Get the properties
318 mlt_properties properties = mlt_consumer_properties( this );
319
320 // Get the thread
321 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
322
323 // Stop the thread
324 mlt_properties_set_int( properties, "running", 0 );
325
326 // Wait for termination
327 pthread_join( *thread, NULL );
328
329 // Close the parent
330 mlt_consumer_close( this );
331
332 // Free the memory
333 free( this );
334 }
335