LibMedia: Move FFmpegIOContext into it's own file

This allows it to be reused for video.
This commit is contained in:
Luke Wilde 2025-03-06 14:30:41 +00:00 committed by Alexander Kalenik
parent 2f9d2acdb2
commit 3412935a62
Notes: github-actions[bot] 2025-03-13 18:35:00 +00:00
5 changed files with 117 additions and 84 deletions

View file

@ -19,73 +19,7 @@ namespace Audio {
static constexpr int BUFFER_MAX_PROBE_SIZE = 64 * KiB;
FFmpegIOContext::FFmpegIOContext(AVIOContext* avio_context)
: m_avio_context(avio_context)
{
}
FFmpegIOContext::~FFmpegIOContext()
{
// NOTE: free the buffer inside the AVIO context, since it might be changed since its initial allocation
av_free(m_avio_context->buffer);
avio_context_free(&m_avio_context);
}
ErrorOr<NonnullOwnPtr<FFmpegIOContext>> FFmpegIOContext::create(AK::SeekableStream& stream)
{
auto* avio_buffer = av_malloc(PAGE_SIZE);
if (avio_buffer == nullptr)
return Error::from_string_literal("Failed to allocate AVIO buffer");
// This AVIOContext explains to avformat how to interact with our stream
auto* avio_context = avio_alloc_context(
static_cast<unsigned char*>(avio_buffer),
PAGE_SIZE,
0,
&stream,
[](void* opaque, u8* buffer, int size) -> int {
auto& stream = *static_cast<SeekableStream*>(opaque);
AK::Bytes buffer_bytes { buffer, AK::min<size_t>(size, PAGE_SIZE) };
auto read_bytes_or_error = stream.read_some(buffer_bytes);
if (read_bytes_or_error.is_error()) {
if (read_bytes_or_error.error().code() == EOF)
return AVERROR_EOF;
return AVERROR_UNKNOWN;
}
int number_of_bytes_read = read_bytes_or_error.value().size();
if (number_of_bytes_read == 0)
return AVERROR_EOF;
return number_of_bytes_read;
},
nullptr,
[](void* opaque, int64_t offset, int whence) -> int64_t {
whence &= ~AVSEEK_FORCE;
auto& stream = *static_cast<SeekableStream*>(opaque);
if (whence == AVSEEK_SIZE)
return static_cast<int64_t>(stream.size().value());
auto seek_mode_from_whence = [](int origin) -> SeekMode {
if (origin == SEEK_CUR)
return SeekMode::FromCurrentPosition;
if (origin == SEEK_END)
return SeekMode::FromEndPosition;
return SeekMode::SetPosition;
};
auto offset_or_error = stream.seek(offset, seek_mode_from_whence(whence));
if (offset_or_error.is_error())
return -EIO;
return 0;
});
if (avio_context == nullptr) {
av_free(avio_buffer);
return Error::from_string_literal("Failed to allocate AVIO context");
}
return make<FFmpegIOContext>(avio_context);
}
FFmpegLoaderPlugin::FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream> stream, NonnullOwnPtr<FFmpegIOContext> io_context)
FFmpegLoaderPlugin::FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream> stream, NonnullOwnPtr<Media::FFmpeg::FFmpegIOContext> io_context)
: LoaderPlugin(move(stream))
, m_io_context(move(io_context))
{
@ -105,7 +39,7 @@ FFmpegLoaderPlugin::~FFmpegLoaderPlugin()
ErrorOr<NonnullOwnPtr<LoaderPlugin>> FFmpegLoaderPlugin::create(NonnullOwnPtr<SeekableStream> stream)
{
auto io_context = TRY(FFmpegIOContext::create(*stream));
auto io_context = TRY(Media::FFmpeg::FFmpegIOContext::create(*stream));
auto loader = make<FFmpegLoaderPlugin>(move(stream), move(io_context));
TRY(loader->initialize());
return loader;
@ -180,7 +114,7 @@ double FFmpegLoaderPlugin::time_base() const
bool FFmpegLoaderPlugin::sniff(SeekableStream& stream)
{
auto io_context = MUST(FFmpegIOContext::create(stream));
auto io_context = MUST(Media::FFmpeg::FFmpegIOContext::create(stream));
#ifdef USE_CONSTIFIED_POINTERS
AVInputFormat const* detected_format {};
#else

View file

@ -9,6 +9,7 @@
#include "Loader.h"
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <LibMedia/FFmpeg/FFmpegIOContext.h>
extern "C" {
#include <libavcodec/avcodec.h>
@ -17,22 +18,9 @@ extern "C" {
namespace Audio {
class FFmpegIOContext {
public:
explicit FFmpegIOContext(AVIOContext*);
~FFmpegIOContext();
static ErrorOr<NonnullOwnPtr<FFmpegIOContext>> create(AK::SeekableStream& stream);
AVIOContext* avio_context() const { return m_avio_context; }
private:
AVIOContext* m_avio_context { nullptr };
};
class FFmpegLoaderPlugin : public LoaderPlugin {
public:
explicit FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream>, NonnullOwnPtr<FFmpegIOContext>);
explicit FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream>, NonnullOwnPtr<Media::FFmpeg::FFmpegIOContext>);
virtual ~FFmpegLoaderPlugin();
static bool sniff(SeekableStream& stream);
@ -58,7 +46,7 @@ private:
AVCodecContext* m_codec_context { nullptr };
AVFormatContext* m_format_context { nullptr };
AVFrame* m_frame { nullptr };
NonnullOwnPtr<FFmpegIOContext> m_io_context;
NonnullOwnPtr<Media::FFmpeg::FFmpegIOContext> m_io_context;
int m_loaded_samples { 0 };
AVPacket* m_packet { nullptr };
int m_total_samples { 0 };

View file

@ -22,6 +22,7 @@ target_link_libraries(LibMedia PRIVATE LibCore LibCrypto LibRIFF LibIPC LibGfx L
if (NOT ANDROID)
target_sources(LibMedia PRIVATE
Audio/FFmpegLoader.cpp
FFmpeg/FFmpegIOContext.cpp
FFmpeg/FFmpegVideoDecoder.cpp
)
target_link_libraries(LibMedia PRIVATE PkgConfig::AVCODEC PkgConfig::AVFORMAT PkgConfig::AVUTIL)

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Stream.h>
#include <LibMedia/FFmpeg/FFmpegIOContext.h>
namespace Media::FFmpeg {
FFmpegIOContext::FFmpegIOContext(AVIOContext* avio_context)
: m_avio_context(avio_context)
{
}
FFmpegIOContext::~FFmpegIOContext()
{
// NOTE: free the buffer inside the AVIO context, since it might be changed since its initial allocation
av_free(m_avio_context->buffer);
avio_context_free(&m_avio_context);
}
ErrorOr<NonnullOwnPtr<FFmpegIOContext>> FFmpegIOContext::create(AK::SeekableStream& stream)
{
auto* avio_buffer = av_malloc(PAGE_SIZE);
if (avio_buffer == nullptr)
return Error::from_string_literal("Failed to allocate AVIO buffer");
// This AVIOContext explains to avformat how to interact with our stream
auto* avio_context = avio_alloc_context(
static_cast<unsigned char*>(avio_buffer),
PAGE_SIZE,
0,
&stream,
[](void* opaque, u8* buffer, int size) -> int {
auto& stream = *static_cast<SeekableStream*>(opaque);
AK::Bytes buffer_bytes { buffer, AK::min<size_t>(size, PAGE_SIZE) };
auto read_bytes_or_error = stream.read_some(buffer_bytes);
if (read_bytes_or_error.is_error()) {
if (read_bytes_or_error.error().code() == EOF)
return AVERROR_EOF;
return AVERROR_UNKNOWN;
}
int number_of_bytes_read = read_bytes_or_error.value().size();
if (number_of_bytes_read == 0)
return AVERROR_EOF;
return number_of_bytes_read;
},
nullptr,
[](void* opaque, int64_t offset, int whence) -> int64_t {
whence &= ~AVSEEK_FORCE;
auto& stream = *static_cast<SeekableStream*>(opaque);
if (whence == AVSEEK_SIZE)
return static_cast<int64_t>(stream.size().value());
auto seek_mode_from_whence = [](int origin) -> SeekMode {
if (origin == SEEK_CUR)
return SeekMode::FromCurrentPosition;
if (origin == SEEK_END)
return SeekMode::FromEndPosition;
return SeekMode::SetPosition;
};
auto offset_or_error = stream.seek(offset, seek_mode_from_whence(whence));
if (offset_or_error.is_error())
return -EIO;
return 0;
});
if (avio_context == nullptr) {
av_free(avio_buffer);
return Error::from_string_literal("Failed to allocate AVIO context");
}
return make<FFmpegIOContext>(avio_context);
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
namespace Media::FFmpeg {
class FFmpegIOContext {
public:
explicit FFmpegIOContext(AVIOContext*);
~FFmpegIOContext();
static ErrorOr<NonnullOwnPtr<FFmpegIOContext>> create(AK::SeekableStream& stream);
AVIOContext* avio_context() const { return m_avio_context; }
private:
AVIOContext* m_avio_context { nullptr };
};
}