mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 09:57:36 +00:00
LibRIFF: Rework to match LibGfx needs
There's now two namespaces, RIFF (little-endian) and IFF (big-endian) which (for the most part) contain the same kinds of structures for handling similar data in both formats. (They also share almost all of their implementation) The main types are ChunkHeader and (Owned)Chunk. While Chunk has no ownership over the data it accesses (and can only be constructed from a byte view), OwnedChunk has ownership over this data and is aimed at reading from streams. OwnedList, implementing the standard RIFF LIST type, is currently only implemented for RIFF due to its only user being WAV, but it may be generalized in the future for use by IFF. Co-authored-by: Timothy Flynn <trflynn89@pm.me>
This commit is contained in:
parent
d125d16287
commit
64598473cc
11 changed files with 366 additions and 145 deletions
63
Userland/Libraries/LibRIFF/ChunkID.h
Normal file
63
Userland/Libraries/LibRIFF/ChunkID.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace RIFF {
|
||||
|
||||
static constexpr size_t const chunk_id_size = 4;
|
||||
|
||||
// Also referred to as "FourCC" (four character code) in the context of some formats.
|
||||
struct ChunkID {
|
||||
constexpr ChunkID(char const name[4])
|
||||
{
|
||||
id_data[0] = static_cast<u8>(name[0]);
|
||||
id_data[1] = static_cast<u8>(name[1]);
|
||||
id_data[2] = static_cast<u8>(name[2]);
|
||||
id_data[3] = static_cast<u8>(name[3]);
|
||||
}
|
||||
constexpr ChunkID(Array<u8, chunk_id_size> data)
|
||||
: id_data(data)
|
||||
{
|
||||
}
|
||||
constexpr ChunkID(ChunkID const&) = default;
|
||||
constexpr ChunkID(ChunkID&&) = default;
|
||||
constexpr ChunkID& operator=(ChunkID const&) = default;
|
||||
static constexpr ChunkID from_big_endian_number(u32 number) { return bit_cast<Array<u8, chunk_id_size>>(AK::convert_between_host_and_big_endian(number)); }
|
||||
|
||||
static ErrorOr<ChunkID> read_from_stream(Stream& stream);
|
||||
|
||||
StringView as_ascii_string() const;
|
||||
constexpr u32 as_big_endian_number() const
|
||||
{
|
||||
return AK::convert_between_host_and_big_endian((id_data[0] << 24) | (id_data[1] << 16) | (id_data[2] << 8) | id_data[3]);
|
||||
}
|
||||
|
||||
bool operator==(ChunkID const&) const = default;
|
||||
bool operator==(StringView) const;
|
||||
|
||||
Array<u8, chunk_id_size> id_data;
|
||||
};
|
||||
static_assert(AssertSize<ChunkID, chunk_id_size>());
|
||||
|
||||
}
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<RIFF::ChunkID> : StandardFormatter {
|
||||
ErrorOr<void> format(FormatBuilder& builder, RIFF::ChunkID const& chunk_id)
|
||||
{
|
||||
TRY(builder.put_padding('\'', 1));
|
||||
TRY(builder.put_literal(chunk_id.as_ascii_string()));
|
||||
TRY(builder.put_padding('\'', 1));
|
||||
return {};
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue