3 * \brief interface for all frame classes
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_frame.h"
25 #include "mlt_producer.h"
26 #include "mlt_factory.h"
27 #include "mlt_profile.h"
35 /** Constructor for a frame.
38 mlt_frame mlt_frame_init( mlt_service service )
41 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
45 mlt_profile profile = mlt_service_profile( service );
47 // Initialise the properties
48 mlt_properties properties = &this->parent;
49 mlt_properties_init( properties, this );
51 // Set default properties on the frame
52 mlt_properties_set_position( properties, "_position", 0.0 );
53 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
54 mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
55 mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
56 mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
57 mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
58 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
59 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
60 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
62 // Construct stacks for frames and methods
63 this->stack_image = mlt_deque_init( );
64 this->stack_audio = mlt_deque_init( );
65 this->stack_service = mlt_deque_init( );
71 /** Fetch the frames properties.
74 mlt_properties mlt_frame_properties( mlt_frame this )
76 return this != NULL ? &this->parent : NULL;
79 /** Check if we have a way to derive something other than a test card.
82 int mlt_frame_is_test_card( mlt_frame this )
84 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
87 /** Check if we have a way to derive something other than test audio.
90 int mlt_frame_is_test_audio( mlt_frame this )
92 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
95 /** Get the aspect ratio of the frame.
98 double mlt_frame_get_aspect_ratio( mlt_frame this )
100 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
103 /** Set the aspect ratio of the frame.
106 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
108 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
111 /** Get the position of this frame.
114 mlt_position mlt_frame_get_position( mlt_frame this )
116 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
117 return pos < 0 ? 0 : pos;
120 /** Set the position of this frame.
123 int mlt_frame_set_position( mlt_frame this, mlt_position value )
125 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
128 /** Stack a get_image callback.
131 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
133 return mlt_deque_push_back( this->stack_image, get_image );
136 /** Pop a get_image callback.
139 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
141 return mlt_deque_pop_back( this->stack_image );
147 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
149 return mlt_deque_push_back( this->stack_image, that );
155 mlt_frame mlt_frame_pop_frame( mlt_frame this )
157 return mlt_deque_pop_back( this->stack_image );
163 int mlt_frame_push_service( mlt_frame this, void *that )
165 return mlt_deque_push_back( this->stack_image, that );
171 void *mlt_frame_pop_service( mlt_frame this )
173 return mlt_deque_pop_back( this->stack_image );
179 int mlt_frame_push_service_int( mlt_frame this, int that )
181 return mlt_deque_push_back_int( this->stack_image, that );
187 int mlt_frame_pop_service_int( mlt_frame this )
189 return mlt_deque_pop_back_int( this->stack_image );
192 /** Push an audio item on the stack.
195 int mlt_frame_push_audio( mlt_frame this, void *that )
197 return mlt_deque_push_back( this->stack_audio, that );
200 /** Pop an audio item from the stack
203 void *mlt_frame_pop_audio( mlt_frame this )
205 return mlt_deque_pop_back( this->stack_audio );
208 /** Return the service stack
211 mlt_deque mlt_frame_service_stack( mlt_frame this )
213 return this->stack_service;
216 /** Replace image stack with the information provided.
218 This might prove to be unreliable and restrictive - the idea is that a transition
219 which normally uses two images may decide to only use the b frame (ie: in the case
220 of a composite where the b frame completely obscures the a frame).
222 The image must be writable and the destructor for the image itself must be taken
223 care of on another frame and that frame cannot have a replace applied to it...
224 Further it assumes that no alpha mask is in use.
226 For these reasons, it can only be used in a specific situation - when you have
227 multiple tracks each with their own transition and these transitions are applied
228 in a strictly reversed order (ie: highest numbered [lowest track] is processed
231 More reliable approach - the cases should be detected during the process phase
232 and the upper tracks should simply not be invited to stack...
235 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
237 // Remove all items from the stack
238 while( mlt_deque_pop_back( this->stack_image ) ) ;
240 // Update the information
241 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
242 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
243 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
244 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
245 this->get_alpha_mask = NULL;
248 const char * mlt_image_format_name( mlt_image_format format )
252 case mlt_image_none: return "none";
253 case mlt_image_rgb24: return "rgb24";
254 case mlt_image_rgb24a: return "rgb24a";
255 case mlt_image_yuv422: return "yuv422";
256 case mlt_image_yuv420p: return "yuv420p";
257 case mlt_image_opengl: return "opengl";
262 /** Get the image associated to the frame.
265 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
267 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
268 mlt_get_image get_image = mlt_frame_pop_get_image( this );
269 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
270 mlt_image_format requested_format = *format;
275 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
276 mlt_position position = mlt_frame_get_position( this );
277 error = get_image( this, buffer, format, width, height, writable );
278 mlt_properties_set_int( properties, "width", *width );
279 mlt_properties_set_int( properties, "height", *height );
280 mlt_properties_set_int( properties, "format", *format );
281 mlt_frame_set_position( this, position );
282 if ( this->convert_image )
283 this->convert_image( this, buffer, format, requested_format );
285 else if ( mlt_properties_get_data( properties, "image", NULL ) )
287 *format = mlt_properties_get_int( properties, "format" );
288 *buffer = mlt_properties_get_data( properties, "image", NULL );
289 *width = mlt_properties_get_int( properties, "width" );
290 *height = mlt_properties_get_int( properties, "height" );
291 if ( this->convert_image )
292 this->convert_image( this, buffer, format, requested_format );
296 mlt_frame test_frame = NULL;
297 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
300 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
301 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
302 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
303 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
304 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
305 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
306 // mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
307 // mlt_properties_set_int( properties, "width", *width );
308 // mlt_properties_set_int( properties, "height", *height );
309 // mlt_properties_set_int( properties, "format", *format );
313 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
314 mlt_frame_get_image( this, buffer, format, width, height, writable );
323 *width = *width == 0 ? 720 : *width;
324 *height = *height == 0 ? 576 : *height;
325 size = *width * *height;
327 mlt_properties_set_int( properties, "format", *format );
328 mlt_properties_set_int( properties, "width", *width );
329 mlt_properties_set_int( properties, "height", *height );
330 mlt_properties_set_int( properties, "aspect_ratio", 0 );
338 case mlt_image_rgb24:
341 *buffer = mlt_pool_alloc( size );
343 memset( *buffer, 255, size );
345 case mlt_image_rgb24a:
346 case mlt_image_opengl:
349 *buffer = mlt_pool_alloc( size );
351 memset( *buffer, 255, size );
353 case mlt_image_yuv422:
356 *buffer = mlt_pool_alloc( size );
359 while ( p != NULL && p != q )
365 case mlt_image_yuv420p:
367 *buffer = mlt_pool_alloc( size );
369 memset( *buffer, 255, size );
373 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
374 mlt_properties_set_int( properties, "test_image", 1 );
377 mlt_properties_set_int( properties, "scaled_width", *width );
378 mlt_properties_set_int( properties, "scaled_height", *height );
383 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
385 uint8_t *alpha = NULL;
388 if ( this->get_alpha_mask != NULL )
389 alpha = this->get_alpha_mask( this );
391 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
394 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
395 alpha = mlt_pool_alloc( size );
396 memset( alpha, 255, size );
397 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
403 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
405 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
406 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
407 int hide = mlt_properties_get_int( properties, "test_audio" );
409 if ( hide == 0 && get_audio != NULL )
411 mlt_position position = mlt_frame_get_position( this );
412 get_audio( this, buffer, format, frequency, channels, samples );
413 mlt_frame_set_position( this, position );
415 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
417 *buffer = mlt_properties_get_data( properties, "audio", NULL );
418 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
419 *channels = mlt_properties_get_int( properties, "audio_channels" );
420 *samples = mlt_properties_get_int( properties, "audio_samples" );
425 *samples = *samples <= 0 ? 1920 : *samples;
426 *channels = *channels <= 0 ? 2 : *channels;
427 *frequency = *frequency <= 0 ? 48000 : *frequency;
428 size = *samples * *channels * sizeof( int16_t );
429 *buffer = mlt_pool_alloc( size );
430 if ( *buffer != NULL )
431 memset( *buffer, 0, size );
432 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
433 mlt_properties_set_int( properties, "test_audio", 1 );
436 mlt_properties_set_int( properties, "audio_frequency", *frequency );
437 mlt_properties_set_int( properties, "audio_channels", *channels );
438 mlt_properties_set_int( properties, "audio_samples", *samples );
440 if ( mlt_properties_get( properties, "meta.volume" ) )
442 double value = mlt_properties_get_double( properties, "meta.volume" );
446 memset( *buffer, 0, *samples * *channels * 2 );
448 else if ( value != 1.0 )
450 int total = *samples * *channels;
451 int16_t *p = *buffer;
459 mlt_properties_set( properties, "meta.volume", NULL );
465 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
468 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
469 mlt_audio_format format = mlt_audio_pcm;
470 int frequency = 32000; // lower frequency available?
472 double fps = mlt_profile_fps( NULL );
473 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
476 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
478 // Make an 8-bit buffer large enough to hold rendering
480 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
481 if ( bitmap != NULL )
482 memset( bitmap, 0, size );
483 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
485 // Render vertical lines
486 int16_t *ubound = pcm + samples * channels;
487 int skip = samples / w - 1;
490 // Iterate sample stream and along x coordinate
491 for ( i = 0; i < w && pcm < ubound; i++ )
493 // pcm data has channels interleaved
494 for ( j = 0; j < channels; j++ )
496 // Determine sample's magnitude from 2s complement;
497 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
498 // The height of a line is the ratio of the magnitude multiplied by
499 // half the vertical resolution
500 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
501 // Determine the starting y coordinate - left channel above center,
502 // right channel below - currently assumes 2 channels
503 int displacement = ( h / 2 ) - ( 1 - j ) * height;
504 // Position buffer pointer using y coordinate, stride, and x coordinate
505 unsigned char *p = &bitmap[ i + displacement * w ];
507 // Draw vertical line
508 for ( k = 0; k < height; k++ )
513 pcm += skip * channels;
519 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
522 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
526 void mlt_frame_close( mlt_frame this )
528 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
530 mlt_deque_close( this->stack_image );
531 mlt_deque_close( this->stack_audio );
532 while( mlt_deque_peek_back( this->stack_service ) )
533 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
534 mlt_deque_close( this->stack_service );
535 mlt_properties_close( &this->parent );
540 /***** convenience functions *****/
542 int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight_start, float weight_end, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
546 int frequency_src = *frequency, frequency_dest = *frequency;
547 int channels_src = *channels, channels_dest = *channels;
548 int samples_src = *samples, samples_dest = *samples;
552 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
553 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
555 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
556 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
558 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
560 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
561 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
563 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
565 if ( channels_src > 6 )
567 if ( channels_dest > 6 )
569 if ( samples_src > 4000 )
571 if ( samples_dest > 4000 )
574 // determine number of samples to process
575 *samples = samples_src < samples_dest ? samples_src : samples_dest;
576 *channels = channels_src < channels_dest ? channels_src : channels_dest;
578 *frequency = frequency_dest;
580 // Compute a smooth ramp over start to end
581 float weight = weight_start;
582 float weight_step = ( weight_end - weight_start ) / *samples;
586 *samples = samples_src;
587 *channels = channels_src;
589 *frequency = frequency_src;
594 for ( i = 0; i < *samples; i++ )
596 for ( j = 0; j < *channels; j++ )
598 if ( j < channels_dest )
599 d = (double) dest[ i * channels_dest + j ];
600 if ( j < channels_src )
601 s = (double) src[ i * channels_src + j ];
602 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
604 weight += weight_step;
610 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
611 // to allow mixing without volume hacking
612 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
616 int frequency_src = *frequency, frequency_dest = *frequency;
617 int channels_src = *channels, channels_dest = *channels;
618 int samples_src = *samples, samples_dest = *samples;
621 double b_weight = 1.0;
623 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
624 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
626 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
627 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
629 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
630 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
632 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
634 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
635 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
637 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
641 *samples = samples_src;
642 *channels = channels_src;
644 *frequency = frequency_src;
648 // determine number of samples to process
649 *samples = samples_src < samples_dest ? samples_src : samples_dest;
650 *channels = channels_src < channels_dest ? channels_src : channels_dest;
652 *frequency = frequency_dest;
654 for ( j = 0; j < *channels; j++ )
655 vp[ j ] = ( double )dest[ j ];
658 double B = exp(-2.0 * M_PI * Fc);
662 for ( i = 0; i < *samples; i++ )
664 for ( j = 0; j < *channels; j++ )
666 v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
667 v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
668 vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
675 /* Will this break when mlt_position is converted to double? -Zach */
676 int mlt_sample_calculator( float fps, int frequency, int64_t position )
680 if ( ( int )( fps * 100 ) == 2997 )
682 samples = frequency / 30;
687 if ( position % 5 != 0 )
691 if ( position % 300 == 0 )
693 else if ( position % 30 == 0 )
695 else if ( position % 2 == 0 )
701 if ( position % 30 == 0 )
703 else if ( position % 29 == 0 )
705 else if ( position % 4 == 2 )
716 samples = frequency / fps;
722 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
726 // TODO: Correct rules for NTSC and drop the * 100 hack
727 if ( ( int )( fps * 100 ) == 2997 )
729 samples = ( ( double )( frame * frequency ) / 30 );
733 samples += 2 * ( frame / 5 );
736 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
739 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
745 samples = ( ( frame * frequency ) / ( int )fps );