From 4806f2ff7566bb243f7c4e897a3b7781340fcda2 Mon Sep 17 00:00:00 2001 From: Chris Kirmse Date: Wed, 18 Dec 2019 18:27:04 -0800 Subject: [PATCH] fix incorrect buffer size being passed to avcodec_fill_audio_frame - most of the time the fixed number being sent in was large enough, but not always - also added logging if avcodec_fill_audio_frame fails --- src/FFmpegWriter.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 645c0cca..f5c85467 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -1482,7 +1482,8 @@ void FFmpegWriter::write_audio_packets(bool is_final) { ChannelLayout channel_layout_in_frame = LAYOUT_MONO; // default channel layout // Create a new array (to hold all S16 audio samples, for the current queued frames - int16_t *all_queued_samples = (int16_t *) av_malloc((sizeof(int16_t) * (queued_audio_frames.size() * AVCODEC_MAX_AUDIO_FRAME_SIZE))); + unsigned int all_queued_samples_size = sizeof(int16_t) * (queued_audio_frames.size() * AVCODEC_MAX_AUDIO_FRAME_SIZE); + int16_t *all_queued_samples = (int16_t *) av_malloc(all_queued_samples_size); int16_t *all_resampled_samples = NULL; int16_t *final_samples_planar = NULL; int16_t *final_samples = NULL; @@ -1542,8 +1543,10 @@ void FFmpegWriter::write_audio_packets(bool is_final) { audio_frame->nb_samples = total_frame_samples / channels_in_frame; // Fill input frame with sample data - avcodec_fill_audio_frame(audio_frame, channels_in_frame, AV_SAMPLE_FMT_S16, (uint8_t *) all_queued_samples, - audio_encoder_buffer_size, 0); + int error_code = avcodec_fill_audio_frame(audio_frame, channels_in_frame, AV_SAMPLE_FMT_S16, (uint8_t *) all_queued_samples, all_queued_samples_size, 0); + if (error_code < 0) { + ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code); + } // Do not convert audio to planar format (yet). We need to keep everything interleaved at this point. switch (audio_codec->sample_fmt) {