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

Jiejun Zhang zhangjiejun1992 at gmail.com
Tue Jan 2 16:27:49 EET 2018

On Tue, Jan 2, 2018 at 8:03 PM, Carl Eugen Hoyos <ceffmpeg at gmail.com> wrote:
> 2018-01-02 8:52 GMT+01:00  <zhangjiejun1992 at gmail.com>:
>> @@ -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);
>> +    }
> The if() looks unnecessary.

Yes. I'll remove it. Thanks for pointing it out.

> Could you explain what this patch changes?
> From a quick look, until now a buffer a was used with a calculated size x.
> After the patch, a buffer b with the same calculated size x is allocated,
> and x bytes are copied from a to b.
> What do I miss?

Although undocumented, AudioToolbox seems to require the data supplied
by the callback (i.e. ffat_encode_callback) being unchanged until the
next time the callback is called. In the old implementation, the
AVBuffer backing the frame is recycled after the frame is freed, and
somebody else (maybe the decoder) will write into the AVBuffer and
change the data. AudioToolbox then encodes some wrong data and noise
is produced. Copying the data to a separate buffer solves this

More information about the ffmpeg-devel mailing list