2 * producer_libdv.c -- simple libdv test case
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.
21 #include "producer_libdv.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
31 #include <sys/types.h>
35 /** To conserve resources, we maintain a stack of dv decoders.
38 static pthread_mutex_t decoder_lock = PTHREAD_MUTEX_INITIALIZER;
39 static mlt_properties dv_decoders = NULL;
41 dv_decoder_t *dv_decoder_alloc( )
43 // We'll return a dv_decoder
44 dv_decoder_t *this = NULL;
47 pthread_mutex_lock( &decoder_lock );
49 // Create the properties if necessary
50 if ( dv_decoders == NULL )
52 // Create the properties
53 dv_decoders = mlt_properties_new( );
56 mlt_properties_set_data( dv_decoders, "stack", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
58 // Register the properties for clean up
59 mlt_factory_register_for_clean_up( dv_decoders, ( mlt_destructor )mlt_properties_close );
62 // Now try to obtain a decoder
63 if ( dv_decoders != NULL )
66 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
68 // Pop the top of the stack
69 this = mlt_deque_pop_back( stack );
71 // Create a new decoder if none available
74 // We'll need a unique property ID for this
77 // Configure the decoder
78 this = dv_decoder_new( FALSE, FALSE, FALSE );
79 this->quality = DV_QUALITY_COLOR | DV_QUALITY_AC_2;
80 this->audio->arg_audio_emphasis = 2;
81 dv_set_audio_correction( this, DV_AUDIO_CORRECT_AVERAGE );
82 dv_set_error_log( this, NULL );
84 // Register it with the properties to ensure clean up
85 sprintf( label, "%p", this );
86 mlt_properties_set_data( dv_decoders, label, this, 0, ( mlt_destructor )dv_decoder_free, NULL );
91 pthread_mutex_unlock( &decoder_lock );
96 void dv_decoder_return( dv_decoder_t *this )
99 pthread_mutex_lock( &decoder_lock );
101 // Now try to return the decoder
102 if ( dv_decoders != NULL )
105 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
108 mlt_deque_push_back( stack, this );
112 pthread_mutex_unlock( &decoder_lock );
116 typedef struct producer_libdv_s *producer_libdv;
118 struct producer_libdv_s
120 struct mlt_producer_s parent;
128 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
129 static void producer_close( mlt_producer parent );
131 static int producer_collect_info( producer_libdv this );
133 mlt_producer producer_libdv_init( char *filename )
135 producer_libdv this = calloc( sizeof( struct producer_libdv_s ), 1 );
137 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
139 mlt_producer producer = &this->parent;
140 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
142 // Register transport implementation with the producer
143 producer->close = ( mlt_destructor )producer_close;
145 // Register our get_frame implementation with the producer
146 producer->get_frame = producer_get_frame;
148 // Open the file if specified
149 this->fd = open( filename, O_RDONLY );
152 if ( this->fd != -1 && producer_collect_info( this ) )
154 // Set the resource property (required for all producers)
155 mlt_properties_set( properties, "resource", filename );
160 mlt_producer_close( producer );
164 // Return the producer
171 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
173 int result = read( fd, frame_buf, FRAME_SIZE_525_60 ) == FRAME_SIZE_525_60;
176 *isPAL = ( frame_buf[3] & 0x80 );
180 int diff = FRAME_SIZE_625_50 - FRAME_SIZE_525_60;
181 result = read( fd, frame_buf + FRAME_SIZE_525_60, diff ) == diff;
188 static int producer_collect_info( producer_libdv this )
192 uint8_t *dv_data = mlt_pool_alloc( FRAME_SIZE_625_50 );
194 if ( dv_data != NULL )
196 // Read the first frame
197 valid = read_frame( this->fd, dv_data, &this->is_pal );
199 // If it looks like a valid frame, the get stats
202 // Get the properties
203 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
206 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
208 // Determine the file size
210 fstat( this->fd, &buf );
212 // Store the file size
213 this->file_size = buf.st_size;
215 // Determine the frame size
216 this->frame_size = this->is_pal ? FRAME_SIZE_625_50 : FRAME_SIZE_525_60;
218 // Determine the number of frames in the file
219 this->frames_in_file = this->file_size / this->frame_size;
221 // Calculate default in/out points
222 double fps = this->is_pal ? 25 : 30000.0 / 1001.0;
223 if ( mlt_properties_get_double( properties, "fps" ) == fps )
225 if ( this->frames_in_file > 0 )
227 mlt_properties_set_position( properties, "length", this->frames_in_file );
228 mlt_properties_set_position( properties, "in", 0 );
229 mlt_properties_set_position( properties, "out", this->frames_in_file - 1 );
237 // Parse the header for meta info
238 dv_parse_header( dv_decoder, dv_data );
239 mlt_properties_set_double( properties, "aspect_ratio",
240 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 ) );
242 // Return the decoder
243 dv_decoder_return( dv_decoder );
246 mlt_pool_release( dv_data );
252 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
254 int pitches[3] = { 0, 0, 0 };
255 uint8_t *pixels[3] = { NULL, NULL, NULL };
257 // Get the frames properties
258 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
261 dv_decoder_t *decoder = dv_decoder_alloc( );
264 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
266 // Get and set the quality request
267 char *quality = mlt_frame_pop_service( this );
269 if ( quality != NULL )
271 if ( strncmp( quality, "fast", 4 ) == 0 )
272 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_DC );
273 else if ( strncmp( quality, "best", 4 ) == 0 )
274 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_2 );
276 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_1 );
279 // Parse the header for meta info
280 dv_parse_header( decoder, dv_data );
282 // Assign width and height from properties
283 *width = mlt_properties_get_int( properties, "width" );
284 *height = mlt_properties_get_int( properties, "height" );
286 // Extract an image of the format requested
287 if ( *format == mlt_image_yuv422 )
290 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 2 );
292 // Pass to properties for clean up
293 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
296 pitches[ 0 ] = *width * 2;
298 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
303 else if ( *format == mlt_image_rgb24 )
306 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 3 );
308 // Pass to properties for clean up
309 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 3, ( mlt_destructor )mlt_pool_release, NULL );
312 pitches[ 0 ] = 720 * 3;
314 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
320 // Return the decoder
321 dv_decoder_return( decoder );
326 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
330 int16_t *audio_channels[ 4 ];
332 // Get the frames properties
333 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
336 dv_decoder_t *decoder = dv_decoder_alloc( );
339 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
341 // Parse the header for meta info
342 dv_parse_header( decoder, dv_data );
344 // Check that we have audio
345 if ( decoder->audio->num_channels > 0 )
347 // Obtain required values
348 *frequency = decoder->audio->frequency;
349 *samples = decoder->audio->samples_this_frame;
350 *channels = decoder->audio->num_channels;
352 // Create a temporary workspace
353 for ( i = 0; i < 4; i++ )
354 audio_channels[ i ] = mlt_pool_alloc( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
356 // Create a workspace for the result
357 *buffer = mlt_pool_alloc( *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
359 // Pass the allocated audio buffer as a property
360 mlt_properties_set_data( properties, "audio", *buffer, *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), ( mlt_destructor )mlt_pool_release, NULL );
363 dv_decode_full_audio( decoder, dv_data, audio_channels );
365 // Interleave the audio
367 for ( i = 0; i < *samples; i++ )
368 for ( j = 0; j < *channels; j++ )
369 *p++ = audio_channels[ j ][ i ];
371 // Free the temporary work space
372 for ( i = 0; i < 4; i++ )
373 mlt_pool_release( audio_channels[ i ] );
377 // No audio available on the frame, so get test audio (silence)
378 mlt_frame_get_audio( this, buffer, format, frequency, channels, samples );
381 // Return the decoder
382 dv_decoder_return( decoder );
387 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
389 producer_libdv this = producer->child;
390 uint8_t *data = mlt_pool_alloc( FRAME_SIZE_625_50 );
392 // Obtain the current frame number
393 uint64_t position = mlt_producer_frame( producer );
395 // Convert timecode to a file position (ensuring that we're on a frame boundary)
396 uint64_t offset = position * this->frame_size;
398 // Create an empty frame
399 *frame = mlt_frame_init( );
402 if ( this->fd != 0 &&
403 lseek( this->fd, offset, SEEK_SET ) == offset &&
404 read_frame( this->fd, data, &this->is_pal ) )
406 // Get the frames properties
407 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
410 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
413 mlt_properties_set_data( properties, "dv_data", data, FRAME_SIZE_625_50, ( mlt_destructor )mlt_pool_release, NULL );
415 // Update other info on the frame
416 mlt_properties_set_int( properties, "width", 720 );
417 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
418 mlt_properties_set_int( properties, "top_field_first", !this->is_pal ? 0 : ( data[ 5 ] & 0x07 ) == 0 ? 0 : 1 );
420 // Parse the header for meta info
421 dv_parse_header( dv_decoder, data );
422 //mlt_properties_set_int( properties, "progressive", dv_is_progressive( dv_decoder ) );
423 mlt_properties_set_double( properties, "aspect_ratio",
424 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 ) );
426 // Hmm - register audio callback
427 mlt_frame_push_audio( *frame, producer_get_audio );
429 // Push the quality string
430 mlt_frame_push_service( *frame, mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "quality" ) );
432 // Push the get_image method on to the stack
433 mlt_frame_push_get_image( *frame, producer_get_image );
435 // Return the decoder
436 dv_decoder_return( dv_decoder );
440 mlt_pool_release( data );
443 // Update timecode on the frame we're creating
444 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
446 // Calculate the next timecode
447 mlt_producer_prepare_next( producer );
452 static void producer_close( mlt_producer parent )
455 producer_libdv this = parent->child;
462 parent->close = NULL;
463 mlt_producer_close( parent );