[FFmpeg-devel] [PATCH] lavc/audiotoolboxenc: fix noise in encoded audio

zhangjiejun1992 at gmail.com zhangjiejun1992 at gmail.com
Tue Jan 2 09:52:23 EET 2018


From: Jiejun Zhang <zhangjiejun1992 at gmail.com>

This fixes #6940
---
 libavcodec/audiotoolboxenc.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c
index 71885d1530..b70375a692 100644
--- a/libavcodec/audiotoolboxenc.c
+++ b/libavcodec/audiotoolboxenc.c
@@ -48,6 +48,9 @@ typedef struct ATDecodeContext {
     AudioFrameQueue afq;
     int eof;
     int frame_size;
+
+    uint8_t* audio_data_buf;
+    uint32_t audio_data_buf_size;
 } ATDecodeContext;
 
 static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
@@ -442,6 +445,9 @@ static av_cold int ffat_init_encoder(AVCodecContext *avctx)
 
     ff_af_queue_init(avctx, &at->afq);
 
+    at->audio_data_buf_size = 0;
+    at->audio_data_buf = NULL;
+
     return 0;
 }
 
@@ -471,7 +477,15 @@ static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_pac
     data->mBuffers[0].mDataByteSize   = frame->nb_samples *
                                         av_get_bytes_per_sample(avctx->sample_fmt) *
                                         avctx->channels;
-    data->mBuffers[0].mData           = frame->data[0];
+    if (at->audio_data_buf_size < data->mBuffers[0].mDataByteSize) {
+        av_log(avctx, AV_LOG_INFO, "Increasing audio data buffer size to %u",
+               data->mBuffers[0].mDataByteSize);
+        av_free(at->audio_data_buf);
+        at->audio_data_buf_size = data->mBuffers[0].mDataByteSize;
+        at->audio_data_buf = av_malloc(at->audio_data_buf_size);
+    }
+    memcpy(at->audio_data_buf, frame->data[0], data->mBuffers[0].mDataByteSize);
+    data->mBuffers[0].mData           = at->audio_data_buf;
     if (*nb_packets > frame->nb_samples)
         *nb_packets = frame->nb_samples;
 
@@ -565,6 +579,10 @@ static av_cold int ffat_close_encoder(AVCodecContext *avctx)
     ff_bufqueue_discard_all(&at->frame_queue);
     ff_bufqueue_discard_all(&at->used_frame_queue);
     ff_af_queue_close(&at->afq);
+    if (at->audio_data_buf_size > 0) {
+        at->audio_data_buf_size = 0;
+        av_free(at->audio_data_buf);
+    }
     return 0;
 }
 
-- 
2.14.3 (Apple Git-98)



More information about the ffmpeg-devel mailing list