ffmpeg: fix missing initialization of AVFrame

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 <[email protected]>
Reviewed-by: Jérôme Duval <[email protected]>
This commit is contained in:
PulkoMandy
2023-01-25 11:58:15 +00:00
committed by Adrien Destugues
parent 5500d61686
commit e35a296ef7
@@ -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));