Truly independent web browser
LibMedia: Don't rely on FFmpeg's av_channel_layout_custom_init
Zaggy1024b595e0f7c427
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cppindex 9cc77a965a7..32f94275614 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp+++ b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.cpp@@ -6,10 +6,6 @@#include <LibMedia/FFmpeg/FFmpegChannelLayout.h>-extern "C" {-#include <libavutil/error.h>-}-namespace Media::FFmpeg {#define ENUMERATE_CHANNEL_POSITIONS(C) \@@ -91,13 +87,19 @@ static AVChannel audio_channel_to_av_channel(Audio::Channel channel)static ErrorOr<AVChannelLayout> channel_map_to_custom_av_channel_layout(FFmpegFunctions const& functions, Audio::ChannelMap const& channel_map){- AVChannelLayout layout;- auto init_result = functions.av_channel_layout_custom_init(&layout, channel_map.channel_count());- if (init_result == AVERROR(EINVAL))+ // This does what av_channel_layout_custom_init() does, as that is missing from FFmpeg before 7.0. The map must come+ // from the same library's allocator, since av_channel_layout_uninit() frees it.+ if (channel_map.channel_count() == 0)return Error::from_string_literal("Attempted to create an FFmpeg channel layout with an invalid channel count");- if (init_result == AVERROR(ENOMEM))+ auto* map = static_cast<AVChannelCustom*>(functions.av_malloc(channel_map.channel_count() * sizeof(AVChannelCustom)));+ if (map == nullptr)return Error::from_string_literal("Failed to allocate an FFmpeg channel layout");- VERIFY(layout.nb_channels == channel_map.channel_count());+ __builtin_memset(map, 0, channel_map.channel_count() * sizeof(AVChannelCustom));++ AVChannelLayout layout {};+ layout.order = AVChannelOrder::AV_CHANNEL_ORDER_CUSTOM;+ layout.nb_channels = channel_map.channel_count();+ layout.u.map = map;for (u8 i = 0; i < channel_map.channel_count(); i++)layout.u.map[i].id = audio_channel_to_av_channel(channel_map.channel_at(i));diff --git a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.h b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.hindex 5dac7ba4462..9b6e3d4c2a1 100644--- a/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.h+++ b/Libraries/LibMedia/FFmpeg/FFmpegChannelLayout.h@@ -8,6 +8,7 @@#include <AK/Error.h>#include <LibAudio/ChannelMap.h>+#include <LibMedia/Export.h>#include <LibMediaCodecs/FFmpeg/FFmpegFunctions.h>extern "C" {@@ -16,7 +17,7 @@ extern "C" {namespace Media::FFmpeg {-ErrorOr<Audio::ChannelMap> av_channel_layout_to_channel_map(FFmpegFunctions const&, AVChannelLayout const&);-ErrorOr<AVChannelLayout> channel_map_to_av_channel_layout(FFmpegFunctions const&, Audio::ChannelMap const&);+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&);}diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.hindex 192f14063c8..d07e8c1ea88 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h@@ -33,6 +33,7 @@ static_assert(sizeof(void*) != 8 || (offsetof(AVFrame, data) == 0 && offsetof(AVstatic_assert(sizeof(void*) != 8 || (offsetof(AVCodecParameters, codec_type) == 0 && offsetof(AVCodecParameters, codec_id) == 4 && offsetof(AVCodecParameters, extradata) == 16 && offsetof(AVCodecParameters, extradata_size) == 24));static_assert(offsetof(AVCodecDescriptor, id) == 0);static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(AVChannelLayout, nb_channels) == 4 && offsetof(AVChannelLayout, u) == 8));+static_assert(sizeof(void*) != 8 || (sizeof(AVChannelCustom) == 32 && offsetof(AVChannelCustom, id) == 0));#define FFMPEG_ENUMERATE_FUNCTIONS(F) \F(avcodec_version) \@@ -80,7 +81,6 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(AF(av_opt_set_chlayout) \F(av_opt_get_int) \F(av_opt_get_chlayout) \- F(av_channel_layout_custom_init) \F(av_channel_layout_copy) \F(av_channel_layout_channel_from_index) \F(av_channel_layout_uninit) \diff --git a/Tests/LibMedia/TestSystemFFmpeg.cpp b/Tests/LibMedia/TestSystemFFmpeg.cppindex 40785c17305..896c061544c 100644--- a/Tests/LibMedia/TestSystemFFmpeg.cpp+++ b/Tests/LibMedia/TestSystemFFmpeg.cpp@@ -6,6 +6,7 @@#include <LibMedia/DemuxerRegistry.h>#include <LibMedia/FFmpeg/FFmpegAudioDecoder.h>+#include <LibMedia/FFmpeg/FFmpegChannelLayout.h>#include <LibMedia/IncrementallyPopulatedStream.h>#include <LibMediaCodecs/AudioBlock.h>#include <LibMediaCodecs/Codecs/AAC.h>@@ -35,6 +36,29 @@ TEST_CASE(a_missing_library_is_an_error)EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::NotImplemented);}+static void expect_custom_channel_layout_round_trips(Media::FFmpeg::FFmpegFunctions const& functions)+{+ // 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);+}++TEST_CASE(custom_channel_layouts_round_trip_through_the_loaded_library)+{+ auto library = load_bundled_library();+ expect_custom_channel_layout_round_trips(library->functions());++ Media::FFmpeg::SystemFFmpeg::load();+ if (auto const* system_library = Media::FFmpeg::SystemFFmpeg::the())+ expect_custom_channel_layout_round_trips(system_library->functions());+}+static size_t decode_every_frame(Media::FFmpeg::SystemFFmpeg const& library, StringView path){auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));