[APU] Rewrite XMA packet parser
Use new FFmpeg frame decoder
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||
* Copyright 2021 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
@@ -10,10 +10,11 @@
|
||||
#ifndef XENIA_APU_XMA_CONTEXT_H_
|
||||
#define XENIA_APU_XMA_CONTEXT_H_
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
//#include <vector>
|
||||
|
||||
#include "xenia/memory.h"
|
||||
#include "xenia/xbox.h"
|
||||
@@ -30,6 +31,7 @@
|
||||
|
||||
// Forward declarations
|
||||
struct AVCodec;
|
||||
struct AVCodecParserContext;
|
||||
struct AVCodecContext;
|
||||
struct AVFrame;
|
||||
struct AVPacket;
|
||||
@@ -121,29 +123,29 @@ struct XMA_CONTEXT_DATA {
|
||||
static_assert_size(XMA_CONTEXT_DATA, 64);
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct WmaProExtraData {
|
||||
uint16_t bits_per_sample;
|
||||
uint32_t channel_mask;
|
||||
uint8_t unk06[8];
|
||||
uint16_t decode_flags;
|
||||
uint8_t unk10[2];
|
||||
// XMA2WAVEFORMATEX
|
||||
struct Xma2ExtraData {
|
||||
uint8_t raw[34];
|
||||
};
|
||||
static_assert_size(WmaProExtraData, 18);
|
||||
static_assert_size(Xma2ExtraData, 34);
|
||||
#pragma pack(pop)
|
||||
|
||||
class XmaContext {
|
||||
public:
|
||||
static const uint32_t kBytesPerPacket = 2048;
|
||||
static const uint32_t kBitsPerPacket = kBytesPerPacket * 8;
|
||||
static const uint32_t kBitsPerHeader = 33;
|
||||
|
||||
static const uint32_t kBytesPerSample = 2;
|
||||
static const uint32_t kSamplesPerFrame = 512;
|
||||
static const uint32_t kSamplesPerSubframe = 128;
|
||||
static const uint32_t kBytesPerFrame = kSamplesPerFrame * kBytesPerSample;
|
||||
static const uint32_t kBytesPerSubframe =
|
||||
static const uint32_t kBytesPerFrameChannel =
|
||||
kSamplesPerFrame * kBytesPerSample;
|
||||
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;
|
||||
|
||||
explicit XmaContext();
|
||||
~XmaContext();
|
||||
@@ -168,28 +170,29 @@ class XmaContext {
|
||||
void set_is_enabled(bool is_enabled) { is_enabled_ = is_enabled; }
|
||||
|
||||
private:
|
||||
static void SwapInputBuffer(XMA_CONTEXT_DATA* data);
|
||||
static void NextPacket(XMA_CONTEXT_DATA* data);
|
||||
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);
|
||||
|
||||
// Convert sample format and swap bytes
|
||||
static bool ConvertFrame(const uint8_t** samples, int num_channels,
|
||||
int num_samples, uint8_t* output_buffer);
|
||||
|
||||
size_t SavePartial(uint8_t* packet, uint32_t frame_offset_bits,
|
||||
size_t frame_size_bits, bool append);
|
||||
bool ValidFrameOffset(uint8_t* block, size_t size_bytes,
|
||||
size_t frame_offset_bits);
|
||||
void DecodePackets(XMA_CONTEXT_DATA* data);
|
||||
uint32_t GetFramePacketNumber(uint8_t* block, size_t size, size_t bit_offset);
|
||||
int PrepareDecoder(uint8_t* block, size_t size, int sample_rate,
|
||||
int channels);
|
||||
|
||||
bool ConvertFrame(const uint8_t** samples, int num_channels, int num_samples,
|
||||
uint8_t* output_buffer);
|
||||
|
||||
int StartPacket(XMA_CONTEXT_DATA* data);
|
||||
|
||||
int PreparePacket(uint8_t* input, size_t seq_offset, size_t size,
|
||||
int sample_rate, int channels);
|
||||
void DiscardPacket();
|
||||
|
||||
int DecodePacket(uint8_t* output, size_t offset, size_t size,
|
||||
size_t* read_bytes);
|
||||
void Decode(XMA_CONTEXT_DATA* data);
|
||||
int PrepareDecoder(uint8_t* packet, int sample_rate, int channels);
|
||||
|
||||
Memory* memory_ = nullptr;
|
||||
|
||||
@@ -198,22 +201,34 @@ class XmaContext {
|
||||
std::mutex lock_;
|
||||
bool is_allocated_ = false;
|
||||
bool is_enabled_ = false;
|
||||
// bool is_dirty_ = true;
|
||||
|
||||
// libav structures
|
||||
AVCodec* codec_ = nullptr;
|
||||
AVCodecContext* context_ = nullptr;
|
||||
AVFrame* decoded_frame_ = nullptr;
|
||||
AVPacket* packet_ = nullptr;
|
||||
WmaProExtraData extra_data_;
|
||||
// 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_;
|
||||
// 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;
|
||||
|
||||
uint8_t* current_frame_ = nullptr;
|
||||
// bool split_frame_pending_ = false;
|
||||
uint32_t split_frame_len_ = 0;
|
||||
uint32_t split_frame_len_partial_ = 0;
|
||||
// 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);
|
||||
};
|
||||
|
||||
} // namespace apu
|
||||
|
||||
Reference in New Issue
Block a user