Skip to content

Commit

Permalink
wip! engine: ffmpeg 7.0 support
Browse files Browse the repository at this point in the history
Signed-off-by: David Heidelberg <david@ixit.cz>
  • Loading branch information
okias committed Nov 26, 2024
1 parent bb294bf commit 19f8778
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions rwengine/src/audio/SoundSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern "C" {
#define avio_context_free av_freep
#endif

#define HAVE_CH_LAYOUT (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))

constexpr int kNumOutputChannels = 2;
constexpr AVSampleFormat kOutputFMT = AV_SAMPLE_FMT_S16;
constexpr size_t kNrFramesToPreload = 50;
Expand Down Expand Up @@ -370,10 +372,31 @@ void SoundSource::decodeAndResampleFrames(const std::filesystem::path& filePath,
while ((receiveFrame =
avcodec_receive_frame(codecContext, frame)) == 0) {
if (!swr) {
#if !HAVE_CH_LAYOUT
if (frame->channels == 1 || frame->channel_layout == 0) {
frame->channel_layout =
av_get_default_channel_layout(1);
}
#endif
#if HAVE_CH_LAYOUT
AVChannelLayout out_chlayout = AV_CHANNEL_LAYOUT_STEREO;
int err = swr_alloc_set_opts2(
&swr,
&out_chlayout,
kOutputFMT,
frame->sample_rate,
&frame->ch_layout, // input channel layout
static_cast<AVSampleFormat>(
frame->format), // input format
frame->sample_rate, // input sample rate
0, nullptr);

if (err < 0) {
RW_ERROR(
"Resampler has not been successfully allocated.");
return;
}
#else
swr = swr_alloc_set_opts(
nullptr,
AV_CH_LAYOUT_STEREO, // output channel layout
Expand All @@ -389,6 +412,7 @@ void SoundSource::decodeAndResampleFrames(const std::filesystem::path& filePath,
"Resampler has not been successfully allocated.");
return;
}
#endif
swr_init(swr);
if (!swr_is_initialized(swr)) {
RW_ERROR(
Expand All @@ -400,11 +424,16 @@ void SoundSource::decodeAndResampleFrames(const std::filesystem::path& filePath,
// Decode audio packet
if (receiveFrame == 0 && sendPacket == 0) {
// Write samples to audio buffer
#if HAVE_CH_LAYOUT
resampled->ch_layout = AV_CHANNEL_LAYOUT_STEREO;
#else
resampled->channel_layout = AV_CH_LAYOUT_STEREO;
#endif
resampled->sample_rate = frame->sample_rate;
resampled->format = kOutputFMT;
#if !HAVE_CH_LAYOUT
resampled->channels = kNumOutputChannels;

#endif
swr_config_frame(swr, resampled, frame);

if (swr_convert_frame(swr, resampled, frame) < 0) {
Expand Down Expand Up @@ -474,7 +503,11 @@ void SoundSource::exposeSoundMetadata() {
}

void SoundSource::exposeSfxMetadata(LoaderSDT& sdt) {
#if HAVE_CH_LAYOUT
channels = static_cast<size_t>(codecContext->ch_layout.nb_channels);
#else
channels = static_cast<size_t>(codecContext->channels);
#endif
sampleRate = sdt.assetInfo.sampleRate;
}

Expand Down Expand Up @@ -502,7 +535,7 @@ void SoundSource::loadFromFile(const std::filesystem::path& filePath, bool strea
if (allocateAudioFrame() && allocateFormatContext(filePath) &&
findAudioStream(filePath) && prepareCodecContextWrap()) {
exposeSoundMetadata();
av_init_packet(&readingPacket);
AVPacket* readingPacket = av_packet_alloc();

decodeFramesWrap(filePath);

Expand All @@ -521,7 +554,7 @@ void SoundSource::loadSfx(LoaderSDT& sdt, size_t index, bool asWave,
if (allocateAudioFrame() && prepareFormatContextSfx(sdt, index, asWave) &&
findAudioStreamSfx() && prepareCodecContextSfxWrap()) {
exposeSfxMetadata(sdt);
av_init_packet(&readingPacket);
AVPacket* readingPacket = av_packet_alloc();

decodeFramesSfxWrap();

Expand Down

0 comments on commit 19f8778

Please sign in to comment.