Skip to content

Commit

Permalink
Cleanups API, adds AVChannelLayoutWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Apr 23, 2024
1 parent 22a36a1 commit 686f528
Show file tree
Hide file tree
Showing 22 changed files with 311 additions and 114 deletions.
3 changes: 1 addition & 2 deletions modules/import-export/mod-ffmpeg/ExportFFmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,8 +1151,7 @@ bool FFmpegExporter::InitCodecs(int sampleRate,

mEncAudioCodecCtx->SetGlobalQuality(mEncAudioCodecCtx->GetGlobalQuality() * AUDACITY_FF_QP2LAMBDA);
mEncAudioCodecCtx->SetSampleRate(mSampleRate);
mEncAudioCodecCtx->SetChannels(mChannels);
mEncAudioCodecCtx->SetChannelLayout(mFFmpeg->av_get_default_channel_layout(mChannels));
mEncAudioCodecCtx->SetChannelLayout(mFFmpeg->CreateDefaultChannelLayout(mChannels).get());
mEncAudioCodecCtx->SetTimeBase({ 1, mSampleRate });
mEncAudioCodecCtx->SetSampleFmt(static_cast<AVSampleFormatFwd>(AUDACITY_AV_SAMPLE_FMT_S16));
mEncAudioCodecCtx->SetStrictStdCompliance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ struct FFMPEG_SUPPORT_API AVUtilFunctions
int (*av_samples_get_buffer_size) (int *linesize, int nb_channels, int nb_samples, AVSampleFormatFwd sample_fmt, int align) = nullptr;
int64_t (*av_get_default_channel_layout) (int nb_channels) = nullptr;
int (*av_strerror) (int errnum, char *errbuf, size_t errbuf_size) = nullptr;
int (*av_get_channel_layout_nb_channels)(uint64_t channel_layout) = nullptr;
void (*av_channel_layout_default)(AVChannelLayout *layout, int nb_channels) = nullptr;
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ if (${_OPT}use_ffmpeg)
impl/avformat/AVInputFormatWrapperImpl.inl
impl/avformat/AVOutputFormatWrapperImpl.inl
impl/avformat/AVStreamWrapperImpl.inl
impl/avutil/AVChannelLayoutWrapperImpl.inl
impl/avutil/AVFrameWrapperImpl.inl
impl/avutil/FFmpegLogImpl.inl
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,27 @@ std::unique_ptr<AVOutputFormatWrapper> FFmpegFunctions::GuessOutputFormat(
return mPrivate->FormatFactories.CreateAVOutputFormatWrapper(outputFormat);
}

std::unique_ptr<AVChannelLayoutWrapper>
FFmpegFunctions::CreateDefaultChannelLayout(int channelsCount) const
{
return mPrivate->UtilFactories.CreateDefaultChannelLayout(
*this, channelsCount);
}

std::unique_ptr<AVChannelLayoutWrapper>
FFmpegFunctions::CreateLegacyChannelLayout(
uint64_t layout, int channelsCount) const
{
return mPrivate->UtilFactories.CreateLegacyChannelLayout(
*this, layout, channelsCount);
}

std::unique_ptr<AVChannelLayoutWrapper>
FFmpegFunctions::CreateAVChannelLayout(const AVChannelLayout* layout) const
{
return mPrivate->UtilFactories.CreateAVChannelLayout(*this, layout);
}

std::unique_ptr<AVOutputFormatWrapper>
FFmpegFunctions::CreateAVOutputFormatWrapper(
const AVOutputFormat* outputFormat) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "wrappers/AVOutputFormatWrapper.h"
#include "wrappers/AVCodecWrapper.h"
#include "wrappers/AVCodecContextWrapper.h"
#include "wrappers/AVFifoBufferWrapper.h"
#include "wrappers/AVChannelLayoutWrapper.h"

class StringSetting;

Expand Down Expand Up @@ -115,7 +115,11 @@ struct FFMPEG_SUPPORT_API FFmpegFunctions :
std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapperFromCodec(std::unique_ptr<AVCodecWrapper> codec) const;

std::unique_ptr<AVOutputFormatWrapper> GuessOutputFormat(const char* short_name, const char* filename, const char* mime_type);


std::unique_ptr<AVChannelLayoutWrapper> CreateDefaultChannelLayout(int channelsCount) const;
std::unique_ptr<AVChannelLayoutWrapper> CreateLegacyChannelLayout(uint64_t layout, int channelsCount) const;
std::unique_ptr<AVChannelLayoutWrapper> CreateAVChannelLayout(const AVChannelLayout* layout) const;

const std::vector<const AVOutputFormatWrapper*>& GetOutputFormats() const;
const std::vector<const AVCodecWrapper*>& GetCodecs() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ struct FFMPegVersion final

typedef struct AVBuffer AVBuffer;

typedef struct AVChannelLayout AVChannelLayout;

struct AudacityAVBufferRef
{
AVBuffer* buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FFmpegLog;
class AVCodecContextWrapper;
class AVCodecWrapper;
class AVPacketWrapper;
class AVChannelLayoutWrapper;

struct AVCodecIDResolver final
{
Expand Down Expand Up @@ -61,6 +62,10 @@ struct AVUtilFactories final
//! @post return value is not null
std::unique_ptr<AVFrameWrapper> (*CreateAVFrameWrapper)(const FFmpegFunctions&) = nullptr;
std::unique_ptr<FFmpegLog> (*CreateLogCallbackSetter)(const FFmpegFunctions&) = nullptr;

std::unique_ptr<AVChannelLayoutWrapper> (*CreateDefaultChannelLayout)(const FFmpegFunctions&, int channelsCount) = nullptr;
std::unique_ptr<AVChannelLayoutWrapper> (*CreateLegacyChannelLayout)(const FFmpegFunctions&, uint64_t layout, int channelsCount) = nullptr;
std::unique_ptr<AVChannelLayoutWrapper> (*CreateAVChannelLayout)(const FFmpegFunctions&, const AVChannelLayout* layout) = nullptr;
};

class FFmpegAPIResolver final
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,34 @@ public:
mAVCodecContext->bit_rate = value;
}

uint64_t GetChannelLayout() const noexcept override
const AVChannelLayoutWrapper* GetChannelLayout() const noexcept override
{
if (mAVCodecContext != nullptr)
return mAVCodecContext->channel_layout;

return {};
return GetChannelLayoutSafe();
}

void SetChannelLayout(uint64_t value) noexcept override
void SetChannelLayout(const AVChannelLayoutWrapper* layout) noexcept override
{
if (mAVCodecContext != nullptr)
mAVCodecContext->channel_layout = value;
if (mAVCodecContext == nullptr || layout == nullptr)
return;

mChannelLayoutWrapper = layout->Clone();
// Clone never returns nullptr
#if HAS_AV_CHANNEL_LAYOUT
mAVCodecContext->ch_layout = *layout->GetChannelLayout();
#else
mAVCodecContext->channel_layout = layout->GetLegacyChannelLayout();
mAVCodecContext->channels = layout->GetChannelsCount();
#endif
}

int GetChannels() const noexcept override
{
if (mAVCodecContext != nullptr)
return mAVCodecContext->channels;
if (auto layout = GetChannelLayoutSafe(); layout != nullptr)
return layout->GetChannelsCount();

return {};
}

void SetChannels(int value) noexcept override
{
if (mAVCodecContext != nullptr)
mAVCodecContext->channels = value;
}

const AVCodecWrapper* GetCodec() const noexcept override
{
if (!mAVCodec && mAVCodecContext && mAVCodecContext->codec)
Expand Down Expand Up @@ -276,18 +276,17 @@ public:

int GetFrameNumber() const noexcept override
{
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 0, 0)
if (mAVCodecContext != nullptr)
return mAVCodecContext->frame_number;
#else
if (mAVCodecContext != nullptr)
return mAVCodecContext->frame_num;
#endif

return {};
}

void SetFrameNumber(int value) noexcept override
{
if (mAVCodecContext != nullptr)
mAVCodecContext->frame_number = value;
}

int GetFrameSize() const noexcept override
{
if (mAVCodecContext != nullptr)
Expand Down Expand Up @@ -495,6 +494,26 @@ public:

return result;
}
const AVChannelLayoutWrapper* GetChannelLayoutSafe() const noexcept
{
if (mAVCodecContext == nullptr)
return nullptr;

if (mChannelLayoutWrapper == nullptr)
{
#if HAS_AV_CHANNEL_LAYOUT
mChannelLayoutWrapper =
mFFmpeg.CreateAVChannelLayout(&mAVCodecContext->ch_layout);
#else
mChannelLayoutWrapper = mFFmpeg.CreateLegacyChannelLayout(
mAVCodecContext->channel_layout, mAVCodecContext->channels);
#endif
}

return mChannelLayoutWrapper.get();
}

mutable std::unique_ptr<AVChannelLayoutWrapper> mChannelLayoutWrapper;
};

std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapperFromCodec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ public:
return {};
}

const uint64_t* GetChannelLayouts() const noexcept override
{
if (mAVCodec != nullptr)
return mAVCodec->channel_layouts;

return {};
}

uint8_t GetMaxLowres() const noexcept override
{
if (mAVCodec != nullptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ extern "C"

#include "FFmpegFunctions.h"

#include "wrappers/AVChannelLayoutWrapper.h"
#include "wrappers/AVFrameWrapper.h"

#include "../../FFmpegAPIResolver.h"
#include "../../FFmpegLog.h"

namespace avutil_52
{
#include "../AVChannelLayoutWrapperImpl.inl"
#include "../AVFrameWrapperImpl.inl"
#include "../FFmpegLogImpl.inl"

const bool registered = ([]() {
FFmpegAPIResolver::Get().AddAVUtilFactories(52, {
&CreateAVFrameWrapper,
&CreateLogCallbackSetter
&CreateLogCallbackSetter,
&CreateDefaultChannelLayout,
&CreateLegacyChannelLayout,
&CreateAVChannelLayout
});

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ extern "C"

#include "FFmpegFunctions.h"

#include "wrappers/AVChannelLayoutWrapper.h"
#include "wrappers/AVFrameWrapper.h"

#include "../../FFmpegAPIResolver.h"
#include "../../FFmpegLog.h"

namespace avutil_55
{
#include "../AVChannelLayoutWrapperImpl.inl"
#include "../AVFrameWrapperImpl.inl"
#include "../FFmpegLogImpl.inl"

const bool registered = ([]() {
FFmpegAPIResolver::Get().AddAVUtilFactories(55, {
&CreateAVFrameWrapper,
&CreateLogCallbackSetter
&CreateLogCallbackSetter,
&CreateDefaultChannelLayout,
&CreateLegacyChannelLayout,
&CreateAVChannelLayout
});

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ extern "C"

#include "FFmpegFunctions.h"

#include "wrappers/AVChannelLayoutWrapper.h"
#include "wrappers/AVFrameWrapper.h"

#include "../../FFmpegAPIResolver.h"
#include "../../FFmpegLog.h"

namespace avutil_56
{
#include "../AVChannelLayoutWrapperImpl.inl"
#include "../AVFrameWrapperImpl.inl"
#include "../FFmpegLogImpl.inl"

const bool registered = ([]() {
FFmpegAPIResolver::Get().AddAVUtilFactories(56, {
&CreateAVFrameWrapper,
&CreateLogCallbackSetter
&CreateLogCallbackSetter,
&CreateDefaultChannelLayout,
&CreateLegacyChannelLayout,
&CreateAVChannelLayout
});

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ extern "C"

#include "FFmpegFunctions.h"

#include "wrappers/AVChannelLayoutWrapper.h"
#include "wrappers/AVFrameWrapper.h"

#include "../../FFmpegAPIResolver.h"
#include "../../FFmpegLog.h"

namespace avutil_57
{
#include "../AVChannelLayoutWrapperImpl.inl"
#include "../AVFrameWrapperImpl.inl"
#include "../FFmpegLogImpl.inl"

const bool registered = ([]() {
FFmpegAPIResolver::Get().AddAVUtilFactories(57, {
&CreateAVFrameWrapper,
&CreateLogCallbackSetter
&CreateLogCallbackSetter,
&CreateDefaultChannelLayout,
&CreateLegacyChannelLayout,
&CreateAVChannelLayout
});

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ extern "C"

#include "FFmpegFunctions.h"

#include "wrappers/AVChannelLayoutWrapper.h"
#include "wrappers/AVFrameWrapper.h"

#include "../../FFmpegAPIResolver.h"
#include "../../FFmpegLog.h"

namespace avutil_58
{
#include "../AVChannelLayoutWrapperImpl.inl"
#include "../AVFrameWrapperImpl.inl"
#include "../FFmpegLogImpl.inl"

const bool registered = ([]() {
FFmpegAPIResolver::Get().AddAVUtilFactories(58, {
&CreateAVFrameWrapper,
&CreateLogCallbackSetter
&CreateLogCallbackSetter,
&CreateDefaultChannelLayout,
&CreateLegacyChannelLayout,
&CreateAVChannelLayout
});

return true;
Expand Down
Loading

0 comments on commit 686f528

Please sign in to comment.