Truly independent web browser
LibMedia+Meta: Add an FFmpeg audio encoder, with libopus for Opus
Ali Mohammad Purba3a1c75af8e
diff --git a/Libraries/LibMedia/CMakeLists.txt b/Libraries/LibMedia/CMakeLists.txtindex 19a4baeb6b4..7cd38116e80 100644--- a/Libraries/LibMedia/CMakeLists.txt+++ b/Libraries/LibMedia/CMakeLists.txt@@ -106,6 +106,7 @@ endif()target_sources(LibMedia PRIVATEFFmpeg/FFmpegAudioConverter.cppFFmpeg/FFmpegAudioDecoder.cpp+ FFmpeg/FFmpegAudioEncoder.cppFFmpeg/FFmpegChannelLayout.cpp)diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cpp b/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cppnew file mode 100644index 00000000000..cfdf1e41953--- /dev/null+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.cpp@@ -0,0 +1,170 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/ScopeGuard.h>+#include <LibMedia/FFmpeg/FFmpegChannelLayout.h>+#include <LibMediaCodecs/FFmpeg/FFmpegFunctions.h>+#include <LibMediaCodecs/FFmpeg/FFmpegHelpers.h>++#include "FFmpegAudioEncoder.h"++namespace Media::FFmpeg {++DecoderErrorOr<NonnullOwnPtr<FFmpegAudioEncoder>> FFmpegAudioEncoder::try_create(CodecID codec_id, Audio::SampleSpecification const& sample_specification, int bitrate)+{+ // The encoder fills in and reads the bundled FFmpeg's structs, so it only runs against that FFmpeg.+ auto const& functions = FFmpegFunctions::bundled();++ AVCodecContext* codec_context = nullptr;+ AVPacket* packet = nullptr;+ AVFrame* frame = nullptr;+ ArmedScopeGuard memory_guard {+ [&] {+ functions.avcodec_free_context(&codec_context);+ functions.av_packet_free(&packet);+ functions.av_frame_free(&frame);+ }+ };++ auto const* codec = functions.avcodec_find_encoder(functions.ffmpeg_codec_id(codec_id));+ if (!codec)+ return DecoderError::format(DecoderErrorCategory::NotImplemented, "Could not find FFmpeg encoder for codec {}", codec_id);++ codec_context = functions.avcodec_alloc_context3(codec);+ if (!codec_context)+ return DecoderError::format(DecoderErrorCategory::Memory, "Failed to allocate FFmpeg codec context for codec {}", codec_id);++ if (sample_specification.sample_rate() > NumericLimits<int>::max())+ return DecoderError::with_description(DecoderErrorCategory::Corrupted, "Sample rate is too large"sv);+ codec_context->sample_rate = static_cast<int>(sample_specification.sample_rate());+ codec_context->time_base = AVRational { 1, codec_context->sample_rate };+ codec_context->bit_rate = bitrate;+ codec_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());+ 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();+ }++ if (functions.avcodec_open2(codec_context, codec, nullptr) < 0)+ return DecoderError::format(DecoderErrorCategory::Unknown, "Unknown error occurred when opening FFmpeg encoder {}", codec_id);++ packet = functions.av_packet_alloc();+ if (!packet)+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to allocate FFmpeg packet"sv);++ frame = functions.av_frame_alloc();+ if (!frame)+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to allocate FFmpeg frame"sv);++ memory_guard.disarm();+ return DECODER_TRY_ALLOC(try_make<FFmpegAudioEncoder>(functions, codec_context, packet, frame));+}++FFmpegAudioEncoder::FFmpegAudioEncoder(FFmpegFunctions const& functions, AVCodecContext* codec_context, AVPacket* packet, AVFrame* frame)+ : m_functions(functions)+ , m_codec_context(codec_context)+ , m_packet(packet)+ , m_frame(frame)+{+}++FFmpegAudioEncoder::~FFmpegAudioEncoder()+{+ m_functions.av_packet_free(&m_packet);+ m_functions.av_frame_free(&m_frame);+ m_functions.avcodec_free_context(&m_codec_context);+}++int FFmpegAudioEncoder::frame_size() const+{+ return m_codec_context->frame_size;+}++DecoderErrorOr<void> FFmpegAudioEncoder::receive_pcm_data(AK::Duration timestamp, AudioBlock const& block)+{+ auto channel_count = block.channel_count();+ if (channel_count == 0)+ return DecoderError::with_description(DecoderErrorCategory::Invalid, "AudioBlock has no channels"sv);++ auto frame_count = block.frame_count();+ if (frame_count == 0)+ return {};++ m_functions.av_frame_unref(m_frame);+ m_frame->nb_samples = static_cast<int>(frame_count);+ m_frame->format = AV_SAMPLE_FMT_FLT;+ m_frame->sample_rate = m_codec_context->sample_rate;+ if (m_functions.av_channel_layout_copy(&m_frame->ch_layout, &m_codec_context->ch_layout) < 0)+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to copy AVChannelLayout to encoder frame"sv);+ m_frame->pts = m_functions.av_rescale_q(timestamp.to_microseconds(), AVRational { 1, 1'000'000 }, m_codec_context->time_base);++ if (m_functions.av_frame_get_buffer(m_frame, 0) < 0)+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to allocate encoder frame buffer"sv);++ // AV_SAMPLE_FMT_FLT is packed/interleaved, so the whole frame lives in data[0].+ auto sample_count = block.sample_count();+ auto destination = Span<float> { reinterpret_cast<float*>(m_frame->data[0]), sample_count };+ if (block.copy_to_interleaved(destination) < sample_count)+ return DecoderError::with_description(DecoderErrorCategory::Corrupted, "AudioBlock produced fewer samples than expected"sv);++ auto result = m_functions.avcodec_send_frame(m_codec_context, m_frame);+ switch (result) {+ case 0:+ return {};+ case AVERROR(EAGAIN):+ return DecoderError::with_description(DecoderErrorCategory::NeedsMoreInput, "FFmpeg encoder cannot accept more frames until packets have been retrieved"sv);+ case AVERROR_EOF:+ return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "FFmpeg encoder has been flushed"sv);+ case AVERROR(EINVAL):+ return DecoderError::with_description(DecoderErrorCategory::Invalid, "FFmpeg encoder is in an invalid state"sv);+ case AVERROR(ENOMEM):+ return DecoderError::with_description(DecoderErrorCategory::Memory, "FFmpeg encoder ran out of internal memory"sv);+ default:+ return DecoderError::format(DecoderErrorCategory::Unknown, "FFmpeg encoder reported error code {:x}", result);+ }+}++DecoderErrorOr<bool> FFmpegAudioEncoder::write_next_packet(Packet& out)+{+ m_functions.av_packet_unref(m_packet);+ auto result = m_functions.avcodec_receive_packet(m_codec_context, m_packet);+ switch (result) {+ case 0: {+ auto buffer_or_err = ByteBuffer::copy(m_packet->data, static_cast<size_t>(m_packet->size));+ if (buffer_or_err.is_error())+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to copy encoded packet bytes"sv);+ out.data = buffer_or_err.release_value();+ // The encoder reports timestamps in its time_base (1 / sample_rate), as it was fed them.+ out.timestamp = AK::Duration::from_microseconds(m_functions.av_rescale_q(m_packet->pts, m_codec_context->time_base, AVRational { 1, 1'000'000 }));+ out.duration = AK::Duration::from_microseconds(m_functions.av_rescale_q(m_packet->duration, m_codec_context->time_base, AVRational { 1, 1'000'000 }));+ return true;+ }+ case AVERROR(EAGAIN):+ return false;+ case AVERROR_EOF:+ return false;+ case AVERROR(EINVAL):+ return DecoderError::with_description(DecoderErrorCategory::Invalid, "FFmpeg encoder has not been opened"sv);+ default:+ return DecoderError::format(DecoderErrorCategory::Unknown, "FFmpeg encoder encountered an unexpected error retrieving packets with code {:x}", result);+ }+}++void FFmpegAudioEncoder::signal_end_of_stream()+{+ auto result = m_functions.avcodec_send_frame(m_codec_context, nullptr);+ VERIFY(result == 0 || result == AVERROR_EOF);+}++void FFmpegAudioEncoder::flush()+{+ m_functions.avcodec_flush_buffers(m_codec_context);+}++}diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.h b/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.hnew file mode 100644index 00000000000..3d49a3b728e--- /dev/null+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioEncoder.h@@ -0,0 +1,49 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/ByteBuffer.h>+#include <AK/NonnullOwnPtr.h>+#include <AK/Time.h>+#include <LibMedia/Export.h>+#include <LibMediaCodecs/AudioBlock.h>+#include <LibMediaCodecs/CodecID.h>+#include <LibMediaCodecs/DecoderError.h>+#include <LibMediaCodecs/FFmpeg/FFmpegForward.h>++namespace Media::FFmpeg {++// Feed PCM via receive_pcm_data, then drain encoded packets via write_next_packet until it reports NeedsMoreInput.+class MEDIA_API FFmpegAudioEncoder final {+ AK_ALLOC_WITH_KMALLOC;++public:+ struct Packet {+ ByteBuffer data;+ AK::Duration timestamp;+ AK::Duration duration;+ };++ static DecoderErrorOr<NonnullOwnPtr<FFmpegAudioEncoder>> try_create(CodecID, Audio::SampleSpecification const&, int bitrate);+ FFmpegAudioEncoder(FFmpegFunctions const&, AVCodecContext*, AVPacket*, AVFrame*);+ ~FFmpegAudioEncoder();++ int frame_size() const;++ DecoderErrorOr<void> receive_pcm_data(AK::Duration timestamp, AudioBlock const& block);+ DecoderErrorOr<bool> write_next_packet(Packet& out);+ void signal_end_of_stream();+ void flush();++private:+ FFmpegFunctions const& m_functions;+ AVCodecContext* m_codec_context;+ AVPacket* m_packet;+ AVFrame* m_frame;+};++}diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.hindex 031bf8bdbbd..4354d95f29f 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h@@ -39,12 +39,15 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(AF(avutil_version) \F(avcodec_find_decoder) \F(avcodec_find_decoder_by_name) \+ F(avcodec_find_encoder) \F(avcodec_descriptor_get_by_name) \F(avcodec_alloc_context3) \F(avcodec_free_context) \F(avcodec_open2) \F(avcodec_send_packet) \F(avcodec_receive_frame) \+ F(avcodec_send_frame) \+ F(avcodec_receive_packet) \F(avcodec_flush_buffers) \F(avcodec_get_hw_config) \F(avcodec_get_hw_frames_parameters) \@@ -54,6 +57,7 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(AF(avcodec_parameters_to_context) \F(av_packet_alloc) \F(av_packet_free) \+ F(av_packet_unref) \F(av_new_packet) \F(av_packet_free_side_data) \F(av_packet_new_side_data) \@@ -62,6 +66,7 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(AF(av_frame_unref) \F(av_frame_ref) \F(av_frame_move_ref) \+ F(av_frame_get_buffer) \F(av_buffer_ref) \F(av_buffer_unref) \F(av_hwdevice_ctx_alloc) \@@ -74,14 +79,16 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(AF(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) \F(av_sample_fmt_is_planar) \- F(av_get_planar_sample_fmt)+ F(av_get_planar_sample_fmt) \+ F(av_rescale_q)namespace Media::FFmpeg {-// Every libavcodec and libavutil entry point the decoders call, so they can run against a copy loaded at runtime.+// Every libavcodec and libavutil entry point the decoders and encoders call, so they can run against a copy loaded at runtime.struct MEDIACODECS_API FFmpegFunctions {AK_ALLOC_WITH_KMALLOC;diff --git a/Meta/CMake/vcpkg/overlay-ports/ffmpeg/portfile.cmake b/Meta/CMake/vcpkg/overlay-ports/ffmpeg/portfile.cmakeindex 85e8280f7d8..e7f0dc62b3e 100644--- a/Meta/CMake/vcpkg/overlay-ports/ffmpeg/portfile.cmake+++ b/Meta/CMake/vcpkg/overlay-ports/ffmpeg/portfile.cmake@@ -282,6 +282,9 @@ if("ladybird-components" IN_LIST FEATURES)set(OPTIONS "${OPTIONS} --disable-everything --disable-network")set(OPTIONS "${OPTIONS} --enable-decoder=vp8,vp9,libdav1d,theora,vorbis,opus,flac,mp3float,pcm_u8,pcm_s16le,pcm_s24le,pcm_s32le,pcm_f32le,pcm_alaw,pcm_mulaw")set(OPTIONS "${OPTIONS} --enable-parser=vp8,vp9,av1,mpegaudio")+ if("opus" IN_LIST FEATURES)+ set(OPTIONS "${OPTIONS} --enable-encoder=libopus")+ endif()if("vulkan" IN_LIST FEATURES)set(OPTIONS "${OPTIONS} --enable-decoder=av1 --enable-hwaccel=vp9_vulkan,av1_vulkan --enable-libdrm")endif()diff --git a/vcpkg.json b/vcpkg.jsonindex 597ac6525f4..4145a8e91ba 100644--- a/vcpkg.json+++ b/vcpkg.json@@ -46,6 +46,7 @@"avcodec","swresample","dav1d",+ "opus","zlib","ladybird-components",{