Skip to content

Truly independent web browser

LibMedia: Select system FFmpeg codec IDs by their names

Zaggy1024d82be35cf2fc
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp b/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp
index a62d2091490..642039c4584 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp
+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp
@@ -34,7 +34,7 @@ Optional<DecoderCapabilities> FFmpegAudioDecoder::capabilities(FFmpegFunctions c
return {};
if (codec.codec_id() == CodecID::AAC && !aac_audio_object_type_is_supported(codec))
return {};
- if (!functions.avcodec_find_decoder(ffmpeg_codec_id_from_media_codec_id(codec.codec_id())))
+ if (!functions.avcodec_find_decoder(functions.ffmpeg_codec_id(codec.codec_id())))
return {};
return DecoderCapabilities { .smooth = true, .power_efficient = true };
}
@@ -52,8 +52,7 @@ DecoderErrorOr<NonnullOwnPtr<FFmpegAudioDecoder>> FFmpegAudioDecoder::try_create
}
};
- auto ff_codec_id = ffmpeg_codec_id_from_media_codec_id(codec_id);
- auto const* codec = functions.avcodec_find_decoder(ff_codec_id);
+ auto const* codec = functions.avcodec_find_decoder(functions.ffmpeg_codec_id(codec_id));
if (!codec)
return DecoderError::format(DecoderErrorCategory::NotImplemented, "Could not find FFmpeg decoder for codec {}", codec_id);
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegFunctions.cpp b/Libraries/LibMedia/FFmpeg/FFmpegFunctions.cpp
index 13011ce6d3b..e01e6a232b4 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegFunctions.cpp
+++ b/Libraries/LibMedia/FFmpeg/FFmpegFunctions.cpp
@@ -5,6 +5,7 @@
*/
#include <LibMedia/FFmpeg/FFmpegFunctions.h>
+#include <LibMedia/FFmpeg/FFmpegHelpers.h>
namespace Media::FFmpeg {
@@ -14,6 +15,12 @@ FFmpegFunctions const& FFmpegFunctions::bundled()
#define FFMPEG_LINKED_FUNCTION(name) .name = &::name,
FFMPEG_ENUMERATE_FUNCTIONS(FFMPEG_LINKED_FUNCTION)
#undef FFMPEG_LINKED_FUNCTION
+ .codec_ids = [] {
+ CodecIDTable codec_ids {};
+ for (auto codec_id : all_codec_ids)
+ codec_ids[to_underlying(codec_id)] = ffmpeg_codec_id_from_media_codec_id(codec_id);
+ return codec_ids;
+ }(),
};
return functions;
}
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegFunctions.h b/Libraries/LibMedia/FFmpeg/FFmpegFunctions.h
index d8d5f086398..fec55e39b53 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegFunctions.h
+++ b/Libraries/LibMedia/FFmpeg/FFmpegFunctions.h
@@ -6,8 +6,11 @@
#pragma once
+#include <AK/Array.h>
+#include <AK/StdLibExtras.h>
#include <AK/Types.h>
#include <AK/kmalloc.h>
+#include <LibMedia/CodecID.h>
extern "C" {
#include <libavcodec/avcodec.h>
@@ -26,11 +29,13 @@ constexpr unsigned HIGHEST_VERIFIED_LIBAVCODEC_MAJOR = 63;
static_assert(sizeof(void*) != 8 || (offsetof(AVPacket, pts) == 8 && offsetof(AVPacket, dts) == 16 && offsetof(AVPacket, data) == 24 && offsetof(AVPacket, size) == 32 && offsetof(AVPacket, duration) == 64));
static_assert(sizeof(void*) != 8 || (offsetof(AVFrame, data) == 0 && offsetof(AVFrame, linesize) == 64 && offsetof(AVFrame, extended_data) == 96 && offsetof(AVFrame, width) == 104 && offsetof(AVFrame, height) == 108 && offsetof(AVFrame, nb_samples) == 112 && offsetof(AVFrame, format) == 116 && offsetof(AVFrame, pts) == 136));
static_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));
#define FFMPEG_ENUMERATE_FUNCTIONS(F) \
F(avcodec_version) \
F(avcodec_find_decoder) \
+ F(avcodec_descriptor_get_by_name) \
F(avcodec_alloc_context3) \
F(avcodec_free_context) \
F(avcodec_open2) \
@@ -69,6 +74,10 @@ struct FFmpegFunctions {
FFMPEG_ENUMERATE_FUNCTIONS(FFMPEG_FUNCTION_POINTER)
#undef FFMPEG_FUNCTION_POINTER
+ using CodecIDTable = Array<AVCodecID, all_codec_ids.size() + 1>;
+ CodecIDTable codec_ids;
+ AVCodecID ffmpeg_codec_id(CodecID codec_id) const { return codec_ids[to_underlying(codec_id)]; }
+
static FFmpegFunctions const& bundled();
};
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegHelpers.cpp b/Libraries/LibMedia/FFmpeg/FFmpegHelpers.cpp
index f608aaca5d3..52b2fb11864 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegHelpers.cpp
+++ b/Libraries/LibMedia/FFmpeg/FFmpegHelpers.cpp
@@ -23,7 +23,7 @@ DecoderErrorOr<void> set_codec_initialization_data(FFmpegFunctions const& functi
ScopeGuard free_parameters = [&] { functions.avcodec_parameters_free(&parameters); };
parameters->codec_type = ffmpeg_media_type_from_track_type(track_type_from_codec_id(codec_id));
- parameters->codec_id = ffmpeg_codec_id_from_media_codec_id(codec_id);
+ parameters->codec_id = functions.ffmpeg_codec_id(codec_id);
parameters->extradata = static_cast<u8*>(functions.av_malloc(codec_initialization_data.size() + AV_INPUT_BUFFER_PADDING_SIZE));
if (!parameters->extradata)
return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to allocate codec initialization data buffer for FFmpeg codec"sv);
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegHelpers.h b/Libraries/LibMedia/FFmpeg/FFmpegHelpers.h
index 55fdd8aedd7..0857d088e3f 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegHelpers.h
+++ b/Libraries/LibMedia/FFmpeg/FFmpegHelpers.h
@@ -19,7 +19,7 @@ extern "C" {
namespace Media::FFmpeg {
-static inline AVCodecID ffmpeg_codec_id_from_media_codec_id(CodecID codec)
+constexpr AVCodecID ffmpeg_codec_id_from_media_codec_id(CodecID codec)
{
switch (codec) {
case CodecID::VP8:
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegVideoDecoder.cpp b/Libraries/LibMedia/FFmpeg/FFmpegVideoDecoder.cpp
index 40b93dc5777..372bedff851 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegVideoDecoder.cpp
+++ b/Libraries/LibMedia/FFmpeg/FFmpegVideoDecoder.cpp
@@ -21,7 +21,7 @@ Optional<DecoderCapabilities> FFmpegVideoDecoder::capabilities(FFmpegFunctions c
{
if (track_type_from_codec_id(codec.codec_id()) != TrackType::Video)
return {};
- if (!functions.avcodec_find_decoder(ffmpeg_codec_id_from_media_codec_id(codec.codec_id())))
+ if (!functions.avcodec_find_decoder(functions.ffmpeg_codec_id(codec.codec_id())))
return {};
return DecoderCapabilities { .smooth = true, .power_efficient = false };
}
@@ -39,8 +39,7 @@ DecoderErrorOr<NonnullOwnPtr<FFmpegVideoDecoder>> FFmpegVideoDecoder::try_create
}
};
- auto ff_codec_id = ffmpeg_codec_id_from_media_codec_id(codec_id);
- auto const* codec = functions.avcodec_find_decoder(ff_codec_id);
+ auto const* codec = functions.avcodec_find_decoder(functions.ffmpeg_codec_id(codec_id));
if (!codec)
return DecoderError::format(DecoderErrorCategory::NotImplemented, "Could not find FFmpeg decoder for codec {}", codec_id);
diff --git a/Libraries/LibMedia/FFmpeg/SystemFFmpeg.cpp b/Libraries/LibMedia/FFmpeg/SystemFFmpeg.cpp
index 14775aade34..4ec278afb8e 100644
--- a/Libraries/LibMedia/FFmpeg/SystemFFmpeg.cpp
+++ b/Libraries/LibMedia/FFmpeg/SystemFFmpeg.cpp
@@ -49,6 +49,16 @@ static DecoderErrorOr<void> verify_library_layout(FFmpegFunctions const& functio
return {};
}
+static void resolve_codec_ids(FFmpegFunctions& functions)
+{
+ for (auto codec_id : all_codec_ids) {
+ auto const* bundled_descriptor = avcodec_descriptor_get(ffmpeg_codec_id_from_media_codec_id(codec_id));
+ VERIFY(bundled_descriptor != nullptr);
+ auto const* descriptor = functions.avcodec_descriptor_get_by_name(bundled_descriptor->name);
+ functions.codec_ids[to_underlying(codec_id)] = descriptor != nullptr ? descriptor->id : AV_CODEC_ID_NONE;
+ }
+}
+
DecoderErrorOr<NonnullOwnPtr<SystemFFmpeg>> SystemFFmpeg::try_load(ByteString const& library_path)
{
auto* library_handle = dlopen(library_path.characters(), RTLD_NOW | RTLD_LOCAL);
@@ -68,6 +78,7 @@ DecoderErrorOr<NonnullOwnPtr<SystemFFmpeg>> SystemFFmpeg::try_load(ByteString co
if (major < LOWEST_SUPPORTED_LIBAVCODEC_MAJOR || major > HIGHEST_VERIFIED_LIBAVCODEC_MAJOR)
return DecoderError::format(DecoderErrorCategory::NotImplemented, "libavcodec major {} provided by {} is outside the supported range", major, library_path);
TRY(verify_library_layout(*functions));
+ resolve_codec_ids(*functions);
close_library.disarm();
return DECODER_TRY_ALLOC(try_make<SystemFFmpeg>(library_handle, move(functions), major));
@@ -120,15 +131,14 @@ SystemFFmpeg::~SystemFFmpeg()
bool SystemFFmpeg::has_decoder(CodecID codec_id) const
{
- return m_functions->avcodec_find_decoder(ffmpeg_codec_id_from_media_codec_id(codec_id)) != nullptr;
+ return m_functions->avcodec_find_decoder(m_functions->ffmpeg_codec_id(codec_id)) != nullptr;
}
bool SystemFFmpeg::provides_decoder_missing_from_bundle() const
{
auto const& bundled = FFmpegFunctions::bundled();
return any_of(all_codec_ids, [&](CodecID codec_id) {
- auto ffmpeg_codec_id = ffmpeg_codec_id_from_media_codec_id(codec_id);
- return bundled.avcodec_find_decoder(ffmpeg_codec_id) == nullptr && m_functions->avcodec_find_decoder(ffmpeg_codec_id) != nullptr;
+ return bundled.avcodec_find_decoder(bundled.ffmpeg_codec_id(codec_id)) == nullptr && has_decoder(codec_id);
});
}
diff --git a/Tests/LibMedia/TestSystemFFmpeg.cpp b/Tests/LibMedia/TestSystemFFmpeg.cpp
index ddd3cf20d66..73e8516fb0a 100644
--- a/Tests/LibMedia/TestSystemFFmpeg.cpp
+++ b/Tests/LibMedia/TestSystemFFmpeg.cpp
@@ -32,19 +32,17 @@ TEST_CASE(a_missing_library_is_an_error)
EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::NotImplemented);
}
-TEST_CASE(decodes_through_the_loaded_library)
+static size_t decode_every_frame(Media::FFmpeg::SystemFFmpeg const& library, StringView path)
{
- auto library = load_bundled_library();
-
- auto file = MUST(Core::File::open("./vp9_in_webm.webm"sv, Core::File::OpenMode::Read));
+ auto file = MUST(Core::File::open(path, Core::File::OpenMode::Read));
auto stream = Media::IncrementallyPopulatedStream::create_from_buffer(MUST(file->read_until_eof()));
auto demuxer = MUST(Media::create_demuxer(stream));
auto track = MUST(demuxer->get_preferred_track_for_type(Media::TrackType::Video)).release_value();
MUST(demuxer->create_context_for_track(track));
auto first_sample = MUST(demuxer->get_next_sample_for_track(track));
- EXPECT(Media::FFmpeg::FFmpegVideoDecoder::capabilities(library->functions(), Media::ParsedCodec { first_sample.codec_id() }).has_value());
- auto decoder = MUST(Media::FFmpeg::FFmpegVideoDecoder::try_create(library->functions(), first_sample.codec_id(), first_sample.new_codec_configuration().value_or({})));
+ EXPECT(Media::FFmpeg::FFmpegVideoDecoder::capabilities(library.functions(), Media::ParsedCodec { first_sample.codec_id() }).has_value());
+ auto decoder = MUST(Media::FFmpeg::FFmpegVideoDecoder::try_create(library.functions(), first_sample.codec_id(), first_sample.new_codec_configuration().value_or({})));
MUST(decoder->receive_coded_data(first_sample, Media::DecodeIntent::Output));
size_t frame_count = 0;
@@ -68,5 +66,22 @@ TEST_CASE(decodes_through_the_loaded_library)
}
decoder->signal_end_of_stream();
EXPECT_EQ(drain().category(), Media::DecoderErrorCategory::EndOfStream);
- EXPECT(frame_count > 0);
+ return frame_count;
+}
+
+TEST_CASE(decodes_through_the_loaded_library)
+{
+ auto library = load_bundled_library();
+ EXPECT(decode_every_frame(*library, "./vp9_in_webm.webm"sv) > 0);
+}
+
+// HEVC sits after codecs that newer libavcodec majors removed, so its ID differs from the bundled headers'.
+TEST_CASE(decodes_hevc_through_the_system_library)
+{
+ auto const* library = Media::FFmpeg::SystemFFmpeg::the();
+ if (library == nullptr || !library->has_decoder(Media::CodecID::H265)) {
+ warnln("No system FFmpeg decodes HEVC, skipping");
+ return;
+ }
+ EXPECT(decode_every_frame(*library, "./hevc_open_gop.mp4"sv) > 0);
}