Truly independent web browser
LibMedia+Tests: Wrap AVChannelLayout in an RAII class
Zaggy1024c1081a414629
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp b/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cppindex d5b2595499c..5c050a324de 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp@@ -61,15 +61,15 @@ ErrorOr<void> FFmpegAudioConverter::set_sample_specifications(Audio::SampleSpeciif (output.sample_rate() > NumericLimits<int>::max())return Error::from_string_literal("Output sample rate is too high");- auto input_channel_layout = TRY(channel_map_to_av_channel_layout(FFmpegFunctions::bundled(), input.channel_map()));+ auto input_channel_layout = TRY(FFmpegChannelLayout::from_channel_map(FFmpegFunctions::bundled(), input.channel_map()));auto input_sample_rate = static_cast<int>(input.sample_rate());- auto output_channel_layout = TRY(channel_map_to_av_channel_layout(FFmpegFunctions::bundled(), output.channel_map()));+ auto output_channel_layout = TRY(FFmpegChannelLayout::from_channel_map(FFmpegFunctions::bundled(), output.channel_map()));auto output_sample_rate = static_cast<int>(output.sample_rate());auto allocation_result = swr_alloc_set_opts2(&m_context,- &output_channel_layout, AVSampleFormat::AV_SAMPLE_FMT_FLTP, output_sample_rate,- &input_channel_layout, AVSampleFormat::AV_SAMPLE_FMT_FLTP, input_sample_rate,+ &output_channel_layout.layout(), AVSampleFormat::AV_SAMPLE_FMT_FLTP, output_sample_rate,+ &input_channel_layout.layout(), AVSampleFormat::AV_SAMPLE_FMT_FLTP, input_sample_rate,0, nullptr);if (allocation_result < 0)return Error::from_string_view(av_error_code_to_string(allocation_result));diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp b/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cppindex 953ce787531..e6a1a2a8678 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp@@ -82,12 +82,11 @@ DecoderErrorOr<NonnullOwnPtr<FFmpegAudioDecoder>> FFmpegAudioDecoder::try_createVERIFY(functions.av_opt_set_int(codec_context, "ar", sample_specification.sample_rate(), 0) == 0);if (sample_specification.channel_map().is_valid()) {- auto channel_layout_result = channel_map_to_av_channel_layout(functions, sample_specification.channel_map());+ auto channel_layout_result = FFmpegChannelLayout::from_channel_map(functions, sample_specification.channel_map());if (channel_layout_result.is_error())return DecoderError::format(DecoderErrorCategory::Invalid, channel_layout_result.error().string_literal());auto channel_layout = channel_layout_result.release_value();- auto set_result = functions.av_opt_set_chlayout(codec_context, "ch_layout", &channel_layout, 0);- functions.av_channel_layout_uninit(&channel_layout);+ auto set_result = functions.av_opt_set_chlayout(codec_context, "ch_layout", &channel_layout.layout(), 0);VERIFY(set_result == 0);}@@ -230,10 +229,8 @@ DecoderErrorOr<void> FFmpegAudioDecoder::receive_next_frame()if (sample_rate <= 0 || sample_rate > NumericLimits<u32>::max())return DecoderError::corrupted("FFmpeg decoder created a frame with an invalid sample rate"sv);- AVChannelLayout channel_layout;- VERIFY(m_functions.av_opt_get_chlayout(m_codec_context, "ch_layout", 0, &channel_layout) == 0);- auto channel_map_result = av_channel_layout_to_channel_map(m_functions, channel_layout);- m_functions.av_channel_layout_uninit(&channel_layout);+ auto channel_layout = FFmpegChannelLayout::from_opt(m_functions, m_codec_context, "ch_layout");+ auto channel_map_result = channel_layout.to_channel_map();if (channel_map_result.is_error())return DecoderError::with_description(DecoderErrorCategory::NotImplemented, channel_map_result.error().string_literal());diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cpp b/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cppindex cfdf1e41953..1915a5f7002 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cpp+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cpp@@ -45,10 +45,10 @@ DecoderErrorOr<NonnullOwnPtr<FFmpegAudioEncoder>> FFmpegAudioEncoder::try_createcodec_context->sample_fmt = AV_SAMPLE_FMT_FLT;if (sample_specification.channel_map().is_valid()) {- auto channel_layout_result = channel_map_to_av_channel_layout(functions, sample_specification.channel_map());+ auto channel_layout_result = FFmpegChannelLayout::from_channel_map(functions, sample_specification.channel_map());if (channel_layout_result.is_error())return DecoderError::format(DecoderErrorCategory::Invalid, channel_layout_result.error().string_literal());- codec_context->ch_layout = channel_layout_result.release_value();+ codec_context->ch_layout = channel_layout_result.release_value().leak_layout();}if (functions.avcodec_open2(codec_context, codec, nullptr) < 0)diff --git a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cppindex 32f94275614..6ec5384d584 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp+++ b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp@@ -28,28 +28,28 @@ namespace Media::FFmpeg {C(Audio::Channel::TopBackCenter, AVChannel::AV_CHAN_TOP_BACK_CENTER) \C(Audio::Channel::TopBackRight, AVChannel::AV_CHAN_TOP_BACK_RIGHT)-ErrorOr<Audio::ChannelMap> av_channel_layout_to_channel_map(FFmpegFunctions const& functions, AVChannelLayout const& layout)+ErrorOr<Audio::ChannelMap> FFmpegChannelLayout::to_channel_map() const{- if (layout.nb_channels <= 0)+ if (m_channel_layout.nb_channels <= 0)return Error::from_string_literal("FFmpeg channel layout had no channels");- if (static_cast<size_t>(layout.nb_channels) > Audio::ChannelMap::capacity())+ if (static_cast<size_t>(m_channel_layout.nb_channels) > Audio::ChannelMap::capacity())return Error::from_string_literal("FFmpeg channel layout had too many channels");Vector<Audio::Channel, Audio::ChannelMap::capacity()> channels;- channels.resize(layout.nb_channels);+ channels.resize(m_channel_layout.nb_channels);- if (layout.order == AVChannelOrder::AV_CHANNEL_ORDER_UNSPEC) {- if (layout.nb_channels == 1)+ if (m_channel_layout.order == AVChannelOrder::AV_CHANNEL_ORDER_UNSPEC) {+ if (m_channel_layout.nb_channels == 1)return Audio::ChannelMap::mono();- if (layout.nb_channels == 2)+ if (m_channel_layout.nb_channels == 2)return Audio::ChannelMap::stereo();- if (layout.nb_channels == 4)+ if (m_channel_layout.nb_channels == 4)return Audio::ChannelMap::quadrophonic();- if (layout.nb_channels == 6)+ if (m_channel_layout.nb_channels == 6)return Audio::ChannelMap::surround_5_1();- if (layout.nb_channels == 8)+ if (m_channel_layout.nb_channels == 8)return Audio::ChannelMap::surround_7_1();- for (int i = 0; i < layout.nb_channels; ++i)+ for (int i = 0; i < m_channel_layout.nb_channels; ++i)channels[i] = Audio::Channel::Unknown;return Audio::ChannelMap(channels);}@@ -58,9 +58,9 @@ ErrorOr<Audio::ChannelMap> av_channel_layout_to_channel_map(FFmpegFunctions conscase av_channel: \return audio_channel;- for (int i = 0; i < layout.nb_channels; i++) {+ for (int i = 0; i < m_channel_layout.nb_channels; i++) {auto channel = [&] {- switch (functions.av_channel_layout_channel_from_index(&layout, i)) {+ switch (m_functions->av_channel_layout_channel_from_index(&m_channel_layout, i)) {ENUMERATE_CHANNEL_POSITIONS(AV_CHANNEL_TO_AUDIO_CHANNEL)default:return Audio::Channel::Unknown;@@ -107,10 +107,10 @@ static ErrorOr<AVChannelLayout> channel_map_to_custom_av_channel_layout(FFmpegFureturn layout;}-ErrorOr<AVChannelLayout> channel_map_to_av_channel_layout(FFmpegFunctions const& functions, Audio::ChannelMap const& channel_map)+ErrorOr<FFmpegChannelLayout> FFmpegChannelLayout::from_channel_map(FFmpegFunctions const& functions, Audio::ChannelMap const& channel_map){if (channel_map.channel_count() > NumericLimits<decltype(AVChannelLayout::u.mask)>::digits())- return channel_map_to_custom_av_channel_layout(functions, channel_map);+ return FFmpegChannelLayout(functions, TRY(channel_map_to_custom_av_channel_layout(functions, channel_map)));AVChannelLayout layout;layout.nb_channels = channel_map.channel_count();@@ -126,12 +126,19 @@ ErrorOr<AVChannelLayout> channel_map_to_av_channel_layout(FFmpegFunctions const&// If we find that one of the channels in our input mapping doesn't match this requirement,// fall back to a custom order.if (channel <= last_channel)- return channel_map_to_custom_av_channel_layout(functions, channel_map);+ return FFmpegChannelLayout(functions, TRY(channel_map_to_custom_av_channel_layout(functions, channel_map)));layout.u.mask |= 1ULL << channel;last_channel = channel;}- return layout;+ return FFmpegChannelLayout(functions, layout);+}++FFmpegChannelLayout FFmpegChannelLayout::from_opt(FFmpegFunctions const& functions, void* obj, char const* name)+{+ AVChannelLayout layout {};+ VERIFY(functions.av_opt_get_chlayout(obj, name, 0, &layout) == 0);+ return FFmpegChannelLayout(functions, layout);}}diff --git a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.h b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.hindex 9b6e3d4c2a1..aadeabcb63b 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.h+++ b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.h@@ -17,7 +17,53 @@ extern "C" {namespace Media::FFmpeg {-MEDIA_API ErrorOr<Audio::ChannelMap> av_channel_layout_to_channel_map(FFmpegFunctions const&, AVChannelLayout const&);-MEDIA_API ErrorOr<AVChannelLayout> channel_map_to_av_channel_layout(FFmpegFunctions const&, Audio::ChannelMap const&);+class MEDIA_API FFmpegChannelLayout {+ AK_MAKE_NONCOPYABLE(FFmpegChannelLayout);++public:+ FFmpegChannelLayout(FFmpegChannelLayout&& other)+ : m_functions(exchange(other.m_functions, nullptr))+ , m_channel_layout(exchange(other.m_channel_layout, {}))+ {+ }++ ~FFmpegChannelLayout()+ {+ if (m_functions)+ m_functions->av_channel_layout_uninit(&m_channel_layout);+ }++ FFmpegChannelLayout& operator=(FFmpegChannelLayout&& other)+ {+ if (this == &other)+ return *this;+ if (m_functions)+ m_functions->av_channel_layout_uninit(&m_channel_layout);+ m_functions = exchange(other.m_functions, nullptr);+ m_channel_layout = exchange(other.m_channel_layout, {});+ return *this;+ }++ ErrorOr<Audio::ChannelMap> to_channel_map() const;+ static ErrorOr<FFmpegChannelLayout> from_channel_map(FFmpegFunctions const&, Audio::ChannelMap const&);+ static FFmpegChannelLayout from_opt(FFmpegFunctions const&, void* obj, char const* name);++ AVChannelLayout const& layout() { return m_channel_layout; }+ AVChannelLayout leak_layout()+ {+ m_functions = nullptr;+ return exchange(m_channel_layout, {});+ }++private:+ FFmpegChannelLayout(FFmpegFunctions const& functions, AVChannelLayout const& layout)+ : m_functions(&functions)+ , m_channel_layout(layout)+ {+ }++ FFmpegFunctions const* m_functions;+ AVChannelLayout m_channel_layout;+};}diff --git a/Tests/LibMedia/TestSystemFFmpeg.cpp b/Tests/LibMedia/TestSystemFFmpeg.cppindex 896c061544c..263ac34b06e 100644--- a/Tests/LibMedia/TestSystemFFmpeg.cpp+++ b/Tests/LibMedia/TestSystemFFmpeg.cpp@@ -40,13 +40,12 @@ static void expect_custom_channel_layout_round_trips(Media::FFmpeg::FFmpegFuncti{// Out of native order, so this needs a custom layout.auto channel_map = Audio::ChannelMap(Audio::Channel::FrontRight, Audio::Channel::FrontLeft, Audio::Channel::LowFrequency);- auto layout = TRY_OR_FAIL(Media::FFmpeg::channel_map_to_av_channel_layout(functions, channel_map));- EXPECT_EQ(layout.order, AV_CHANNEL_ORDER_CUSTOM);- EXPECT_EQ(layout.nb_channels, 3);- for (int i = 0; i < layout.nb_channels; i++)- EXPECT_EQ(layout.u.map[i].name[0], '\0');- EXPECT_EQ(TRY_OR_FAIL(Media::FFmpeg::av_channel_layout_to_channel_map(functions, layout)), channel_map);- functions.av_channel_layout_uninit(&layout);+ auto layout = TRY_OR_FAIL(Media::FFmpeg::FFmpegChannelLayout::from_channel_map(functions, channel_map));+ EXPECT_EQ(layout.layout().order, AV_CHANNEL_ORDER_CUSTOM);+ EXPECT_EQ(layout.layout().nb_channels, 3);+ for (int i = 0; i < layout.layout().nb_channels; i++)+ EXPECT_EQ(layout.layout().u.map[i].name[0], '\0');+ EXPECT_EQ(TRY_OR_FAIL(layout.to_channel_map()), channel_map);}TEST_CASE(custom_channel_layouts_round_trip_through_the_loaded_library)