From: ddennedy Date: Sat, 10 Jan 2004 20:16:04 +0000 (+0000) Subject: attempt to retain samples in mlt_frame_mix_audio, make consumers request the number... X-Git-Url: http://research.m1stereo.tv/gitweb?a=commitdiff_plain;h=41cbc901f138ce1ba5ed3cbde5942b91d2b803a0;p=melted attempt to retain samples in mlt_frame_mix_audio, make consumers request the number of samples to get_audio git-svn-id: https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt@50 d19143bc-622f-0410-bfdd-b5b2a6649095 --- diff --git a/mlt/src/framework/mlt_frame.c b/mlt/src/framework/mlt_frame.c index 5a5701a..3b11fcf 100644 --- a/mlt/src/framework/mlt_frame.c +++ b/mlt/src/framework/mlt_frame.c @@ -239,17 +239,20 @@ int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *for } else { + if ( *samples <= 0 ) + *samples = 1920; + if ( *channels <= 0 ) + *channels = 2; + if ( *frequency <= 0 ) + *frequency = 48000; if ( test_card.afmt != *format ) { test_card.afmt = *format; - test_card.audio = realloc( test_card.audio, 1920 * 2 * sizeof( int16_t ) ); - memset( test_card.audio, 0, 1920 * 2 * sizeof( int16_t ) ); + test_card.audio = realloc( test_card.audio, *samples * *channels * sizeof( int16_t ) ); + memset( test_card.audio, 0, *samples * *channels * sizeof( int16_t ) ); } *buffer = test_card.audio; - *frequency = 48000; - *channels = 2; - *samples = 1920; } return 0; } @@ -692,6 +695,9 @@ int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight, int16_t * { int ret = 0; int16_t *p_src, *p_dest; + int16_t *src, *dest; + static int16_t *extra_src = NULL, *extra_dest = NULL; + static int extra_src_samples = 0, extra_dest_samples = 0; int frequency_src, frequency_dest; int channels_src, channels_dest; int samples_src, samples_dest; @@ -699,20 +705,86 @@ int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight, int16_t * mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest ); mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src ); + //fprintf( stderr, "frame dest samples %d channels %d\n", samples_dest, channels_dest ); + //fprintf( stderr, "frame src samples %d channels %d\n", samples_src, channels_src ); + - *samples = samples_src < samples_dest ? samples_src : samples_dest; +#if 0 + // Append new samples to leftovers + if ( extra_dest_samples > 0 ) + { + fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples ); + dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest ); + memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest ); + } + else + dest = p_dest; + if ( extra_src_samples > 0 ) + { + fprintf( stderr, "prepending %d samples to src\n", extra_src_samples ); + src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src ); + memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src ); + } + else + src = p_src; +#else + src = p_src; + dest = p_dest; +#endif + + // determine number of samples to process + if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples ) + *samples = samples_src + extra_src_samples; + else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples ) + *samples = samples_dest + extra_dest_samples; + *channels = channels_src < channels_dest ? channels_src : channels_dest; *buffer = p_dest; + // Mixdown for ( i = 0; i < *samples; i++ ) { for ( j = 0; j < *channels; j++ ) { - double dest = (double) p_dest[ i * channels_dest + j ]; - double src = (double) p_src[ i * channels_src + j ]; - p_dest[ i * channels_dest + j ] = src * weight + dest * ( 1.0 - weight ); + double d = (double) dest[ i * channels_dest + j ]; + double s = (double) src[ i * channels_src + j ]; + dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight ); } } + + // We have to copy --sigh + if ( dest != p_dest ) + memcpy( p_dest, dest, *samples * 2 * *channels ); + +#if 0 + // Store the leftovers + if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples ) + { + extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples ); + size_t size = extra_dest_samples * 2 * channels_dest; + fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples ); + if ( extra_dest ) + free( extra_dest ); + extra_dest = malloc( size ); + if ( extra_dest ) + memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size ); + else + extra_dest_samples = 0; + } + else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples ) + { + extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples ); + size_t size = extra_src_samples * 2 * channels_src; + fprintf( stderr, "storing %d samples from src\n", extra_dest_samples ); + if ( extra_src ) + free( extra_src ); + extra_src = malloc( size ); + if ( extra_src ) + memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size ); + else + extra_src_samples = 0; + } +#endif return ret; } diff --git a/mlt/src/modules/core/transition_luma.c b/mlt/src/modules/core/transition_luma.c index fa6d7c3..f5be459 100644 --- a/mlt/src/modules/core/transition_luma.c +++ b/mlt/src/modules/core/transition_luma.c @@ -352,12 +352,13 @@ static mlt_frame transition_process( mlt_transition transition, mlt_frame a_fram mlt_frame_push_frame( a_frame, b_frame ); /************************ AUDIO ***************************/ +#if 1 // Backup the original get_audio (it's still needed) mlt_properties_set_data( mlt_frame_properties( a_frame ), "get_audio", a_frame->get_audio, 0, NULL, NULL ); // Override the get_audio method a_frame->get_audio = transition_get_audio; - +#endif return a_frame; } diff --git a/mlt/src/modules/sdl/consumer_sdl.c b/mlt/src/modules/sdl/consumer_sdl.c index 2725d1d..943a0d1 100644 --- a/mlt/src/modules/sdl/consumer_sdl.c +++ b/mlt/src/modules/sdl/consumer_sdl.c @@ -199,9 +199,13 @@ static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_aud // Get the properties of this consumer mlt_properties properties = this->properties; mlt_audio_format afmt = mlt_audio_pcm; - int channels = 0; - int samples; - int frequency; + + // Set the preferred params of the test card signal + int channels = 2; + int frequency = 48000; + static int counter = 0; + int samples = mlt_sample_calculator( ( this->height < 576 ? 29.97 : 25 ), frequency, counter++ ); + int16_t *pcm; int bytes; diff --git a/mlt/src/tests/dan.c b/mlt/src/tests/dan.c index a896b14..b33cb7e 100644 --- a/mlt/src/tests/dan.c +++ b/mlt/src/tests/dan.c @@ -23,9 +23,9 @@ int main( int argc, char **argv ) // Create the producer(s) mlt_producer dv1 = mlt_factory_producer( "mcdv", file1 ); - mlt_producer_set_in_and_out( dv1, 300.0, 303.0 ); + mlt_producer_set_in_and_out( dv1, 300, 305 ); - mlt_producer dv2 = mlt_factory_producer( "mcmpeg", file2 ); + mlt_producer dv2 = mlt_factory_producer( "mcdv", file2 ); //mlt_producer_set_in_and_out( dv2, 10.0, 30.0 ); #if 0 @@ -43,22 +43,22 @@ int main( int argc, char **argv ) //mlt_producer dv2 = producer_libdv_init( file2 ); //mlt_producer dv2 = mlt_factory_producer( "pixbuf", file2 ); #if 0 - mlt_producer dv2 = mlt_factory_producer( "pango", NULL ); //"Mutton Lettuce Tomato" ); - mlt_properties_set( mlt_producer_properties( dv2 ), "font", "Sans Bold 36" ); - mlt_properties_set( mlt_producer_properties( dv2 ), "text", "Mutton Lettuce\nTomato" ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "video_standard", mlt_video_standard_ntsc ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "bgcolor", 0x0000007f ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "pad", 8 ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "align", 1 ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "x", -20 ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "y", 40 ); + mlt_producer title = mlt_factory_producer( "pango", NULL ); //"Mutton Lettuce Tomato" ); + mlt_properties_set_int( mlt_producer_properties( title ), "video_standard", mlt_video_standard_ntsc ); + mlt_properties_set( mlt_producer_properties( title ), "font", "Sans Bold 36" ); + mlt_properties_set( mlt_producer_properties( title ), "text", "Mutton Lettuce\nTomato" ); + mlt_properties_set_int( mlt_producer_properties( title ), "bgcolor", 0x0000007f ); + mlt_properties_set_int( mlt_producer_properties( title ), "pad", 8 ); + mlt_properties_set_int( mlt_producer_properties( title ), "align", 1 ); + mlt_properties_set_int( mlt_producer_properties( title ), "x", 20 ); + mlt_properties_set_int( mlt_producer_properties( title ), "y", 40 ); #endif mlt_playlist playlist1 = mlt_playlist_init(); mlt_playlist_append( playlist1, dv1 ); mlt_playlist playlist2 = mlt_playlist_init(); - mlt_playlist_blank( playlist2, 1.0 ); + mlt_playlist_blank( playlist2, 3.0 ); mlt_playlist_append( playlist2, dv2 ); // Register producers(s) with a multitrack object @@ -74,8 +74,8 @@ int main( int argc, char **argv ) // Define a transition mlt_transition transition = mlt_factory_transition( "luma", NULL ); mlt_transition_connect( transition, mlt_multitrack_service( multitrack ), 0, 1 ); - mlt_transition_set_in_and_out( transition, 1.0, 3.0 ); - //mlt_properties_set( mlt_transition_properties( transition ), "filename", "clock.pgm" ); + mlt_transition_set_in_and_out( transition, 3.0, 5.0 ); + mlt_properties_set( mlt_transition_properties( transition ), "filename", "clock.pgm" ); mlt_properties_set_double( mlt_transition_properties( transition ), "softness", 0.1 ); // Buy a tractor and connect it to the filter @@ -92,7 +92,7 @@ int main( int argc, char **argv ) // Close everything... mlt_consumer_close( consumer ); mlt_tractor_close( tractor ); -// mlt_filter_close( filter ); + mlt_transition_close( transition ); mlt_multitrack_close( multitrack ); mlt_producer_close( dv1 ); mlt_producer_close( dv2 ); diff --git a/src/framework/mlt_frame.c b/src/framework/mlt_frame.c index 5a5701a..3b11fcf 100644 --- a/src/framework/mlt_frame.c +++ b/src/framework/mlt_frame.c @@ -239,17 +239,20 @@ int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *for } else { + if ( *samples <= 0 ) + *samples = 1920; + if ( *channels <= 0 ) + *channels = 2; + if ( *frequency <= 0 ) + *frequency = 48000; if ( test_card.afmt != *format ) { test_card.afmt = *format; - test_card.audio = realloc( test_card.audio, 1920 * 2 * sizeof( int16_t ) ); - memset( test_card.audio, 0, 1920 * 2 * sizeof( int16_t ) ); + test_card.audio = realloc( test_card.audio, *samples * *channels * sizeof( int16_t ) ); + memset( test_card.audio, 0, *samples * *channels * sizeof( int16_t ) ); } *buffer = test_card.audio; - *frequency = 48000; - *channels = 2; - *samples = 1920; } return 0; } @@ -692,6 +695,9 @@ int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight, int16_t * { int ret = 0; int16_t *p_src, *p_dest; + int16_t *src, *dest; + static int16_t *extra_src = NULL, *extra_dest = NULL; + static int extra_src_samples = 0, extra_dest_samples = 0; int frequency_src, frequency_dest; int channels_src, channels_dest; int samples_src, samples_dest; @@ -699,20 +705,86 @@ int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight, int16_t * mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest ); mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src ); + //fprintf( stderr, "frame dest samples %d channels %d\n", samples_dest, channels_dest ); + //fprintf( stderr, "frame src samples %d channels %d\n", samples_src, channels_src ); + - *samples = samples_src < samples_dest ? samples_src : samples_dest; +#if 0 + // Append new samples to leftovers + if ( extra_dest_samples > 0 ) + { + fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples ); + dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest ); + memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest ); + } + else + dest = p_dest; + if ( extra_src_samples > 0 ) + { + fprintf( stderr, "prepending %d samples to src\n", extra_src_samples ); + src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src ); + memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src ); + } + else + src = p_src; +#else + src = p_src; + dest = p_dest; +#endif + + // determine number of samples to process + if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples ) + *samples = samples_src + extra_src_samples; + else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples ) + *samples = samples_dest + extra_dest_samples; + *channels = channels_src < channels_dest ? channels_src : channels_dest; *buffer = p_dest; + // Mixdown for ( i = 0; i < *samples; i++ ) { for ( j = 0; j < *channels; j++ ) { - double dest = (double) p_dest[ i * channels_dest + j ]; - double src = (double) p_src[ i * channels_src + j ]; - p_dest[ i * channels_dest + j ] = src * weight + dest * ( 1.0 - weight ); + double d = (double) dest[ i * channels_dest + j ]; + double s = (double) src[ i * channels_src + j ]; + dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight ); } } + + // We have to copy --sigh + if ( dest != p_dest ) + memcpy( p_dest, dest, *samples * 2 * *channels ); + +#if 0 + // Store the leftovers + if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples ) + { + extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples ); + size_t size = extra_dest_samples * 2 * channels_dest; + fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples ); + if ( extra_dest ) + free( extra_dest ); + extra_dest = malloc( size ); + if ( extra_dest ) + memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size ); + else + extra_dest_samples = 0; + } + else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples ) + { + extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples ); + size_t size = extra_src_samples * 2 * channels_src; + fprintf( stderr, "storing %d samples from src\n", extra_dest_samples ); + if ( extra_src ) + free( extra_src ); + extra_src = malloc( size ); + if ( extra_src ) + memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size ); + else + extra_src_samples = 0; + } +#endif return ret; } diff --git a/src/modules/core/transition_luma.c b/src/modules/core/transition_luma.c index fa6d7c3..f5be459 100644 --- a/src/modules/core/transition_luma.c +++ b/src/modules/core/transition_luma.c @@ -352,12 +352,13 @@ static mlt_frame transition_process( mlt_transition transition, mlt_frame a_fram mlt_frame_push_frame( a_frame, b_frame ); /************************ AUDIO ***************************/ +#if 1 // Backup the original get_audio (it's still needed) mlt_properties_set_data( mlt_frame_properties( a_frame ), "get_audio", a_frame->get_audio, 0, NULL, NULL ); // Override the get_audio method a_frame->get_audio = transition_get_audio; - +#endif return a_frame; } diff --git a/src/modules/sdl/consumer_sdl.c b/src/modules/sdl/consumer_sdl.c index 2725d1d..943a0d1 100644 --- a/src/modules/sdl/consumer_sdl.c +++ b/src/modules/sdl/consumer_sdl.c @@ -199,9 +199,13 @@ static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_aud // Get the properties of this consumer mlt_properties properties = this->properties; mlt_audio_format afmt = mlt_audio_pcm; - int channels = 0; - int samples; - int frequency; + + // Set the preferred params of the test card signal + int channels = 2; + int frequency = 48000; + static int counter = 0; + int samples = mlt_sample_calculator( ( this->height < 576 ? 29.97 : 25 ), frequency, counter++ ); + int16_t *pcm; int bytes; diff --git a/src/tests/dan.c b/src/tests/dan.c index a896b14..b33cb7e 100644 --- a/src/tests/dan.c +++ b/src/tests/dan.c @@ -23,9 +23,9 @@ int main( int argc, char **argv ) // Create the producer(s) mlt_producer dv1 = mlt_factory_producer( "mcdv", file1 ); - mlt_producer_set_in_and_out( dv1, 300.0, 303.0 ); + mlt_producer_set_in_and_out( dv1, 300, 305 ); - mlt_producer dv2 = mlt_factory_producer( "mcmpeg", file2 ); + mlt_producer dv2 = mlt_factory_producer( "mcdv", file2 ); //mlt_producer_set_in_and_out( dv2, 10.0, 30.0 ); #if 0 @@ -43,22 +43,22 @@ int main( int argc, char **argv ) //mlt_producer dv2 = producer_libdv_init( file2 ); //mlt_producer dv2 = mlt_factory_producer( "pixbuf", file2 ); #if 0 - mlt_producer dv2 = mlt_factory_producer( "pango", NULL ); //"Mutton Lettuce Tomato" ); - mlt_properties_set( mlt_producer_properties( dv2 ), "font", "Sans Bold 36" ); - mlt_properties_set( mlt_producer_properties( dv2 ), "text", "Mutton Lettuce\nTomato" ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "video_standard", mlt_video_standard_ntsc ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "bgcolor", 0x0000007f ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "pad", 8 ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "align", 1 ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "x", -20 ); - mlt_properties_set_int( mlt_producer_properties( dv2 ), "y", 40 ); + mlt_producer title = mlt_factory_producer( "pango", NULL ); //"Mutton Lettuce Tomato" ); + mlt_properties_set_int( mlt_producer_properties( title ), "video_standard", mlt_video_standard_ntsc ); + mlt_properties_set( mlt_producer_properties( title ), "font", "Sans Bold 36" ); + mlt_properties_set( mlt_producer_properties( title ), "text", "Mutton Lettuce\nTomato" ); + mlt_properties_set_int( mlt_producer_properties( title ), "bgcolor", 0x0000007f ); + mlt_properties_set_int( mlt_producer_properties( title ), "pad", 8 ); + mlt_properties_set_int( mlt_producer_properties( title ), "align", 1 ); + mlt_properties_set_int( mlt_producer_properties( title ), "x", 20 ); + mlt_properties_set_int( mlt_producer_properties( title ), "y", 40 ); #endif mlt_playlist playlist1 = mlt_playlist_init(); mlt_playlist_append( playlist1, dv1 ); mlt_playlist playlist2 = mlt_playlist_init(); - mlt_playlist_blank( playlist2, 1.0 ); + mlt_playlist_blank( playlist2, 3.0 ); mlt_playlist_append( playlist2, dv2 ); // Register producers(s) with a multitrack object @@ -74,8 +74,8 @@ int main( int argc, char **argv ) // Define a transition mlt_transition transition = mlt_factory_transition( "luma", NULL ); mlt_transition_connect( transition, mlt_multitrack_service( multitrack ), 0, 1 ); - mlt_transition_set_in_and_out( transition, 1.0, 3.0 ); - //mlt_properties_set( mlt_transition_properties( transition ), "filename", "clock.pgm" ); + mlt_transition_set_in_and_out( transition, 3.0, 5.0 ); + mlt_properties_set( mlt_transition_properties( transition ), "filename", "clock.pgm" ); mlt_properties_set_double( mlt_transition_properties( transition ), "softness", 0.1 ); // Buy a tractor and connect it to the filter @@ -92,7 +92,7 @@ int main( int argc, char **argv ) // Close everything... mlt_consumer_close( consumer ); mlt_tractor_close( tractor ); -// mlt_filter_close( filter ); + mlt_transition_close( transition ); mlt_multitrack_close( multitrack ); mlt_producer_close( dv1 ); mlt_producer_close( dv2 );