remove config.h
[melted] / src / modules / dv / producer_libdv.c
1 /*
2 * producer_libdv.c -- simple libdv test case
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 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <libdv/dv.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 #define FRAME_SIZE_525_60 10 * 150 * 80
36 #define FRAME_SIZE_625_50 12 * 150 * 80
37
38 /** To conserve resources, we maintain a stack of dv decoders.
39 */
40
41 static pthread_mutex_t decoder_lock = PTHREAD_MUTEX_INITIALIZER;
42 static mlt_properties dv_decoders = NULL;
43
44 dv_decoder_t *dv_decoder_alloc( )
45 {
46 // We'll return a dv_decoder
47 dv_decoder_t *this = NULL;
48
49 // Lock the mutex
50 pthread_mutex_lock( &decoder_lock );
51
52 // Create the properties if necessary
53 if ( dv_decoders == NULL )
54 {
55 // Create the properties
56 dv_decoders = mlt_properties_new( );
57
58 // Create the stack
59 mlt_properties_set_data( dv_decoders, "stack", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
60
61 // Register the properties for clean up
62 mlt_factory_register_for_clean_up( dv_decoders, ( mlt_destructor )mlt_properties_close );
63 }
64
65 // Now try to obtain a decoder
66 if ( dv_decoders != NULL )
67 {
68 // Obtain the stack
69 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
70
71 // Pop the top of the stack
72 this = mlt_deque_pop_back( stack );
73
74 // Create a new decoder if none available
75 if ( this == NULL )
76 {
77 // We'll need a unique property ID for this
78 char label[ 256 ];
79
80 // Configure the decoder
81 this = dv_decoder_new( FALSE, FALSE, FALSE );
82 this->quality = DV_QUALITY_COLOR | DV_QUALITY_AC_2;
83 this->audio->arg_audio_emphasis = 2;
84 dv_set_audio_correction( this, DV_AUDIO_CORRECT_AVERAGE );
85 dv_set_error_log( this, NULL );
86
87 // Register it with the properties to ensure clean up
88 sprintf( label, "%p", this );
89 mlt_properties_set_data( dv_decoders, label, this, 0, ( mlt_destructor )dv_decoder_free, NULL );
90 }
91 }
92
93 // Unlock the mutex
94 pthread_mutex_unlock( &decoder_lock );
95
96 return this;
97 }
98
99 void dv_decoder_return( dv_decoder_t *this )
100 {
101 // Lock the mutex
102 pthread_mutex_lock( &decoder_lock );
103
104 // Now try to return the decoder
105 if ( dv_decoders != NULL )
106 {
107 // Obtain the stack
108 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
109
110 // Push it back
111 mlt_deque_push_back( stack, this );
112 }
113
114 // Unlock the mutex
115 pthread_mutex_unlock( &decoder_lock );
116 }
117
118
119 typedef struct producer_libdv_s *producer_libdv;
120
121 struct producer_libdv_s
122 {
123 struct mlt_producer_s parent;
124 int fd;
125 int is_pal;
126 uint64_t file_size;
127 int frame_size;
128 long frames_in_file;
129 mlt_producer alternative;
130 };
131
132 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
133 static void producer_close( mlt_producer parent );
134
135 static int producer_collect_info( producer_libdv this );
136
137 mlt_producer producer_libdv_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
138 {
139 producer_libdv this = calloc( sizeof( struct producer_libdv_s ), 1 );
140
141 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
142 {
143 int destroy = 0;
144 mlt_producer producer = &this->parent;
145 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
146
147 // Set the resource property (required for all producers)
148 mlt_properties_set( properties, "resource", filename );
149
150 // Register transport implementation with the producer
151 producer->close = ( mlt_destructor )producer_close;
152
153 // Register our get_frame implementation with the producer
154 producer->get_frame = producer_get_frame;
155
156 // If we have mov or dv, then we'll use an alternative producer
157 if ( strchr( filename, '.' ) != NULL && (
158 strncasecmp( strrchr( filename, '.' ), ".avi", 4 ) == 0 ||
159 strncasecmp( strrchr( filename, '.' ), ".mov", 4 ) == 0 ) )
160 {
161 // Load via an alternative mechanism
162 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
163 this->alternative = mlt_factory_producer( profile, "kino", filename );
164
165 // If it's unavailable, then clean up
166 if ( this->alternative == NULL )
167 destroy = 1;
168 else
169 mlt_properties_pass( properties, MLT_PRODUCER_PROPERTIES( this->alternative ), "" );
170 this->is_pal = ( ( int ) mlt_producer_get_fps( producer ) ) == 25;
171 }
172 else
173 {
174 // Open the file if specified
175 this->fd = open( filename, O_RDONLY );
176
177 // Collect info
178 if ( this->fd == -1 || !producer_collect_info( this ) )
179 destroy = 1;
180 }
181
182 // If we couldn't open the file, then destroy it now
183 if ( destroy )
184 {
185 mlt_producer_close( producer );
186 producer = NULL;
187 }
188
189 // Return the producer
190 return producer;
191 }
192 free( this );
193 return NULL;
194 }
195
196 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
197 {
198 int result = read( fd, frame_buf, FRAME_SIZE_525_60 ) == FRAME_SIZE_525_60;
199 if ( result )
200 {
201 *isPAL = ( frame_buf[3] & 0x80 );
202
203 if ( *isPAL )
204 {
205 int diff = FRAME_SIZE_625_50 - FRAME_SIZE_525_60;
206 result = read( fd, frame_buf + FRAME_SIZE_525_60, diff ) == diff;
207 }
208 }
209
210 return result;
211 }
212
213 static int producer_collect_info( producer_libdv this )
214 {
215 int valid = 0;
216
217 uint8_t *dv_data = mlt_pool_alloc( FRAME_SIZE_625_50 );
218
219 if ( dv_data != NULL )
220 {
221 // Read the first frame
222 valid = read_frame( this->fd, dv_data, &this->is_pal );
223
224 // If it looks like a valid frame, the get stats
225 if ( valid )
226 {
227 // Get the properties
228 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
229
230 // Get a dv_decoder
231 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
232
233 // Determine the file size
234 struct stat buf;
235 fstat( this->fd, &buf );
236
237 // Store the file size
238 this->file_size = buf.st_size;
239
240 // Determine the frame size
241 this->frame_size = this->is_pal ? FRAME_SIZE_625_50 : FRAME_SIZE_525_60;
242
243 // Determine the number of frames in the file
244 this->frames_in_file = this->file_size / this->frame_size;
245
246 // Calculate default in/out points
247 double fps = this->is_pal ? 25 : 30000.0 / 1001.0;
248 if ( mlt_producer_get_fps( &this->parent ) == fps )
249 {
250 if ( this->frames_in_file > 0 )
251 {
252 mlt_properties_set_position( properties, "length", this->frames_in_file );
253 mlt_properties_set_position( properties, "in", 0 );
254 mlt_properties_set_position( properties, "out", this->frames_in_file - 1 );
255 }
256 }
257 else
258 {
259 valid = 0;
260 }
261
262 // Parse the header for meta info
263 dv_parse_header( dv_decoder, dv_data );
264 mlt_properties_set_double( properties, "aspect_ratio",
265 dv_format_wide( dv_decoder ) ? ( this->is_pal ? 118.0/81.0 : 40.0/33.0 ) : ( this->is_pal ? 59.0/54.0 : 10.0/11.0 ) );
266
267 // Return the decoder
268 dv_decoder_return( dv_decoder );
269 }
270
271 mlt_pool_release( dv_data );
272 }
273
274 return valid;
275 }
276
277 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
278 {
279 int pitches[3] = { 0, 0, 0 };
280 uint8_t *pixels[3] = { NULL, NULL, NULL };
281
282 // Get the frames properties
283 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
284
285 // Get a dv_decoder
286 dv_decoder_t *decoder = dv_decoder_alloc( );
287
288 // Get the dv data
289 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
290
291 // Get and set the quality request
292 char *quality = mlt_frame_pop_service( this );
293
294 if ( quality != NULL )
295 {
296 if ( strncmp( quality, "fast", 4 ) == 0 )
297 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_DC );
298 else if ( strncmp( quality, "best", 4 ) == 0 )
299 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_2 );
300 else
301 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_1 );
302 }
303
304 // Parse the header for meta info
305 dv_parse_header( decoder, dv_data );
306
307 // Assign width and height according to the frame
308 *width = 720;
309 *height = dv_data[ 3 ] & 0x80 ? 576 : 480;
310
311 // Extract an image of the format requested
312 if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
313 {
314 // Allocate an image
315 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 2 );
316
317 // Pass to properties for clean up
318 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
319
320 // Decode the image
321 pitches[ 0 ] = *width * 2;
322 pixels[ 0 ] = image;
323 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
324
325 // Assign result
326 *buffer = image;
327 *format = mlt_image_yuv422;
328 }
329 else
330 {
331 // Allocate an image
332 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 3 );
333
334 // Pass to properties for clean up
335 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 3, ( mlt_destructor )mlt_pool_release, NULL );
336
337 // Decode the frame
338 pitches[ 0 ] = 720 * 3;
339 pixels[ 0 ] = image;
340 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
341
342 // Assign result
343 *buffer = image;
344 *format = mlt_image_rgb24;
345 }
346
347 // Return the decoder
348 dv_decoder_return( decoder );
349
350 return 0;
351 }
352
353 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
354 {
355 int16_t *p;
356 int i, j;
357 int16_t *audio_channels[ 4 ];
358
359 // Get the frames properties
360 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
361
362 // Get a dv_decoder
363 dv_decoder_t *decoder = dv_decoder_alloc( );
364
365 // Get the dv data
366 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
367
368 // Parse the header for meta info
369 dv_parse_header( decoder, dv_data );
370
371 // Check that we have audio
372 if ( decoder->audio->num_channels > 0 )
373 {
374 // Obtain required values
375 *frequency = decoder->audio->frequency;
376 *samples = decoder->audio->samples_this_frame;
377 *channels = decoder->audio->num_channels;
378
379 // Create a temporary workspace
380 for ( i = 0; i < 4; i++ )
381 audio_channels[ i ] = mlt_pool_alloc( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
382
383 // Create a workspace for the result
384 *buffer = mlt_pool_alloc( *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
385
386 // Pass the allocated audio buffer as a property
387 mlt_properties_set_data( properties, "audio", *buffer, *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), ( mlt_destructor )mlt_pool_release, NULL );
388
389 // Decode the audio
390 dv_decode_full_audio( decoder, dv_data, audio_channels );
391
392 // Interleave the audio
393 p = *buffer;
394 for ( i = 0; i < *samples; i++ )
395 for ( j = 0; j < *channels; j++ )
396 *p++ = audio_channels[ j ][ i ];
397
398 // Free the temporary work space
399 for ( i = 0; i < 4; i++ )
400 mlt_pool_release( audio_channels[ i ] );
401 }
402 else
403 {
404 // No audio available on the frame, so get test audio (silence)
405 mlt_frame_get_audio( this, buffer, format, frequency, channels, samples );
406 }
407
408 // Return the decoder
409 dv_decoder_return( decoder );
410
411 return 0;
412 }
413
414 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
415 {
416 // Access the private data
417 producer_libdv this = producer->child;
418
419 // Will carry the frame data
420 uint8_t *data = NULL;
421
422 // Obtain the current frame number
423 uint64_t position = mlt_producer_frame( producer );
424
425 if ( this->alternative == NULL )
426 {
427 // Convert timecode to a file position (ensuring that we're on a frame boundary)
428 uint64_t offset = position * this->frame_size;
429
430 // Allocate space
431 data = mlt_pool_alloc( FRAME_SIZE_625_50 );
432
433 // Create an empty frame
434 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
435
436 // Seek and fetch
437 if ( this->fd != 0 &&
438 lseek( this->fd, offset, SEEK_SET ) == offset &&
439 read_frame( this->fd, data, &this->is_pal ) )
440 {
441 // Pass the dv data
442 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "dv_data", data, FRAME_SIZE_625_50, ( mlt_destructor )mlt_pool_release, NULL );
443 }
444 else
445 {
446 mlt_pool_release( data );
447 data = NULL;
448 }
449 }
450 else
451 {
452 // Seek
453 mlt_producer_seek( this->alternative, position );
454
455 // Fetch
456 mlt_service_get_frame( MLT_PRODUCER_SERVICE( this->alternative ), frame, 0 );
457
458 // Verify
459 if ( *frame != NULL )
460 data = mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame ), "dv_data", NULL );
461 }
462
463 if ( data != NULL )
464 {
465 // Get the frames properties
466 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
467
468 // Get a dv_decoder
469 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
470
471 mlt_properties_set_int( properties, "test_image", 0 );
472 mlt_properties_set_int( properties, "test_audio", 0 );
473
474 // Update other info on the frame
475 mlt_properties_set_int( properties, "width", 720 );
476 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
477 mlt_properties_set_int( properties, "top_field_first", !this->is_pal ? 0 : ( data[ 5 ] & 0x07 ) == 0 ? 0 : 1 );
478
479 // Parse the header for meta info
480 dv_parse_header( dv_decoder, data );
481 //mlt_properties_set_int( properties, "progressive", dv_is_progressive( dv_decoder ) );
482 mlt_properties_set_double( properties, "aspect_ratio",
483 dv_format_wide( dv_decoder ) ? ( this->is_pal ? 118.0/81.0 : 40.0/33.0 ) : ( this->is_pal ? 59.0/54.0 : 10.0/11.0 ) );
484
485
486 mlt_properties_set_int( properties, "frequency", dv_decoder->audio->frequency );
487 mlt_properties_set_int( properties, "channels", dv_decoder->audio->num_channels );
488
489 // Hmm - register audio callback
490 mlt_frame_push_audio( *frame, producer_get_audio );
491
492 // Push the quality string
493 mlt_frame_push_service( *frame, mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "quality" ) );
494
495 // Push the get_image method on to the stack
496 mlt_frame_push_get_image( *frame, producer_get_image );
497
498 // Return the decoder
499 dv_decoder_return( dv_decoder );
500 }
501
502 // Update timecode on the frame we're creating
503 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
504
505 // Calculate the next timecode
506 mlt_producer_prepare_next( producer );
507
508 return 0;
509 }
510
511 static void producer_close( mlt_producer parent )
512 {
513 // Obtain this
514 producer_libdv this = parent->child;
515
516 // Close the file
517 if ( this->fd > 0 )
518 close( this->fd );
519
520 if ( this->alternative )
521 mlt_producer_close( this->alternative );
522
523 // Close the parent
524 parent->close = NULL;
525 mlt_producer_close( parent );
526
527 // Free the memory
528 free( this );
529 }