]> git.sesse.net Git - mlt/blobdiff - src/modules/avformat/producer_avformat.c
Fix audio buffer overflow on large resampling (2902193).
[mlt] / src / modules / avformat / producer_avformat.c
index 798b005c578bed7ba53627eac27a24ffce8df795..2d0be647bc3c5fe446c602d8cf597cd945eb70ee 100644 (file)
@@ -85,6 +85,7 @@ struct producer_avformat_s
        int max_channel;
        int max_frequency;
        unsigned int invalid_pts_counter;
+       double resample_factor;
 };
 typedef struct producer_avformat_s *producer_avformat;
 
@@ -154,6 +155,8 @@ mlt_producer producer_avformat_init( mlt_profile profile, char *file )
 
                        // Register our get_frame implementation
                        producer->get_frame = producer_get_frame;
+                       
+                       this->resample_factor = 1.0;
 
                        // Open the file
                        if ( producer_open( this, profile, file ) != 0 )
@@ -695,7 +698,7 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                AVPicture output;
                output.data[0] = buffer;
                output.data[1] = buffer + width * height;
-               output.data[2] = buffer + ( 3 * width * height ) / 2;
+               output.data[2] = buffer + ( 5 * width * height ) / 4;
                output.linesize[0] = width;
                output.linesize[1] = width >> 1;
                output.linesize[2] = width >> 1;
@@ -739,7 +742,7 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                AVPicture pict;
                pict.data[0] = buffer;
                pict.data[1] = buffer + width * height;
-               pict.data[2] = buffer + ( 3 * width * height ) / 2;
+               pict.data[2] = buffer + ( 5 * width * height ) / 4;
                pict.linesize[0] = width;
                pict.linesize[1] = width >> 1;
                pict.linesize[2] = width >> 1;
@@ -1216,12 +1219,19 @@ static int video_codec_init( producer_avformat this, int index, mlt_properties p
                // Determine the fps first from the codec
                double source_fps = (double) this->video_codec->time_base.den /
                                                                   ( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
-
-               // If the muxer reports a frame rate different than the codec
-               double muxer_fps = av_q2d( stream->r_frame_rate );
-               if ( source_fps != muxer_fps )
+               
+               if ( mlt_properties_get( properties, "force_fps" ) )
+               {
+                       source_fps = mlt_properties_get_double( properties, "force_fps" );
+                       stream->time_base = av_d2q( source_fps, 255 );
+               }
+               else
+               {
+                       // If the muxer reports a frame rate different than the codec
+                       double muxer_fps = av_q2d( stream->r_frame_rate );
                        // Choose the lesser - the wrong tends to be off by some multiple of 10
-                       source_fps = muxer_fps < source_fps ? muxer_fps : source_fps;
+                       source_fps = FFMIN( source_fps, muxer_fps );
+               }
 
                // We'll use fps if it's available
                if ( source_fps > 0 )
@@ -1363,7 +1373,7 @@ static int seek_audio( producer_avformat this, mlt_position position, double tim
        return paused;
 }
 
-static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int samples, double timecode, double source_fps )
+static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int channels, int samples, double timecode, double source_fps )
 {
        // Fetch the audio_format
        AVFormatContext *context = this->audio_format;
@@ -1382,7 +1392,6 @@ static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int
        int16_t *decode_buffer = this->decode_buffer[ index ];
 
        int audio_used = this->audio_used[ index ];
-       int channels = codec_context->channels;
        uint8_t *ptr = pkt->data;
        int len = pkt->size;
        int ret = 0;
@@ -1411,26 +1420,28 @@ static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int
                // If decoded successfully
                if ( data_size > 0 )
                {
+                       // Figure out how many samples will be needed after resampling
+                       int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
+                       int samples_needed = lrint( this->resample_factor * convert_samples );
+                       
                        // Resize audio buffer to prevent overflow
-                       if ( audio_used * channels + data_size > this->audio_buffer_size[ index ] )
+                       if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
                        {
-                               mlt_pool_release( this->audio_buffer[ index ] );
-                               this->audio_buffer_size[ index ] = audio_used * channels * sizeof(int16_t) + data_size * 2;
-                               audio_buffer = this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] );
+                               this->audio_buffer_size[ index ] *= 2;
+                               audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
                        }
                        if ( resample )
                        {
                                // Copy to audio buffer while resampling
                                int16_t *source = decode_buffer;
                                int16_t *dest = &audio_buffer[ audio_used * channels ];
-                               int convert_samples = data_size / channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
                                audio_used += audio_resample( resample, dest, source, convert_samples );
                        }
                        else
                        {
                                // Straight copy to audio buffer
-                               memcpy( &audio_buffer[ audio_used * channels ], decode_buffer, data_size );
-                               audio_used += data_size / channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
+                               memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
+                               audio_used += convert_samples;
                        }
 
                        // Handle ignore
@@ -1438,7 +1449,8 @@ static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int
                        {
                                *ignore -= 1;
                                audio_used -= samples;
-                               memmove( audio_buffer, &audio_buffer[ samples * channels ], audio_used * sizeof( int16_t ) );
+                               memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
+                                        audio_used * sizeof( int16_t ) );
                        }
                }
        }
@@ -1486,7 +1498,7 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format
 
        // Fetch the audio_format
        AVFormatContext *context = this->audio_format;
-
+       
        // Determine the tracks to use
        int index = this->audio_index;
        int index_max = this->audio_index + 1;
@@ -1509,6 +1521,12 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format
                        // Check for resample and create if necessary
                        if ( codec_context->channels <= 2 )
                        {
+                               // Determine by how much resampling will increase number of samples
+                               double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
+                               resample_factor *= (double) *frequency / codec_context->sample_rate;
+                               if ( resample_factor > this->resample_factor )
+                                       this->resample_factor = resample_factor;
+                               
                                // Create the resampler
 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
                                this->audio_resample[ index ] = av_audio_resample_init(
@@ -1527,8 +1545,8 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format
                        }
 
                        // Check for audio buffer and create if necessary
-                       this->audio_buffer[ index ] = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
                        this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+                       this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
 
                        // Check for decoder buffer and create if necessary
                        this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
@@ -1559,7 +1577,7 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format
                        // We only deal with audio from the selected audio index
                        if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
                                 ( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
-                               ret = decode_audio( this, &ignore, &pkt, *samples, real_timecode, source_fps );
+                               ret = decode_audio( this, &ignore, &pkt, *channels, *samples, real_timecode, source_fps );
                        av_free_packet( &pkt );
 
                        if ( this->audio_index == INT_MAX && ret >= 0 )