mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 20:29:18 +00:00
LibMedia: Move FFmpegIOContext into it's own file
This allows it to be reused for video.
This commit is contained in:
parent
2f9d2acdb2
commit
3412935a62
Notes:
github-actions[bot]
2025-03-13 18:35:00 +00:00
Author: https://github.com/Lubrsi
Commit: 3412935a62
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3892
Reviewed-by: https://github.com/ADKaster
5 changed files with 117 additions and 84 deletions
|
@ -19,73 +19,7 @@ namespace Audio {
|
||||||
|
|
||||||
static constexpr int BUFFER_MAX_PROBE_SIZE = 64 * KiB;
|
static constexpr int BUFFER_MAX_PROBE_SIZE = 64 * KiB;
|
||||||
|
|
||||||
FFmpegIOContext::FFmpegIOContext(AVIOContext* avio_context)
|
FFmpegLoaderPlugin::FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream> stream, NonnullOwnPtr<Media::FFmpeg::FFmpegIOContext> io_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)
|
|
||||||
: LoaderPlugin(move(stream))
|
: LoaderPlugin(move(stream))
|
||||||
, m_io_context(move(io_context))
|
, m_io_context(move(io_context))
|
||||||
{
|
{
|
||||||
|
@ -105,7 +39,7 @@ FFmpegLoaderPlugin::~FFmpegLoaderPlugin()
|
||||||
|
|
||||||
ErrorOr<NonnullOwnPtr<LoaderPlugin>> FFmpegLoaderPlugin::create(NonnullOwnPtr<SeekableStream> stream)
|
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));
|
auto loader = make<FFmpegLoaderPlugin>(move(stream), move(io_context));
|
||||||
TRY(loader->initialize());
|
TRY(loader->initialize());
|
||||||
return loader;
|
return loader;
|
||||||
|
@ -180,7 +114,7 @@ double FFmpegLoaderPlugin::time_base() const
|
||||||
|
|
||||||
bool FFmpegLoaderPlugin::sniff(SeekableStream& stream)
|
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
|
#ifdef USE_CONSTIFIED_POINTERS
|
||||||
AVInputFormat const* detected_format {};
|
AVInputFormat const* detected_format {};
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "Loader.h"
|
#include "Loader.h"
|
||||||
#include <AK/Error.h>
|
#include <AK/Error.h>
|
||||||
#include <AK/NonnullOwnPtr.h>
|
#include <AK/NonnullOwnPtr.h>
|
||||||
|
#include <LibMedia/FFmpeg/FFmpegIOContext.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <libavcodec/avcodec.h>
|
#include <libavcodec/avcodec.h>
|
||||||
|
@ -17,22 +18,9 @@ extern "C" {
|
||||||
|
|
||||||
namespace Audio {
|
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 {
|
class FFmpegLoaderPlugin : public LoaderPlugin {
|
||||||
public:
|
public:
|
||||||
explicit FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream>, NonnullOwnPtr<FFmpegIOContext>);
|
explicit FFmpegLoaderPlugin(NonnullOwnPtr<SeekableStream>, NonnullOwnPtr<Media::FFmpeg::FFmpegIOContext>);
|
||||||
virtual ~FFmpegLoaderPlugin();
|
virtual ~FFmpegLoaderPlugin();
|
||||||
|
|
||||||
static bool sniff(SeekableStream& stream);
|
static bool sniff(SeekableStream& stream);
|
||||||
|
@ -58,7 +46,7 @@ private:
|
||||||
AVCodecContext* m_codec_context { nullptr };
|
AVCodecContext* m_codec_context { nullptr };
|
||||||
AVFormatContext* m_format_context { nullptr };
|
AVFormatContext* m_format_context { nullptr };
|
||||||
AVFrame* m_frame { nullptr };
|
AVFrame* m_frame { nullptr };
|
||||||
NonnullOwnPtr<FFmpegIOContext> m_io_context;
|
NonnullOwnPtr<Media::FFmpeg::FFmpegIOContext> m_io_context;
|
||||||
int m_loaded_samples { 0 };
|
int m_loaded_samples { 0 };
|
||||||
AVPacket* m_packet { nullptr };
|
AVPacket* m_packet { nullptr };
|
||||||
int m_total_samples { 0 };
|
int m_total_samples { 0 };
|
||||||
|
|
|
@ -22,6 +22,7 @@ target_link_libraries(LibMedia PRIVATE LibCore LibCrypto LibRIFF LibIPC LibGfx L
|
||||||
if (NOT ANDROID)
|
if (NOT ANDROID)
|
||||||
target_sources(LibMedia PRIVATE
|
target_sources(LibMedia PRIVATE
|
||||||
Audio/FFmpegLoader.cpp
|
Audio/FFmpegLoader.cpp
|
||||||
|
FFmpeg/FFmpegIOContext.cpp
|
||||||
FFmpeg/FFmpegVideoDecoder.cpp
|
FFmpeg/FFmpegVideoDecoder.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(LibMedia PRIVATE PkgConfig::AVCODEC PkgConfig::AVFORMAT PkgConfig::AVUTIL)
|
target_link_libraries(LibMedia PRIVATE PkgConfig::AVCODEC PkgConfig::AVFORMAT PkgConfig::AVUTIL)
|
||||||
|
|
78
Libraries/LibMedia/FFmpeg/FFmpegIOContext.cpp
Normal file
78
Libraries/LibMedia/FFmpeg/FFmpegIOContext.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
32
Libraries/LibMedia/FFmpeg/FFmpegIOContext.h
Normal file
32
Libraries/LibMedia/FFmpeg/FFmpegIOContext.h
Normal 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 };
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue