Fix to compositing/watermark; miracle/mlt shutdown cleanup
[melted] / src / framework / mlt_frame.c
index 09eb6f0..e3e4f3f 100644 (file)
@@ -53,6 +53,7 @@ mlt_frame mlt_frame_init( )
                        mlt_properties_set_int( properties, "height", 576 );
                        mlt_properties_set_int( properties, "normalised_width", 720 );
                        mlt_properties_set_int( properties, "normalised_height", 576 );
+                       mlt_properties_set_double( properties, "aspect_ratio", 72.0/79.0 );
                }
                else
                {
@@ -60,16 +61,15 @@ mlt_frame mlt_frame_init( )
                        mlt_properties_set_int( properties, "height", 480 );
                        mlt_properties_set_int( properties, "normalised_width", 720 );
                        mlt_properties_set_int( properties, "normalised_height", 480 );
+                       mlt_properties_set_double( properties, "aspect_ratio", 128.0/117.0 );
                }
 
-               mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
                mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
                mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
 
                // Construct stacks for frames and methods
-               this->stack_get_image = mlt_deque_init( );
-               this->stack_frame = mlt_deque_init( );
-               this->stack_service = mlt_deque_init( );
+               this->stack_image = mlt_deque_init( );
+               this->stack_audio = mlt_deque_init( );
        }
 
        return this;
@@ -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.
@@ -88,7 +88,7 @@ mlt_properties mlt_frame_properties( mlt_frame this )
 
 int mlt_frame_is_test_card( mlt_frame this )
 {
-       return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
+       return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
 }
 
 /** Check if we have a way to derive something than test audio.
@@ -136,7 +136,7 @@ int mlt_frame_set_position( mlt_frame this, mlt_position value )
 
 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
 {
-       return mlt_deque_push_back( this->stack_get_image, get_image );
+       return mlt_deque_push_back( this->stack_image, get_image );
 }
 
 /** Pop a get_image callback.
@@ -144,7 +144,7 @@ int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
 
 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
 {
-       return mlt_deque_pop_back( this->stack_get_image );
+       return mlt_deque_pop_back( this->stack_image );
 }
 
 /** Push a frame.
@@ -152,7 +152,7 @@ mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
 
 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
 {
-       return mlt_deque_push_back( this->stack_frame, that );
+       return mlt_deque_push_back( this->stack_image, that );
 }
 
 /** Pop a frame.
@@ -160,7 +160,7 @@ int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
 
 mlt_frame mlt_frame_pop_frame( mlt_frame this )
 {
-       return mlt_deque_pop_back( this->stack_frame );
+       return mlt_deque_pop_back( this->stack_image );
 }
 
 /** Push a service.
@@ -168,7 +168,7 @@ mlt_frame mlt_frame_pop_frame( mlt_frame this )
 
 int mlt_frame_push_service( mlt_frame this, void *that )
 {
-       return mlt_deque_push_back( this->stack_service, that );
+       return mlt_deque_push_back( this->stack_image, that );
 }
 
 /** Pop a service.
@@ -176,7 +176,23 @@ int mlt_frame_push_service( mlt_frame this, void *that )
 
 void *mlt_frame_pop_service( mlt_frame this )
 {
-       return mlt_deque_pop_back( this->stack_service );
+       return mlt_deque_pop_back( this->stack_image );
+}
+
+/** Push an audio item on the stack.
+*/
+
+int mlt_frame_push_audio( mlt_frame this, void *that )
+{
+       return mlt_deque_push_back( this->stack_audio, that );
+}
+
+/** Pop an audio item from the stack
+*/
+
+void *mlt_frame_pop_audio( mlt_frame this )
+{
+       return mlt_deque_pop_back( this->stack_audio );
 }
 
 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
@@ -184,6 +200,8 @@ 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 )
        {
@@ -204,13 +222,15 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
                {
                        mlt_properties test_properties = mlt_frame_properties( test_frame );
                        mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
-                       mlt_properties_set_double( test_properties, "consumer_scale", mlt_properties_get_double( properties, "consumer_scale" ) );
-                       mlt_properties_set( test_properties, "rescale.interp", "nearest" );
+                       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_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
+                       mlt_properties_set( properties, "rescale.interp", "none" );
+                       mlt_properties_set( properties, "scale", "off" );
                }
                else
                {
@@ -220,8 +240,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;
@@ -230,6 +250,7 @@ int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *for
 
                mlt_properties_set_int( properties, "width", *width );
                mlt_properties_set_int( properties, "height", *height );
+               mlt_properties_set_int( properties, "aspect_ratio", 1 );
 
                switch( *format )
                {
@@ -291,7 +312,14 @@ int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *for
 
        if ( this->get_audio != NULL )
        {
-               return this->get_audio( this, buffer, format, frequency, channels, samples );
+               this->get_audio( this, buffer, format, frequency, channels, samples );
+       }
+       else if ( mlt_properties_get_data( properties, "audio", NULL ) )
+       {
+               *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
        {
@@ -306,16 +334,75 @@ 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;
 }
 
+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;
+}
+
+
 void mlt_frame_close( mlt_frame this )
 {
-       if ( this != NULL )
+       if ( this != NULL && mlt_properties_dec_ref( mlt_frame_properties( this ) ) <= 0 )
        {
-               mlt_deque_close( this->stack_get_image );
-               mlt_deque_close( this->stack_frame );
-               mlt_deque_close( this->stack_service );
+               mlt_deque_close( this->stack_image );
+               mlt_deque_close( this->stack_audio );
                mlt_properties_close( &this->parent );
                free( this );
        }
@@ -323,30 +410,6 @@ void mlt_frame_close( mlt_frame this )
 
 /***** convenience functions *****/
 
-/* this macro scales rgb into the yuv gamut, y is scaled by 219/255 and uv by 224/255 */
-#define RGB2YUV(r, g, b, y, u, v)\
-  y = ((257*r + 504*g + 98*b) >> 10) + 16;\
-  u = ((-148*r - 291*g + 439*b) >> 10) + 128;\
-  v = ((439*r - 368*g - 71*b) >> 10) + 128;\
-  y = y < 16 ? 16 : y;\
-  u = u < 16 ? 16 : u;\
-  v = v < 16 ? 16 : v;\
-  y = y > 235 ? 235 : y;\
-  u = u > 240 ? 240 : u;\
-  v = v > 240 ? 240 : v
-
-/* this macro assumes the user has already scaled their rgb down into the broadcast limits */
-#define RGB2YUV_UNSCALED(r, g, b, y, u, v)\
-  y = (299*r + 587*g + 114*b) >> 10;\
-  u = ((-169*r - 331*g + 500*b) >> 10) + 128;\
-  v = ((500*r - 419*g - 81*b) >> 10) + 128;\
-  y = y < 16 ? 16 : y;\
-  u = u < 16 ? 16 : u;\
-  v = v < 16 ? 16 : v;\
-  y = y > 235 ? 235 : y;\
-  u = u > 240 ? 240 : u;\
-  v = v > 240 ? 240 : v
-  
 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
 {
        int ret = 0;
@@ -503,6 +566,8 @@ void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input
 
        int active_width = 2 * iwidth;
        int inactive_width = out_x_range - in_x_range;
+       uint8_t *p = NULL;
+       uint8_t *end = NULL;
 
        // Loop for the entirety of our output height.
        while ( iheight -- )
@@ -519,8 +584,13 @@ void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input
                }
 
                // We're in the input range for this row.
-               memcpy( out_ptr, in_middle + in_line, active_width );
-               out_ptr += active_width;
+               p = in_middle + in_line;
+               end = out_ptr + active_width;
+               while ( out_ptr != end )
+               {
+                       *out_ptr ++ = *p ++;
+                       *out_ptr ++ = *p ++;
+               }
 
                // Fill the outer part with black
                elements = inactive_width;
@@ -729,7 +799,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;
 
@@ -770,4 +840,3 @@ int mlt_sample_calculator( float fps, int frequency, int64_t position )
 
        return samples;
 }
-