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 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.
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.
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
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 <framework/mlt_profile.h>
33 #include <sys/types.h>
37 #define FRAME_SIZE_525_60 10 * 150 * 80
38 #define FRAME_SIZE_625_50 12 * 150 * 80
40 /** To conserve resources, we maintain a stack of dv decoders.
43 static pthread_mutex_t decoder_lock = PTHREAD_MUTEX_INITIALIZER;
44 static mlt_properties dv_decoders = NULL;
46 dv_decoder_t *dv_decoder_alloc( )
48 // We'll return a dv_decoder
49 dv_decoder_t *this = NULL;
52 pthread_mutex_lock( &decoder_lock );
54 // Create the properties if necessary
55 if ( dv_decoders == NULL )
57 // Create the properties
58 dv_decoders = mlt_properties_new( );
61 mlt_properties_set_data( dv_decoders, "stack", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
63 // Register the properties for clean up
64 mlt_factory_register_for_clean_up( dv_decoders, ( mlt_destructor )mlt_properties_close );
67 // Now try to obtain a decoder
68 if ( dv_decoders != NULL )
71 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
73 // Pop the top of the stack
74 this = mlt_deque_pop_back( stack );
76 // Create a new decoder if none available
79 // We'll need a unique property ID for this
82 // Configure the decoder
83 this = dv_decoder_new( FALSE, FALSE, FALSE );
84 this->quality = DV_QUALITY_COLOR | DV_QUALITY_AC_2;
85 this->audio->arg_audio_emphasis = 2;
86 dv_set_audio_correction( this, DV_AUDIO_CORRECT_AVERAGE );
87 dv_set_error_log( this, NULL );
89 // Register it with the properties to ensure clean up
90 sprintf( label, "%p", this );
91 mlt_properties_set_data( dv_decoders, label, this, 0, ( mlt_destructor )dv_decoder_free, NULL );
96 pthread_mutex_unlock( &decoder_lock );
101 void dv_decoder_return( dv_decoder_t *this )
104 pthread_mutex_lock( &decoder_lock );
106 // Now try to return the decoder
107 if ( dv_decoders != NULL )
110 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
113 mlt_deque_push_back( stack, this );
117 pthread_mutex_unlock( &decoder_lock );
121 typedef struct producer_libdv_s *producer_libdv;
123 struct producer_libdv_s
125 struct mlt_producer_s parent;
131 mlt_producer alternative;
134 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
135 static void producer_close( mlt_producer parent );
137 static int producer_collect_info( producer_libdv this, mlt_profile profile );
139 mlt_producer producer_libdv_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
141 producer_libdv this = calloc( 1, sizeof( struct producer_libdv_s ) );
143 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
146 mlt_producer producer = &this->parent;
147 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
149 // Set the resource property (required for all producers)
150 mlt_properties_set( properties, "resource", filename );
152 // Register transport implementation with the producer
153 producer->close = ( mlt_destructor )producer_close;
155 // Register our get_frame implementation with the producer
156 producer->get_frame = producer_get_frame;
158 // If we have mov or dv, then we'll use an alternative producer
159 if ( strchr( filename, '.' ) != NULL && (
160 strncasecmp( strrchr( filename, '.' ), ".avi", 4 ) == 0 ||
161 strncasecmp( strrchr( filename, '.' ), ".mov", 4 ) == 0 ) )
163 // Load via an alternative mechanism
164 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
165 this->alternative = mlt_factory_producer( profile, "kino", filename );
167 // If it's unavailable, then clean up
168 if ( this->alternative == NULL )
171 mlt_properties_pass( properties, MLT_PRODUCER_PROPERTIES( this->alternative ), "" );
172 this->is_pal = ( ( int ) mlt_producer_get_fps( producer ) ) == 25;
176 // Open the file if specified
177 this->fd = open( filename, O_RDONLY );
180 if ( this->fd == -1 || !producer_collect_info( this, profile ) )
184 // If we couldn't open the file, then destroy it now
187 mlt_producer_close( producer );
191 // Return the producer
198 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
200 int result = read( fd, frame_buf, FRAME_SIZE_525_60 ) == FRAME_SIZE_525_60;
203 *isPAL = ( frame_buf[3] & 0x80 );
206 int diff = FRAME_SIZE_625_50 - FRAME_SIZE_525_60;
207 result = read( fd, frame_buf + FRAME_SIZE_525_60, diff ) == diff;
214 static int producer_collect_info( producer_libdv this, mlt_profile profile )
218 uint8_t *dv_data = mlt_pool_alloc( FRAME_SIZE_625_50 );
220 if ( dv_data != NULL )
222 // Read the first frame
223 valid = read_frame( this->fd, dv_data, &this->is_pal );
225 // If it looks like a valid frame, the get stats
230 // Get the properties
231 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
234 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
236 // Determine the file size
238 fstat( this->fd, &buf );
240 // Store the file size
241 this->file_size = buf.st_size;
243 // Determine the frame size
244 this->frame_size = this->is_pal ? FRAME_SIZE_625_50 : FRAME_SIZE_525_60;
246 // Determine the number of frames in the file
247 this->frames_in_file = this->file_size / this->frame_size;
249 // Calculate default in/out points
250 int fps = 1000 * ( this->is_pal ? 25 : ( 30000.0 / 1001.0 ) );
251 if ( ( int )( mlt_profile_fps( profile ) * 1000 ) == fps )
253 if ( this->frames_in_file > 0 )
255 mlt_properties_set_position( properties, "length", this->frames_in_file );
256 mlt_properties_set_position( properties, "in", 0 );
257 mlt_properties_set_position( properties, "out", this->frames_in_file - 1 );
265 // Parse the header for meta info
266 dv_parse_header( dv_decoder, dv_data );
269 if ( dv_format_wide( dv_decoder ) )
270 aspect_ratio = 64.0 / 45.0;
272 aspect_ratio = 16.0 / 15.0;
276 if ( dv_format_wide( dv_decoder ) )
277 aspect_ratio = 32.0 / 27.0;
279 aspect_ratio = 8 / 9;
281 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio);
282 mlt_properties_set_int( properties, "meta.media.nb_streams", 2 );
283 mlt_properties_set_int( properties, "video_index", 0 );
284 mlt_properties_set( properties, "meta.media.0.stream.type", "video" );
285 mlt_properties_set( properties, "meta.media.0.codec.name", "dvvideo" );
286 mlt_properties_set( properties, "meta.media.0.codec.long_name", "DV (Digital Video)" );
287 mlt_properties_set_int( properties, "audio_index", 1 );
288 mlt_properties_set( properties, "meta.media.1.stream.type", "audio" );
289 mlt_properties_set( properties, "meta.media.1.codec.name", "pcm_s16le" );
290 mlt_properties_set( properties, "meta.media.1.codec.long_name", "signed 16-bit little-endian PCM" );
291 mlt_properties_set_int( properties, "meta.media.width", 720 );
292 mlt_properties_set_int( properties, "meta.media.height", this->is_pal ? 576 : 480 );
293 mlt_properties_set_int( properties, "meta.media.frame_rate_num", this->is_pal? 25 : 30000 );
294 mlt_properties_set_int( properties, "meta.media.frame_rate_den", this->is_pal? 1 : 1001 );
296 // Return the decoder
297 dv_decoder_return( dv_decoder );
300 mlt_pool_release( dv_data );
306 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
308 int pitches[3] = { 0, 0, 0 };
309 uint8_t *pixels[3] = { NULL, NULL, NULL };
311 // Get the frames properties
312 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
315 dv_decoder_t *decoder = dv_decoder_alloc( );
318 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
320 // Get and set the quality request
321 char *quality = mlt_frame_pop_service( this );
323 if ( quality != NULL )
325 if ( strncmp( quality, "fast", 4 ) == 0 )
326 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_DC );
327 else if ( strncmp( quality, "best", 4 ) == 0 )
328 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_2 );
330 decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_1 );
333 // Parse the header for meta info
334 dv_parse_header( decoder, dv_data );
336 // Assign width and height according to the frame
338 *height = dv_data[ 3 ] & 0x80 ? 576 : 480;
340 // Extract an image of the format requested
341 if ( *format != mlt_image_rgb24 )
344 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 2 );
346 // Pass to properties for clean up
347 mlt_frame_set_image( this, image, *width * ( *height + 1 ) * 2, mlt_pool_release );
350 pitches[ 0 ] = *width * 2;
352 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
356 *format = mlt_image_yuv422;
361 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 3 );
363 // Pass to properties for clean up
364 mlt_frame_set_image( this, image, *width * ( *height + 1 ) * 3, mlt_pool_release );
367 pitches[ 0 ] = 720 * 3;
369 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
373 *format = mlt_image_rgb24;
376 // Return the decoder
377 dv_decoder_return( decoder );
382 static int producer_get_audio( mlt_frame this, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
386 int16_t *audio_channels[ 4 ];
388 // Get the frames properties
389 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
392 dv_decoder_t *decoder = dv_decoder_alloc( );
395 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
397 // Parse the header for meta info
398 dv_parse_header( decoder, dv_data );
400 // Check that we have audio
401 if ( decoder->audio->num_channels > 0 )
403 int size = *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t );
405 // Obtain required values
406 *frequency = decoder->audio->frequency;
407 *samples = decoder->audio->samples_this_frame;
408 *channels = decoder->audio->num_channels;
409 *format = mlt_audio_s16;
411 // Create a temporary workspace
412 for ( i = 0; i < 4; i++ )
413 audio_channels[ i ] = mlt_pool_alloc( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
415 // Create a workspace for the result
416 *buffer = mlt_pool_alloc( size );
418 // Pass the allocated audio buffer as a property
419 mlt_frame_set_audio( this, *buffer, *format, size, mlt_pool_release );
422 dv_decode_full_audio( decoder, dv_data, audio_channels );
424 // Interleave the audio
426 for ( i = 0; i < *samples; i++ )
427 for ( j = 0; j < *channels; j++ )
428 *p++ = audio_channels[ j ][ i ];
430 // Free the temporary work space
431 for ( i = 0; i < 4; i++ )
432 mlt_pool_release( audio_channels[ i ] );
436 // No audio available on the frame, so get test audio (silence)
437 mlt_frame_get_audio( this, buffer, format, frequency, channels, samples );
440 // Return the decoder
441 dv_decoder_return( decoder );
446 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
448 // Access the private data
449 producer_libdv this = producer->child;
451 // Will carry the frame data
452 uint8_t *data = NULL;
454 // Obtain the current frame number
455 uint64_t position = mlt_producer_frame( producer );
457 if ( this->alternative == NULL )
459 // Convert timecode to a file position (ensuring that we're on a frame boundary)
460 uint64_t offset = position * this->frame_size;
463 data = mlt_pool_alloc( FRAME_SIZE_625_50 );
465 // Create an empty frame
466 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
469 if ( this->fd != 0 &&
470 lseek( this->fd, offset, SEEK_SET ) == offset &&
471 read_frame( this->fd, data, &this->is_pal ) )
474 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "dv_data", data, FRAME_SIZE_625_50, ( mlt_destructor )mlt_pool_release, NULL );
478 mlt_pool_release( data );
485 mlt_producer_seek( this->alternative, position );
488 mlt_service_get_frame( MLT_PRODUCER_SERVICE( this->alternative ), frame, 0 );
491 if ( *frame != NULL )
492 data = mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame ), "dv_data", NULL );
497 // Get the frames properties
498 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
501 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
503 mlt_properties_set_int( properties, "test_image", 0 );
504 mlt_properties_set_int( properties, "test_audio", 0 );
506 // Update other info on the frame
507 mlt_properties_set_int( properties, "width", 720 );
508 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
509 mlt_properties_set_int( properties, "top_field_first", !this->is_pal ? 0 : ( data[ 5 ] & 0x07 ) == 0 ? 0 : 1 );
510 mlt_properties_set_int( properties, "colorspace", 601 );
512 // Parse the header for meta info
513 dv_parse_header( dv_decoder, data );
514 //mlt_properties_set_int( properties, "progressive", dv_is_progressive( dv_decoder ) );
515 mlt_properties_set_double( properties, "aspect_ratio",
516 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 ) );
519 mlt_properties_set_int( properties, "audio_frequency", dv_decoder->audio->frequency );
520 mlt_properties_set_int( properties, "audio_channels", dv_decoder->audio->num_channels );
522 // Register audio callback
523 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( producer ), "audio_index" ) > 0 )
524 mlt_frame_push_audio( *frame, producer_get_audio );
526 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( producer ), "video_index" ) > -1 )
528 // Push the quality string
529 mlt_frame_push_service( *frame, mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "quality" ) );
531 // Push the get_image method on to the stack
532 mlt_frame_push_get_image( *frame, producer_get_image );
535 // Return the decoder
536 dv_decoder_return( dv_decoder );
539 // Update timecode on the frame we're creating
540 if ( *frame != NULL )
541 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
543 // Calculate the next timecode
544 mlt_producer_prepare_next( producer );
549 static void producer_close( mlt_producer parent )
552 producer_libdv this = parent->child;
558 if ( this->alternative )
559 mlt_producer_close( this->alternative );
562 parent->close = NULL;
563 mlt_producer_close( parent );