Skip to content

Commit

Permalink
Fix AudioWriter from failing after libavcodec updates
Browse files Browse the repository at this point in the history
Since FFmpeg 4.4.2 the code interfacing with libavcodec started to fail
with "Invalid argument" error.

This commit:
- Fixes the "Invalid argument" error by specifying explicitly the number of
  channels in frames.
- Improves error message for diagnostics.
  • Loading branch information
dbogdanov committed Jul 13, 2023
1 parent 062928c commit 9ceef0a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/essentia/utils/audiocontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ void AudioContext::encodePacket(int size) {
frame->nb_samples = _codecCtx->frame_size;
frame->format = _codecCtx->sample_fmt;
frame->channel_layout = _codecCtx->channel_layout;
frame->channels = _codecCtx->channels;

int result = avcodec_fill_audio_frame(frame, _codecCtx->channels, _codecCtx->sample_fmt,
bufferFmt, outputPlaneSize * _codecCtx->channels, 0);
Expand All @@ -328,8 +329,13 @@ void AudioContext::encodePacket(int size) {
packet.size = 0;

int got_output;
if (avcodec_encode_audio2(_codecCtx, &packet, frame, &got_output) < 0) {
throw EssentiaException("Error while encoding audio frame");
result = avcodec_encode_audio2(_codecCtx, &packet, frame, &got_output);
if (result < 0) {
char errstring[1204];
av_strerror(result, errstring, sizeof(errstring));
ostringstream msg;
msg << "Error while encoding audio frame: " << errstring;
throw EssentiaException(msg);
}

if (got_output) { // packet is not empty, write the frame in the media file
Expand Down

0 comments on commit 9ceef0a

Please sign in to comment.