Skip to content

Commit

Permalink
Support compiling with ffmpeg 5 and greater
Browse files Browse the repository at this point in the history
  • Loading branch information
cykoder committed Aug 3, 2024
1 parent d45d251 commit 61cb5b4
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 25 deletions.
130 changes: 107 additions & 23 deletions apps/openmw/mwsound/ffmpeg_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <components/debug/debuglog.hpp>
#include <components/vfs/manager.hpp>

#if FFMPEG_5_OR_GREATER
#include <libavutil/channel_layout.h>
#endif

namespace MWSound
{
void AVIOContextDeleter::operator()(AVIOContext* ptr) const
Expand Down Expand Up @@ -57,7 +61,11 @@ namespace MWSound
}
}

#if FFMPEG_CONST_WRITEPACKET
int FFmpeg_Decoder::writePacket(void*, const uint8_t*, int)
#else
int FFmpeg_Decoder::writePacket(void*, uint8_t*, int)
#endif
{
Log(Debug::Error) << "can't write to read-only stream";
return -1;
Expand Down Expand Up @@ -152,7 +160,11 @@ namespace MWSound
if (!mDataBuf || mDataBufLen < mFrame->nb_samples)
{
av_freep(&mDataBuf);
#if FFMPEG_5_OR_GREATER
if (av_samples_alloc(&mDataBuf, nullptr, mOutputChannelLayout.nb_channels,
#else
if (av_samples_alloc(&mDataBuf, nullptr, av_get_channel_layout_nb_channels(mOutputChannelLayout),
#endif
mFrame->nb_samples, mOutputSampleFormat, 0)
< 0)
return false;
Expand Down Expand Up @@ -189,7 +201,11 @@ namespace MWSound
if (!getAVAudioData())
break;
mFramePos = 0;
#if FFMPEG_5_OR_GREATER
mFrameSize = mFrame->nb_samples * mOutputChannelLayout.nb_channels
#else
mFrameSize = mFrame->nb_samples * av_get_channel_layout_nb_channels(mOutputChannelLayout)
#endif
* av_get_bytes_per_sample(mOutputSampleFormat);
}

Expand Down Expand Up @@ -277,11 +293,19 @@ namespace MWSound
else
mOutputSampleFormat = AV_SAMPLE_FMT_S16;

#if FFMPEG_5_OR_GREATER
mOutputChannelLayout = (*stream)->codecpar->ch_layout; // sefault
if (mOutputChannelLayout.u.mask == 0)
av_channel_layout_default(&mOutputChannelLayout, codecCtxPtr->ch_layout.nb_channels);

codecCtxPtr->ch_layout = mOutputChannelLayout;
#else
mOutputChannelLayout = (*stream)->codecpar->channel_layout;
if (mOutputChannelLayout == 0)
mOutputChannelLayout = av_get_default_channel_layout(codecCtxPtr->channels);

codecCtxPtr->channel_layout = mOutputChannelLayout;
#endif

mIoCtx = std::move(ioCtx);
mFrame = std::move(frame);
Expand Down Expand Up @@ -332,41 +356,88 @@ namespace MWSound
*type = SampleType_Int16;
}

if (mOutputChannelLayout == AV_CH_LAYOUT_MONO)
*chans = ChannelConfig_Mono;
else if (mOutputChannelLayout == AV_CH_LAYOUT_STEREO)
*chans = ChannelConfig_Stereo;
else if (mOutputChannelLayout == AV_CH_LAYOUT_QUAD)
*chans = ChannelConfig_Quad;
else if (mOutputChannelLayout == AV_CH_LAYOUT_5POINT1)
*chans = ChannelConfig_5point1;
else if (mOutputChannelLayout == AV_CH_LAYOUT_7POINT1)
*chans = ChannelConfig_7point1;
else
#if FFMPEG_5_OR_GREATER
switch (mOutputChannelLayout.u.mask)
#else
switch (mOutputChannelLayout)
#endif
{
char str[1024];
av_get_channel_layout_string(str, sizeof(str), mCodecCtx->channels, mCodecCtx->channel_layout);
Log(Debug::Error) << "Unsupported channel layout: " << str;

if (mCodecCtx->channels == 1)
{
mOutputChannelLayout = AV_CH_LAYOUT_MONO;
case AV_CH_LAYOUT_MONO:
*chans = ChannelConfig_Mono;
}
else
{
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
break;
case AV_CH_LAYOUT_STEREO:
*chans = ChannelConfig_Stereo;
}
break;
case AV_CH_LAYOUT_QUAD:
*chans = ChannelConfig_Quad;
break;
case AV_CH_LAYOUT_5POINT1:
*chans = ChannelConfig_5point1;
break;
case AV_CH_LAYOUT_7POINT1:
*chans = ChannelConfig_7point1;
break;
default:
char str[1024];
#if FFMPEG_5_OR_GREATER
av_channel_layout_describe(&mCodecCtx->ch_layout, str, sizeof(str));
Log(Debug::Error) << "Unsupported channel layout: " << str;

if (mCodecCtx->ch_layout.nb_channels == 1)
{
mOutputChannelLayout = AV_CHANNEL_LAYOUT_MONO;
*chans = ChannelConfig_Mono;
}
else
{
mOutputChannelLayout = AV_CHANNEL_LAYOUT_STEREO;
*chans = ChannelConfig_Stereo;
}
#else
av_get_channel_layout_string(str, sizeof(str), mCodecCtx->channels, mCodecCtx->channel_layout);
Log(Debug::Error) << "Unsupported channel layout: " << str;

if (mCodecCtx->channels == 1)
{
mOutputChannelLayout = AV_CH_LAYOUT_MONO;
*chans = ChannelConfig_Mono;
}
else
{
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
*chans = ChannelConfig_Stereo;
}
#endif
break;
}

*samplerate = mCodecCtx->sample_rate;
#if FFMPEG_5_OR_GREATER
AVChannelLayout ch_layout = mCodecCtx->ch_layout;
if (ch_layout.u.mask == 0)
av_channel_layout_default(&ch_layout, mCodecCtx->ch_layout.nb_channels);

if (mOutputSampleFormat != mCodecCtx->sample_fmt || mOutputChannelLayout.u.mask != ch_layout.u.mask)
#else
int64_t ch_layout = mCodecCtx->channel_layout;
if (ch_layout == 0)
ch_layout = av_get_default_channel_layout(mCodecCtx->channels);

if (mOutputSampleFormat != mCodecCtx->sample_fmt || mOutputChannelLayout != ch_layout)
#endif

{
#if FFMPEG_5_OR_GREATER
swr_alloc_set_opts2(&mSwr, // SwrContext
&mOutputChannelLayout, // output ch layout
mOutputSampleFormat, // output sample format
mCodecCtx->sample_rate, // output sample rate
&ch_layout, // input ch layout
mCodecCtx->sample_fmt, // input sample format
mCodecCtx->sample_rate, // input sample rate
0, // logging level offset
nullptr); // log context
#else
mSwr = swr_alloc_set_opts(mSwr, // SwrContext
mOutputChannelLayout, // output ch layout
mOutputSampleFormat, // output sample format
Expand All @@ -376,6 +447,7 @@ namespace MWSound
mCodecCtx->sample_rate, // input sample rate
0, // logging level offset
nullptr); // log context
#endif
if (!mSwr)
throw std::runtime_error("Couldn't allocate SwrContext");
int init = swr_init(mSwr);
Expand Down Expand Up @@ -404,7 +476,11 @@ namespace MWSound

while (getAVAudioData())
{
#if FFMPEG_5_OR_GREATER
size_t got = mFrame->nb_samples * mOutputChannelLayout.nb_channels
#else
size_t got = mFrame->nb_samples * av_get_channel_layout_nb_channels(mOutputChannelLayout)
#endif
* av_get_bytes_per_sample(mOutputSampleFormat);
const char* inbuf = reinterpret_cast<char*>(mFrameData[0]);
output.insert(output.end(), inbuf, inbuf + got);
Expand All @@ -413,7 +489,11 @@ namespace MWSound

size_t FFmpeg_Decoder::getSampleOffset()
{
#if FFMPEG_5_OR_GREATER
std::size_t delay = (mFrameSize - mFramePos) / mOutputChannelLayout.nb_channels
#else
std::size_t delay = (mFrameSize - mFramePos) / av_get_channel_layout_nb_channels(mOutputChannelLayout)
#endif
/ av_get_bytes_per_sample(mOutputSampleFormat);
return static_cast<std::size_t>(mNextPts * mCodecCtx->sample_rate) - delay;
}
Expand All @@ -426,7 +506,11 @@ namespace MWSound
, mNextPts(0.0)
, mSwr(nullptr)
, mOutputSampleFormat(AV_SAMPLE_FMT_NONE)
#if FFMPEG_5_OR_GREATER
, mOutputChannelLayout({})
#else
, mOutputChannelLayout(0)
#endif
, mDataBuf(nullptr)
, mFrameData(nullptr)
, mDataBufLen(0)
Expand Down
11 changes: 11 additions & 0 deletions apps/openmw/mwsound/ffmpeg_decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extern "C"

#include "sound_decoder.hpp"

#define FFMPEG_5_OR_GREATER (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
#define FFMPEG_CONST_WRITEPACKET (LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(60, 12, 100))

namespace MWSound
{
struct AVIOContextDeleter
Expand Down Expand Up @@ -77,7 +80,11 @@ namespace MWSound

SwrContext* mSwr;
enum AVSampleFormat mOutputSampleFormat;
#if FFMPEG_5_OR_GREATER
AVChannelLayout mOutputChannelLayout;
#else
int64_t mOutputChannelLayout;
#endif
uint8_t* mDataBuf;
uint8_t** mFrameData;
int mDataBufLen;
Expand All @@ -87,7 +94,11 @@ namespace MWSound
Files::IStreamPtr mDataStream;

static int readPacket(void* user_data, uint8_t* buf, int buf_size);
#if FFMPEG_CONST_WRITEPACKET
static int writePacket(void* user_data, const uint8_t* buf, int buf_size);
#else
static int writePacket(void* user_data, uint8_t* buf, int buf_size);
#endif
static int64_t seek(void* user_data, int64_t offset, int whence);

bool getAVAudioData();
Expand Down
9 changes: 8 additions & 1 deletion apps/openmw/mwsound/movieaudiofactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ namespace MWSound

size_t getSampleOffset()
{
#if FFMPEG_5_OR_GREATER
ssize_t clock_delay = (mFrameSize - mFramePos) / mOutputChannelLayout.nb_channels
#else
ssize_t clock_delay = (mFrameSize - mFramePos) / av_get_channel_layout_nb_channels(mOutputChannelLayout)
#endif
/ av_get_bytes_per_sample(mOutputSampleFormat);
return (size_t)(mAudioClock * mAudioContext->sample_rate) - clock_delay;
}

std::string getStreamName() { return std::string(); }
std::string getStreamName()
{
return std::string();
}

private:
// MovieAudioDecoder overrides
Expand Down
Loading

0 comments on commit 61cb5b4

Please sign in to comment.