From e35a296ef741ac78419683680a5ed7d0103eebdf Mon Sep 17 00:00:00 2001 From: PulkoMandy Date: Sat, 21 Jan 2023 17:40:01 +0100 Subject: [PATCH] ffmpeg: fix missing initialization of AVFrame MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ffmpeg API reference does not allow to allocate AVFrame on the stack, and especially not without initializing anything in the frame. Calling av_frame_unref then attempts to free some non-existing data, and crashes. The code for video was already using a correctly allocated frame, which we can reuse here. Fixes #17415 Probably fixes #16831 Change-Id: I9d6ee7724ab9f22547b0030de12542ef3a650640 fix avframe Change-Id: I170597ec323ac67be460ccbab9dae5ee3e6e1b71 Reviewed-on: https://review.haiku-os.org/c/haiku/+/6015 Tested-by: Commit checker robot Reviewed-by: Jérôme Duval --- .../media/plugins/ffmpeg/AVCodecEncoder.cpp | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp index 9b3df63f0e..b3d1a4bd48 100644 --- a/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp +++ b/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp @@ -541,27 +541,26 @@ AVCodecEncoder::_EncodeAudio(const uint8* buffer, size_t bufferSize, packet.data = NULL; packet.size = 0; - // We need to wrap our input data into an AVFrame structure. - AVFrame frame; int gotPacket = 0; if (buffer) { - av_frame_unref(&frame); + av_frame_unref(fFrame); + fFrame->nb_samples = frameCount; - frame.nb_samples = frameCount; - - ret = avcodec_fill_audio_frame(&frame, fCodecContext->channels, + ret = avcodec_fill_audio_frame(fFrame, fCodecContext->channels, fCodecContext->sample_fmt, (const uint8_t *) buffer, bufferSize, 1); - if (ret != 0) + if (ret != 0) { + TRACE(" avcodec_encode_audio() failed filling data: %ld\n", ret); return B_ERROR; + } /* Set the presentation time of the frame */ - frame.pts = (bigtime_t)(fFramesWritten * 1000000LL + fFrame->pts = (bigtime_t)(fFramesWritten * 1000000LL / fInputFormat.u.raw_audio.frame_rate); - fFramesWritten += frame.nb_samples; + fFramesWritten += fFrame->nb_samples; - ret = avcodec_send_frame(fCodecContext, &frame); + ret = avcodec_send_frame(fCodecContext, fFrame); gotPacket = avcodec_receive_packet(fCodecContext, &packet) == 0; } else { // If called with NULL, ask the encoder to flush any buffers it may @@ -570,8 +569,8 @@ AVCodecEncoder::_EncodeAudio(const uint8* buffer, size_t bufferSize, gotPacket = (ret == 0); } - if (buffer && frame.extended_data != frame.data) - av_freep(&frame.extended_data); + if (buffer && fFrame->extended_data != fFrame->data) + av_freep(&fFrame->extended_data); if (ret != 0) { TRACE(" avcodec_encode_audio() failed: %s\n", strerror(ret));