|
|
@ -8,6 +8,10 @@
|
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if FFMPEG_5_OR_GREATER
|
|
|
|
|
|
|
|
#include <libavutil/channel_layout.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MWSound
|
|
|
|
namespace MWSound
|
|
|
|
{
|
|
|
|
{
|
|
|
|
void AVIOContextDeleter::operator()(AVIOContext* ptr) const
|
|
|
|
void AVIOContextDeleter::operator()(AVIOContext* ptr) const
|
|
|
@ -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)
|
|
|
|
int FFmpeg_Decoder::writePacket(void*, uint8_t*, int)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Log(Debug::Error) << "can't write to read-only stream";
|
|
|
|
Log(Debug::Error) << "can't write to read-only stream";
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
@ -152,7 +160,11 @@ namespace MWSound
|
|
|
|
if (!mDataBuf || mDataBufLen < mFrame->nb_samples)
|
|
|
|
if (!mDataBuf || mDataBufLen < mFrame->nb_samples)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
av_freep(&mDataBuf);
|
|
|
|
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),
|
|
|
|
if (av_samples_alloc(&mDataBuf, nullptr, av_get_channel_layout_nb_channels(mOutputChannelLayout),
|
|
|
|
|
|
|
|
#endif
|
|
|
|
mFrame->nb_samples, mOutputSampleFormat, 0)
|
|
|
|
mFrame->nb_samples, mOutputSampleFormat, 0)
|
|
|
|
< 0)
|
|
|
|
< 0)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
@ -189,7 +201,11 @@ namespace MWSound
|
|
|
|
if (!getAVAudioData())
|
|
|
|
if (!getAVAudioData())
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
mFramePos = 0;
|
|
|
|
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)
|
|
|
|
mFrameSize = mFrame->nb_samples * av_get_channel_layout_nb_channels(mOutputChannelLayout)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
* av_get_bytes_per_sample(mOutputSampleFormat);
|
|
|
|
* av_get_bytes_per_sample(mOutputSampleFormat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -277,11 +293,19 @@ namespace MWSound
|
|
|
|
else
|
|
|
|
else
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16;
|
|
|
|
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;
|
|
|
|
mOutputChannelLayout = (*stream)->codecpar->channel_layout;
|
|
|
|
if (mOutputChannelLayout == 0)
|
|
|
|
if (mOutputChannelLayout == 0)
|
|
|
|
mOutputChannelLayout = av_get_default_channel_layout(codecCtxPtr->channels);
|
|
|
|
mOutputChannelLayout = av_get_default_channel_layout(codecCtxPtr->channels);
|
|
|
|
|
|
|
|
|
|
|
|
codecCtxPtr->channel_layout = mOutputChannelLayout;
|
|
|
|
codecCtxPtr->channel_layout = mOutputChannelLayout;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
mIoCtx = std::move(ioCtx);
|
|
|
|
mIoCtx = std::move(ioCtx);
|
|
|
|
mFrame = std::move(frame);
|
|
|
|
mFrame = std::move(frame);
|
|
|
@ -332,19 +356,44 @@ namespace MWSound
|
|
|
|
*type = SampleType_Int16;
|
|
|
|
*type = SampleType_Int16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mOutputChannelLayout == AV_CH_LAYOUT_MONO)
|
|
|
|
#if FFMPEG_5_OR_GREATER
|
|
|
|
|
|
|
|
switch (mOutputChannelLayout.u.mask)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
switch (mOutputChannelLayout)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case AV_CH_LAYOUT_MONO:
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
else if (mOutputChannelLayout == AV_CH_LAYOUT_STEREO)
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_CH_LAYOUT_STEREO:
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
else if (mOutputChannelLayout == AV_CH_LAYOUT_QUAD)
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_CH_LAYOUT_QUAD:
|
|
|
|
*chans = ChannelConfig_Quad;
|
|
|
|
*chans = ChannelConfig_Quad;
|
|
|
|
else if (mOutputChannelLayout == AV_CH_LAYOUT_5POINT1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_CH_LAYOUT_5POINT1:
|
|
|
|
*chans = ChannelConfig_5point1;
|
|
|
|
*chans = ChannelConfig_5point1;
|
|
|
|
else if (mOutputChannelLayout == AV_CH_LAYOUT_7POINT1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_CH_LAYOUT_7POINT1:
|
|
|
|
*chans = ChannelConfig_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
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char str[1024];
|
|
|
|
mOutputChannelLayout = AV_CHANNEL_LAYOUT_STEREO;
|
|
|
|
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
av_get_channel_layout_string(str, sizeof(str), mCodecCtx->channels, mCodecCtx->channel_layout);
|
|
|
|
av_get_channel_layout_string(str, sizeof(str), mCodecCtx->channels, mCodecCtx->channel_layout);
|
|
|
|
Log(Debug::Error) << "Unsupported channel layout: " << str;
|
|
|
|
Log(Debug::Error) << "Unsupported channel layout: " << str;
|
|
|
|
|
|
|
|
|
|
|
@ -358,15 +407,37 @@ namespace MWSound
|
|
|
|
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*samplerate = mCodecCtx->sample_rate;
|
|
|
|
*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;
|
|
|
|
int64_t ch_layout = mCodecCtx->channel_layout;
|
|
|
|
if (ch_layout == 0)
|
|
|
|
if (ch_layout == 0)
|
|
|
|
ch_layout = av_get_default_channel_layout(mCodecCtx->channels);
|
|
|
|
ch_layout = av_get_default_channel_layout(mCodecCtx->channels);
|
|
|
|
|
|
|
|
|
|
|
|
if (mOutputSampleFormat != mCodecCtx->sample_fmt || mOutputChannelLayout != ch_layout)
|
|
|
|
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
|
|
|
|
mSwr = swr_alloc_set_opts(mSwr, // SwrContext
|
|
|
|
mOutputChannelLayout, // output ch layout
|
|
|
|
mOutputChannelLayout, // output ch layout
|
|
|
|
mOutputSampleFormat, // output sample format
|
|
|
|
mOutputSampleFormat, // output sample format
|
|
|
@ -376,6 +447,7 @@ namespace MWSound
|
|
|
|
mCodecCtx->sample_rate, // input sample rate
|
|
|
|
mCodecCtx->sample_rate, // input sample rate
|
|
|
|
0, // logging level offset
|
|
|
|
0, // logging level offset
|
|
|
|
nullptr); // log context
|
|
|
|
nullptr); // log context
|
|
|
|
|
|
|
|
#endif
|
|
|
|
if (!mSwr)
|
|
|
|
if (!mSwr)
|
|
|
|
throw std::runtime_error("Couldn't allocate SwrContext");
|
|
|
|
throw std::runtime_error("Couldn't allocate SwrContext");
|
|
|
|
int init = swr_init(mSwr);
|
|
|
|
int init = swr_init(mSwr);
|
|
|
@ -404,7 +476,11 @@ namespace MWSound
|
|
|
|
|
|
|
|
|
|
|
|
while (getAVAudioData())
|
|
|
|
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)
|
|
|
|
size_t got = mFrame->nb_samples * av_get_channel_layout_nb_channels(mOutputChannelLayout)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
* av_get_bytes_per_sample(mOutputSampleFormat);
|
|
|
|
* av_get_bytes_per_sample(mOutputSampleFormat);
|
|
|
|
const char* inbuf = reinterpret_cast<char*>(mFrameData[0]);
|
|
|
|
const char* inbuf = reinterpret_cast<char*>(mFrameData[0]);
|
|
|
|
output.insert(output.end(), inbuf, inbuf + got);
|
|
|
|
output.insert(output.end(), inbuf, inbuf + got);
|
|
|
@ -413,7 +489,11 @@ namespace MWSound
|
|
|
|
|
|
|
|
|
|
|
|
size_t FFmpeg_Decoder::getSampleOffset()
|
|
|
|
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)
|
|
|
|
std::size_t delay = (mFrameSize - mFramePos) / av_get_channel_layout_nb_channels(mOutputChannelLayout)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
/ av_get_bytes_per_sample(mOutputSampleFormat);
|
|
|
|
/ av_get_bytes_per_sample(mOutputSampleFormat);
|
|
|
|
return static_cast<std::size_t>(mNextPts * mCodecCtx->sample_rate) - delay;
|
|
|
|
return static_cast<std::size_t>(mNextPts * mCodecCtx->sample_rate) - delay;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -426,7 +506,11 @@ namespace MWSound
|
|
|
|
, mNextPts(0.0)
|
|
|
|
, mNextPts(0.0)
|
|
|
|
, mSwr(nullptr)
|
|
|
|
, mSwr(nullptr)
|
|
|
|
, mOutputSampleFormat(AV_SAMPLE_FMT_NONE)
|
|
|
|
, mOutputSampleFormat(AV_SAMPLE_FMT_NONE)
|
|
|
|
|
|
|
|
#if FFMPEG_5_OR_GREATER
|
|
|
|
|
|
|
|
, mOutputChannelLayout({})
|
|
|
|
|
|
|
|
#else
|
|
|
|
, mOutputChannelLayout(0)
|
|
|
|
, mOutputChannelLayout(0)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
, mDataBuf(nullptr)
|
|
|
|
, mDataBuf(nullptr)
|
|
|
|
, mFrameData(nullptr)
|
|
|
|
, mFrameData(nullptr)
|
|
|
|
, mDataBufLen(0)
|
|
|
|
, mDataBufLen(0)
|
|
|
|