diff options
Diffstat (limited to 'runtime/src/xma2_decoder.h')
| -rw-r--r-- | runtime/src/xma2_decoder.h | 116 |
1 files changed, 0 insertions, 116 deletions
diff --git a/runtime/src/xma2_decoder.h b/runtime/src/xma2_decoder.h deleted file mode 100644 index 3ee9fed17..000000000 --- a/runtime/src/xma2_decoder.h +++ /dev/null | |||
| @@ -1,116 +0,0 @@ | |||
| 1 | // Portable XMA2 packet parser and WMA Pro frame decoder (1–2 channels, 512-sample frames, 128–512 subframes). | ||
| 2 | // | ||
| 3 | // Algorithm and canonical Huffman / quantization tables follow FFmpeg's libavcodec/wmaprodec.c and wmaprodata.h | ||
| 4 | // (LGPL-2.1-or-later, used here under GPL-3.0-or-later), specialized for Xbox XMA2 streams (up to 2 channels, no LFE, | ||
| 5 | // 2048-byte packets with 32-bit MSB-first packet headers). | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include <array> | ||
| 9 | #include <cstddef> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <optional> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace leonida::xma2 { | ||
| 15 | |||
| 16 | inline constexpr std::size_t kPacketBytes = 2048; | ||
| 17 | inline constexpr std::size_t kPacketBits = kPacketBytes * 8; | ||
| 18 | inline constexpr std::size_t kHeaderBits = 32; | ||
| 19 | inline constexpr std::size_t kFrameSamples = 512; | ||
| 20 | inline constexpr std::size_t kSubframeSamples = 128; | ||
| 21 | |||
| 22 | struct PacketHeader { | ||
| 23 | std::uint32_t frames = 0; | ||
| 24 | std::uint32_t first_frame_bit = 0; | ||
| 25 | std::uint32_t skip = 0; | ||
| 26 | }; | ||
| 27 | |||
| 28 | struct FrameBits { | ||
| 29 | std::vector<std::uint8_t> data; | ||
| 30 | std::size_t bits = 0; | ||
| 31 | std::size_t start = 0; | ||
| 32 | std::optional<std::size_t> next; | ||
| 33 | }; | ||
| 34 | |||
| 35 | // Parses the 32-bit MSB-first header of packet `packet_index` inside `data[0..size)`. | ||
| 36 | std::optional<PacketHeader> parse_packet(const std::uint8_t* data, std::size_t size, std::size_t packet_index); | ||
| 37 | |||
| 38 | // Gathers one XMA2 frame starting at bit offset `start_bit` inside a buffer of 2048-byte packets, crossing packet | ||
| 39 | // headers and skipped interleaved stream packets, and computes the bit offset of the next frame if present. | ||
| 40 | std::optional<FrameBits> read_frame(const std::uint8_t* data, std::size_t size, std::size_t start_bit); | ||
| 41 | |||
| 42 | // Half-length inverse MDCT (negated DCT-IV scaled by `scale`) and sine window overlap-add helpers. | ||
| 43 | void imdct_half(const float* input, float* output, std::size_t n, float scale); | ||
| 44 | void window_overlap(float* block, const float* window, std::size_t block_len); | ||
| 45 | const std::vector<float>& sine_window(std::size_t n); | ||
| 46 | |||
| 47 | class Decoder { | ||
| 48 | public: | ||
| 49 | static constexpr std::size_t kBlockSizes = 3; | ||
| 50 | static constexpr std::size_t kMaxBands = 29; | ||
| 51 | static constexpr std::size_t kMaxSubframes = 32; | ||
| 52 | |||
| 53 | Decoder(std::size_t channels, std::uint32_t sample_rate); | ||
| 54 | |||
| 55 | std::size_t channels() const { return channels_; } | ||
| 56 | void reset(); | ||
| 57 | |||
| 58 | // Decodes one frame (`data`, `bits`) and appends kFrameSamples interleaved 16-bit PCM samples per channel to `out`. | ||
| 59 | // Returns false and optionally sets `*error` on a bitstream error (still emitting timing-preserving silence). | ||
| 60 | bool decode(const std::uint8_t* data, std::size_t bits, std::vector<std::int16_t>& out, | ||
| 61 | const char** error = nullptr); | ||
| 62 | |||
| 63 | // Emits the remaining half-frame overlap tail (kFrameSamples / 2 samples per channel) and resets state. | ||
| 64 | void flush(std::vector<std::int16_t>& out); | ||
| 65 | |||
| 66 | private: | ||
| 67 | struct Channel { | ||
| 68 | std::size_t prev_block_len = kFrameSamples; | ||
| 69 | bool transmit_coefs = false; | ||
| 70 | std::size_t num_subframes = 0; | ||
| 71 | std::array<std::size_t, kMaxSubframes> subframe_len{}; | ||
| 72 | std::size_t cur_subframe = 0; | ||
| 73 | std::size_t decoded_samples = 0; | ||
| 74 | std::int32_t quant_step = 0; | ||
| 75 | bool reuse_sf = false; | ||
| 76 | std::int32_t scale_factor_step = 1; | ||
| 77 | std::int32_t max_scale_factor = 0; | ||
| 78 | std::array<std::array<std::int32_t, kMaxBands>, 2> saved_scale_factors{}; | ||
| 79 | std::size_t scale_factor_idx = 0; | ||
| 80 | std::size_t scale_factors = 0; | ||
| 81 | std::size_t table_idx = 0; | ||
| 82 | std::size_t num_vec_coeffs = 0; | ||
| 83 | std::array<float, kFrameSamples> coeffs{}; | ||
| 84 | std::array<float, kFrameSamples + kFrameSamples / 2> out{}; | ||
| 85 | }; | ||
| 86 | |||
| 87 | struct Bits; | ||
| 88 | |||
| 89 | bool decode_frame(Bits& b, std::size_t len, const char*& err); | ||
| 90 | bool decode_tile_header(Bits& b, const char*& err); | ||
| 91 | bool decode_subframe_length(Bits& b, std::size_t offset, std::size_t& out_len, const char*& err) const; | ||
| 92 | bool decode_subframe(Bits& b, const char*& err); | ||
| 93 | bool decode_channel_transform(Bits& b, const char*& err); | ||
| 94 | bool decode_scale_factors(Bits& b, const char*& err); | ||
| 95 | bool decode_coefficients(Bits& b, std::size_t c, const char*& err); | ||
| 96 | void inverse_channel_transform(); | ||
| 97 | |||
| 98 | std::size_t channels_ = 1; | ||
| 99 | std::array<std::size_t, kBlockSizes> num_sfb_{}; | ||
| 100 | std::array<std::array<std::size_t, kMaxBands + 1>, kBlockSizes> sfb_offsets_{}; | ||
| 101 | std::array<std::array<std::array<std::size_t, kMaxBands>, kBlockSizes>, kBlockSizes> sf_offsets_{}; | ||
| 102 | std::array<Channel, 2> ch_{}; | ||
| 103 | |||
| 104 | std::size_t subframe_len_ = kFrameSamples; | ||
| 105 | std::size_t channels_for_cur_subframe_ = 0; | ||
| 106 | std::array<std::size_t, 2> channel_indexes_{0, 1}; | ||
| 107 | std::size_t num_bands_ = 0; | ||
| 108 | std::size_t table_idx_ = 0; | ||
| 109 | std::uint32_t esc_len_ = 0; | ||
| 110 | bool transmit_num_vec_coeffs_ = false; | ||
| 111 | bool transform_ = false; | ||
| 112 | std::array<bool, kMaxBands> transform_band_{}; | ||
| 113 | bool parsed_all_subframes_ = false; | ||
| 114 | }; | ||
| 115 | |||
| 116 | } // namespace leonida::xma2 | ||