[APU] Initial version of new XMA decoder

This commit is contained in:
Gliniak 2024-01-23 19:42:44 +01:00
parent f1c8f5e5bc
commit d83d8eee01
3 changed files with 560 additions and 754 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Copyright 2023 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@ -14,8 +14,9 @@
#include <atomic>
#include <mutex>
#include <queue>
// #include <vector>
#include "xenia/base/bit_stream.h"
#include "xenia/base/ring_buffer.h"
#include "xenia/memory.h"
#include "xenia/xbox.h"
@ -76,7 +77,7 @@ struct XMA_CONTEXT_DATA {
// DWORD 2
uint32_t input_buffer_read_offset : 26; // XMAGetInputBufferReadOffset
uint32_t unk_dword_2 : 6; // ErrorStatus/ErrorSet (?)
uint32_t error_status : 6; // ErrorStatus/ErrorSet (?)
// DWORD 3
uint32_t loop_start : 26; // XMASetLoopData LoopStartOffset
@ -96,7 +97,7 @@ struct XMA_CONTEXT_DATA {
// DWORD 7
uint32_t output_buffer_ptr; // physical address
// DWORD 8
uint32_t work_buffer_ptr; // PtrOverlapAdd(?)
uint32_t work_buffer_ptr; // Stores currently processed subframe
// DWORD 9
// +0bit, XMAGetOutputBufferReadOffset AKA WriteBufferOffsetRead
@ -115,10 +116,40 @@ struct XMA_CONTEXT_DATA {
}
void Store(void* ptr) {
// TODO: Compare current ptr state with this and see which dwords we should
// write
xe::copy_and_swap(reinterpret_cast<uint32_t*>(ptr),
reinterpret_cast<const uint32_t*>(this),
sizeof(XMA_CONTEXT_DATA) / 4);
}
bool IsInputBufferValid(uint8_t buffer_index) const {
return buffer_index == 0 ? input_buffer_0_valid : input_buffer_1_valid;
}
bool IsCurrentInputBufferValid() const {
return IsInputBufferValid(current_buffer);
}
bool IsAnyInputBufferValid() const {
return input_buffer_0_valid || input_buffer_1_valid;
}
const uint32_t GetInputBufferAddress(uint8_t buffer_index) const {
return buffer_index == 0 ? input_buffer_0_ptr : input_buffer_1_ptr;
}
const uint32_t GetCurrentInputBufferAddress() const {
return GetInputBufferAddress(current_buffer);
}
const uint32_t GetInputBufferPacketCount(uint8_t buffer_index) const {
return buffer_index == 0 ? input_buffer_0_packet_count
: input_buffer_1_packet_count;
}
const uint32_t GetCurrentInputBufferPacketCount() const {
return GetInputBufferPacketCount(current_buffer);
}
};
static_assert_size(XMA_CONTEXT_DATA, 64);
@ -130,11 +161,28 @@ struct Xma2ExtraData {
static_assert_size(Xma2ExtraData, 34);
#pragma pack(pop)
struct kPacketInfo {
uint8_t frame_count_;
uint8_t current_frame_;
uint32_t current_frame_size_;
const bool isLastFrameInPacket() const {
return current_frame_ == frame_count_ - 1;
}
};
static constexpr int kIdToSampleRate[4] = {24000, 32000, 44100, 48000};
class XmaContext {
public:
static const uint32_t kBytesPerPacket = 2048;
static const uint32_t kBytesPerPacketHeader = 4;
static const uint32_t kBytesPerPacketData =
kBytesPerPacket - kBytesPerPacketHeader;
static const uint32_t kBitsPerPacket = kBytesPerPacket * 8;
static const uint32_t kBitsPerHeader = 32;
static const uint32_t kBitsPerPacketHeader = 32;
static const uint32_t kBitsPerFrameHeader = 15;
static const uint32_t kBytesPerSample = 2;
static const uint32_t kSamplesPerFrame = 512;
@ -144,8 +192,11 @@ class XmaContext {
static const uint32_t kBytesPerSubframeChannel =
kSamplesPerSubframe * kBytesPerSample;
// static const uint32_t kOutputBytesPerBlock = 256;
// static const uint32_t kOutputMaxSizeBytes = 31 * kOutputBytesPerBlock;
static const uint32_t kOutputBytesPerBlock = 256;
static const uint32_t kOutputMaxSizeBytes = 31 * kOutputBytesPerBlock;
static const uint32_t kLastFrameMarker = 0x7FFF;
static const uint32_t kMaxFrameSizeinBits = 0x4000 - kBitsPerPacketHeader;
explicit XmaContext();
~XmaContext();
@ -171,30 +222,44 @@ class XmaContext {
private:
static void SwapInputBuffer(XMA_CONTEXT_DATA* data);
static bool TrySetupNextLoop(XMA_CONTEXT_DATA* data,
bool ignore_input_buffer_offset);
static void NextPacket(XMA_CONTEXT_DATA* data);
// Convert sampling rate from ID to frequency.
static int GetSampleRate(int id);
// Get the offset of the next frame. Does not traverse packets.
static size_t GetNextFrame(uint8_t* block, size_t size, size_t bit_offset);
// Get the containing packet number of the frame pointed to by the offset.
static int GetFramePacketNumber(uint8_t* block, size_t size,
size_t bit_offset);
// Get the packet number and the index of the frame inside that packet
static std::tuple<int, int> GetFrameNumber(uint8_t* block, size_t size,
size_t bit_offset);
// Get the number of frames contained in the packet (including truncated) and
// if the last frame is split.
static std::tuple<int, bool> GetPacketFrameCount(uint8_t* packet);
static int16_t GetPacketNumber(size_t size, size_t bit_offset);
const kPacketInfo GetPacketInfo(uint8_t* packet, uint32_t frame_offset);
const uint32_t GetAmountOfBitsToRead(const uint32_t remaining_stream_bits,
const uint32_t frame_size);
const uint8_t* GetNextPacket(XMA_CONTEXT_DATA* data,
uint32_t next_packet_index,
uint32_t current_input_packet_count);
const uint32_t GetNextPacketReadOffset(uint8_t* buffer,
uint32_t next_packet_index,
uint32_t current_input_packet_count);
// Returns currently used buffer
uint8_t* GetCurrentInputBuffer(XMA_CONTEXT_DATA* data);
static uint32_t GetCurrentInputBufferSize(XMA_CONTEXT_DATA* data);
// Convert sample format and swap bytes
static void ConvertFrame(const uint8_t** samples, bool is_two_channel,
uint8_t* output_buffer);
bool ValidFrameOffset(uint8_t* block, size_t size_bytes,
size_t frame_offset_bits);
void Decode(XMA_CONTEXT_DATA* data);
int PrepareDecoder(uint8_t* packet, int sample_rate, bool is_two_channel);
void Consume(RingBuffer* output_rb, XMA_CONTEXT_DATA* data);
void UpdateLoopStatus(XMA_CONTEXT_DATA* data);
int PrepareDecoder(int sample_rate, bool is_two_channel);
void PreparePacket(const uint32_t frame_size, const uint32_t frame_padding);
RingBuffer PrepareOutputRingBuffer(XMA_CONTEXT_DATA* data);
bool DecodePacket(AVCodecContext* av_context, const AVPacket* av_packet,
AVFrame* av_frame);
// This method should be used ONLY when we're at the last packet of the stream
// and we want to find offset in next buffer
@ -204,39 +269,24 @@ class XmaContext {
uint32_t id_ = 0;
uint32_t guest_ptr_ = 0;
xe_mutex lock_;
volatile bool is_allocated_ = false;
volatile bool is_enabled_ = false;
// bool is_dirty_ = true;
// ffmpeg structures
AVPacket* av_packet_ = nullptr;
AVCodec* av_codec_ = nullptr;
AVCodecContext* av_context_ = nullptr;
AVFrame* av_frame_ = nullptr;
// uint32_t decoded_consumed_samples_ = 0; // TODO do this dynamically
// int decoded_idx_ = -1;
// bool partial_frame_saved_ = false;
// bool partial_frame_size_known_ = false;
// size_t partial_frame_total_size_bits_ = 0;
// size_t partial_frame_start_offset_bits_ = 0;
// size_t partial_frame_offset_bits_ = 0; // blah internal don't use this
// std::vector<uint8_t> partial_frame_buffer_;
uint32_t packets_skip_ = 0;
bool is_stream_done_ = false;
// bool split_frame_pending_ = false;
uint32_t split_frame_len_ = 0;
uint32_t split_frame_len_partial_ = 0;
uint8_t split_frame_padding_start_ = 0;
std::array<uint8_t, kBytesPerPacketData * 2> input_buffer_;
// first byte contains bit offset information
std::array<uint8_t, 1 + 4096> xma_frame_;
// uint8_t* current_frame_ = nullptr;
// conversion buffer for 2 channel frame
std::array<uint8_t, kBytesPerFrameChannel * 2> raw_frame_;
// std::vector<uint8_t> current_frame_ = std::vector<uint8_t>(0);
int32_t remaining_subframe_blocks_in_output_buffer_ = 0;
uint8_t current_frame_remaining_subframes_ = 0;
};
} // namespace apu

View File

@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Copyright 2023 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@ -20,29 +20,29 @@ namespace xma {
static const uint32_t kMaxFrameLength = 0x7FFF;
// Get number of frames that /begin/ in this packet.
uint32_t GetPacketFrameCount(uint8_t* packet) {
return (uint8_t)(packet[0] >> 2);
// Get number of frames that /begin/ in this packet. This is valid only for XMA2
// packets
const uint8_t GetPacketFrameCount(const uint8_t* packet) {
return packet[0] >> 2;
}
const uint8_t GetPacketMetadata(const uint8_t* packet) {
return packet[2] & 0x7;
}
const bool IsPacketXma2Type(const uint8_t* packet) {
return GetPacketMetadata(packet) == 1;
}
const uint8_t GetPacketSkipCount(const uint8_t* packet) { return packet[3]; }
// Get the first frame offset in bits
uint32_t GetPacketFrameOffset(uint8_t* packet) {
uint32_t GetPacketFrameOffset(const uint8_t* packet) {
uint32_t val = (uint16_t)(((packet[0] & 0x3) << 13) | (packet[1] << 5) |
(packet[2] >> 3));
// if (val > kBitsPerPacket - kBitsPerHeader) {
// // There is no data in this packet
// return -1;
// } else {
return val + 32;
// }
}
uint32_t GetPacketMetadata(uint8_t* packet) {
return (uint8_t)(packet[2] & 0x7);
}
uint32_t GetPacketSkipCount(uint8_t* packet) { return (uint8_t)(packet[3]); }
} // namespace xma
} // namespace apu
} // namespace xe