More corrections to frame position and audio/track handling
[melted] / src / framework / mlt_frame.c
index 4d58232..be21562 100644 (file)
@@ -80,7 +80,7 @@ mlt_frame mlt_frame_init( )
 
 mlt_properties mlt_frame_properties( mlt_frame this )
 {
-       return &this->parent;
+       return this != NULL ? &this->parent : NULL;
 }
 
 /** Check if we have a way to derive something other than a test card.
@@ -200,10 +200,16 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
        mlt_properties properties = mlt_frame_properties( this );
        mlt_get_image get_image = mlt_frame_pop_get_image( this );
        mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
+
+       *width = *width >> 1 << 1;
        
        if ( get_image != NULL )
        {
-               return get_image( this, buffer, format, width, height, writable );
+               int error = 0;
+               mlt_position position = mlt_frame_get_position( this );
+               error = get_image( this, buffer, format, width, height, writable );
+               mlt_frame_set_position( this, position );
+               return error;
        }
        else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
        {
@@ -219,15 +225,14 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
                if ( test_frame != NULL )
                {
                        mlt_properties test_properties = mlt_frame_properties( test_frame );
-                       mlt_properties_set( test_properties, "rescale.interp", "nearest" );
                        mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
+                       mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
                        mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
                        mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
                        mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
                        mlt_properties_set_int( properties, "width", *width );
                        mlt_properties_set_int( properties, "height", *height );
-                       mlt_properties_set( properties, "rescale.interp", "none" );
-                       mlt_properties_set( properties, "scale", "off" );
+                       mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
                }
                else
                {
@@ -237,8 +242,8 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
        }
        else
        {
-               uint8_t *p;
-               uint8_t *q;
+               register uint8_t *p;
+               register uint8_t *q;
                int size = 0;
 
                *width = *width == 0 ? 720 : *width;
@@ -306,10 +311,20 @@ uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
 {
        mlt_properties properties = mlt_frame_properties( this );
+       int hide = mlt_properties_get_int( properties, "test_audio" );
 
-       if ( this->get_audio != NULL )
+       if ( hide == 0 && this->get_audio != NULL )
+       {
+               mlt_position position = mlt_frame_get_position( this );
+               this->get_audio( this, buffer, format, frequency, channels, samples );
+               mlt_frame_set_position( this, position );
+       }
+       else if ( mlt_properties_get_data( properties, "audio", NULL ) )
        {
-               return this->get_audio( this, buffer, format, frequency, channels, samples );
+               *buffer = mlt_properties_get_data( properties, "audio", NULL );
+               *frequency = mlt_properties_get_int( properties, "audio_frequency" );
+               *channels = mlt_properties_get_int( properties, "audio_channels" );
+               *samples = mlt_properties_get_int( properties, "audio_samples" );
        }
        else
        {
@@ -324,12 +339,78 @@ int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *for
                mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
                mlt_properties_set_int( properties, "test_audio", 1 );
        }
+
+       mlt_properties_set_int( properties, "audio_frequency", *frequency );
+       mlt_properties_set_int( properties, "audio_channels", *channels );
+       mlt_properties_set_int( properties, "audio_samples", *samples );
+
        return 0;
 }
 
-void mlt_frame_close( mlt_frame this )
+unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
+{
+       int16_t *pcm = NULL;
+       mlt_properties properties = mlt_frame_properties( this );
+       mlt_audio_format format = mlt_audio_pcm;
+       int frequency = 32000; // lower frequency available?
+       int channels = 2;
+       double fps = mlt_properties_get_double( properties, "fps" );
+       int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
+       
+       // Get the pcm data
+       mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
+       
+       // Make an 8-bit buffer large enough to hold rendering
+       int size = w * h;
+       unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
+       if ( bitmap != NULL )
+               memset( bitmap, 0, size );
+       mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
+       
+       // Render vertical lines
+       int16_t *ubound = pcm + samples * channels;
+       int skip = samples / w - 1;
+       int i, j, k;
+       
+       // Iterate sample stream and along x coordinate
+       for ( i = 0; i < w && pcm < ubound; i++ )
+       {
+               // pcm data has channels interleaved
+               for ( j = 0; j < channels; j++ )
+               {
+                       // Determine sample's magnitude from 2s complement;
+                       int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
+                       // The height of a line is the ratio of the magnitude multiplied by 
+                       // half the vertical resolution
+                       int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
+                       // Determine the starting y coordinate - left channel above center,
+                       // right channel below - currently assumes 2 channels
+                       int displacement = ( h / 2 ) - ( 1 - j ) * height;
+                       // Position buffer pointer using y coordinate, stride, and x coordinate
+                       unsigned char *p = &bitmap[ i + displacement * w ];
+                       
+                       // Draw vertical line
+                       for ( k = 0; k < height; k++ )
+                               p[ w * k ] = 0xFF;
+                       
+                       pcm++;
+               }
+               pcm += skip * channels;
+       }
+
+       return bitmap;
+}
+
+mlt_producer mlt_frame_get_original_producer( mlt_frame this )
 {
        if ( this != NULL )
+               return mlt_properties_get_data( mlt_frame_properties( this ), "_producer", NULL );
+       return NULL;
+}
+
+void mlt_frame_close( mlt_frame this )
+{
+       if ( this != NULL && mlt_properties_dec_ref( mlt_frame_properties( this ) ) <= 0 )
        {
                mlt_deque_close( this->stack_image );
                mlt_deque_close( this->stack_audio );
@@ -729,7 +810,7 @@ int mlt_sample_calculator( float fps, int frequency, int64_t position )
 {
        int samples = 0;
 
-       if ( fps > 29 && fps <= 30 )
+       if ( ( int )( fps * 100 ) == 2997 )
        {
                samples = frequency / 30;