upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/xma_decoder/tests/ffi_cpp_smoke.cpp')
-rw-r--r--runtime/xma_decoder/tests/ffi_cpp_smoke.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp b/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp
new file mode 100644
index 000000000..cb41c81f5
--- /dev/null
+++ b/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp
@@ -0,0 +1,87 @@
1#include "../leonida_xma2_decoder.h"
2
3#include <algorithm>
4#include <array>
5#include <cstdint>
6#include <cstdio>
7#include <cstdlib>
8
9void check(bool condition, const char* expression, int line) {
10 if (!condition) {
11 std::fprintf(stderr, "ffi_cpp_smoke.cpp:%d: %s\n", line, expression);
12 std::exit(EXIT_FAILURE);
13 }
14}
15
16#define CHECK(condition) check((condition), #condition, __LINE__)
17
18template <size_t Size>
19void put_bits(std::array<uint8_t, Size>& bytes, size_t& position, uint32_t width,
20 uint32_t value) {
21 for (uint32_t bit = width; bit > 0; --bit, ++position) {
22 if (((value >> (bit - 1)) & 1u) != 0) {
23 bytes[position / 8] |= static_cast<uint8_t>(1u << (7 - (position % 8)));
24 }
25 }
26}
27
28std::array<uint8_t, LEONIDA_XMA2_PACKET_BYTES> make_silent_stereo_packet() {
29 std::array<uint8_t, LEONIDA_XMA2_PACKET_BYTES> packet{};
30 constexpr uint32_t frame_bits = 36;
31 size_t position = 0;
32 put_bits(packet, position, 6, 1);
33 put_bits(packet, position, 15, frame_bits);
34 put_bits(packet, position, 3, 1);
35 put_bits(packet, position, 8, 0);
36 put_bits(packet, position, 15, frame_bits);
37 put_bits(packet, position, 1, 1); // Fixed-size tile header.
38 put_bits(packet, position, 1, 0); // One full-length subframe.
39 put_bits(packet, position, 1, 0); // No optional stereo decorrelation matrix.
40 put_bits(packet, position, 8, 0); // No dynamic range adjustment.
41 put_bits(packet, position, 1, 0); // No trim metadata.
42 put_bits(packet, position, 1, 0); // No fill data.
43 put_bits(packet, position, 1, 0); // No corrupt-subframe marker.
44 put_bits(packet, position, 1, 0); // No stereo transform error.
45 put_bits(packet, position, 1, 1); // Two-channel group without decorrelation.
46 put_bits(packet, position, 1, 0);
47 put_bits(packet, position, 2, 0); // No transmitted coefficients: silence.
48 put_bits(packet, position, 1, 0); // Frame alignment bit.
49 put_bits(packet, position, 1, 0); // No additional frame in this packet.
50 CHECK(position == 32 + frame_bits);
51 return packet;
52}
53
54int main() {
55 LeonidaXma2Decoder* decoder = nullptr;
56 CHECK(leonida_xma2_decoder_create(48000, 2, &decoder) == LEONIDA_XMA2_OK);
57 CHECK(decoder != nullptr);
58
59 const auto packet = make_silent_stereo_packet();
60 std::array<uint8_t, LEONIDA_XMA2_PACKET_BYTES * 3> input{};
61 std::copy(packet.begin(), packet.end(), input.begin() + LEONIDA_XMA2_PACKET_BYTES);
62 std::copy(packet.begin(), packet.end(), input.begin() + LEONIDA_XMA2_PACKET_BYTES * 2);
63 LeonidaXma2PcmBlock* pcm = nullptr;
64 CHECK(leonida_xma2_decode_buffer(decoder, input.data(), input.size(), &pcm) ==
65 LEONIDA_XMA2_OK);
66 CHECK(pcm != nullptr);
67 const size_t samples = leonida_xma2_pcm_sample_count(pcm);
68 CHECK(samples > 0);
69 CHECK(samples % 2 == 0);
70 const int16_t* data = leonida_xma2_pcm_data(pcm);
71 CHECK(data != nullptr);
72
73 LeonidaXma2PcmBlock* invalid = reinterpret_cast<LeonidaXma2PcmBlock*>(1);
74 CHECK(leonida_xma2_decode_buffer(decoder, input.data(), input.size() - 1, &invalid) ==
75 LEONIDA_XMA2_BAD_PACKET_SIZE);
76 CHECK(invalid == nullptr);
77
78 leonida_xma2_decoder_destroy(decoder);
79 CHECK(leonida_xma2_pcm_sample_count(pcm) == samples);
80 CHECK(leonida_xma2_pcm_data(pcm) == data);
81 for (size_t i = 0; i < samples; ++i) {
82 CHECK(data[i] == 0);
83 }
84 leonida_xma2_pcm_destroy(pcm);
85 leonida_xma2_decoder_destroy(nullptr);
86 return 0;
87}