Repository navigation
Expand file tree
/
Copy pathacp.cpp
More file actions
249 lines (248 loc) · 9.33 KB
/
Copy pathacp.cpp
File metadata and controls
249 lines (248 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "acp_memory.h"
#include "xma_decoder.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <memory>
#include <thread>
#include <vector>
namespace rage::acp {
namespace {
UINT field(Context& c,UINT word,UINT shift,UINT bits) {
return (std::atomic_ref(c.words[word]).load()>>shift)&((1u<<bits)-1u);
}
void set_field(Context& c,UINT word,UINT shift,UINT bits,UINT value) {
const UINT mask = ((1u<<bits)-1u)<<shift;
std::atomic_ref cell(c.words[word]);
UINT old = cell.load();
while (!cell.compare_exchange_weak(old,(old&~mask)|((value<<shift)&mask))) {}
}
struct Voice {
XmaDecoder decoder;
std::array<int16_t,128> block{};
size_t filled{}, skip{};
bool draining{}, ended{};
};
bool send_next(AudioMemory& mem,Context& c,Voice& v) {
UINT current = field(c,4,31,1);
const UINT valid = field(c,0,20,2);
if (!valid) {
v.draining = true;
return v.decoder.finish();
}
UINT bit = field(c,2,0,26);
if (!(valid&(1u<<current))) {
current ^= 1;
set_field(c,4,31,1,current);
bit = 0;
}
constexpr UINT packet_bits = 2048*8;
const UINT offset = bit/packet_bits;
const UINT count = field(c,current,0,12);
const UINT base = c.words[5+current]&~2047u;
const UINT64 address = UINT64(base)+UINT64(offset)*2048;
if (offset>=count || address>UINT32_MAX) return false;
const auto* data = static_cast<BYTE*>(mem.resolve(static_cast<UINT>(address),2048));
if (!data || !v.decoder.send(data,bit%packet_bits)) return false;
if (offset+1==count) {
set_field(c,0,20+current,1,0);
set_field(c,2,0,26,0);
} else set_field(c,2,0,26,(offset+1)*packet_bits);
return true;
}
void end_voice(Context& c,Voice& v,UINT error) {
set_field(c,2,26,6,32u|error);
v.ended = true;
}
bool pump(AudioMemory& mem,Context& c,Voice& v) {
if (v.ended || !field(c,1,31,1)) return false;
const UINT count = field(c,0,22,5);
const UINT write = field(c,0,27,5);
const UINT64 addr = UINT64(c.words[7])+UINT64(write)*256;
auto* out = addr<=UINT32_MAX ? mem.resolve(static_cast<UINT>(addr),256) : nullptr;
if (!count || write>=count || !out) { end_voice(c,v,1); return false; }
// At most one decoder operation or one PCM ring block per pass. The worker
// releases both locks between passes so the mixer can free or advance a voice.
size_t n{};
const auto result = v.decoder.read(std::span(v.block).subspan(v.filled),n);
if (result==XmaDecoder::Read::input) {
if (v.draining || !send_next(mem,c,v)) end_voice(c,v,1);
return !v.ended;
}
if (result==XmaDecoder::Read::invalid) { end_voice(c,v,1); return false; }
if (result==XmaDecoder::Read::end && !v.filled) { end_voice(c,v,4); return false; }
const size_t skipped = (std::min)(v.skip,n);
if (skipped) {
v.skip -= skipped;
std::memmove(v.block.data()+v.filled,v.block.data()+v.filled+skipped,(n-skipped)*sizeof(int16_t));
}
v.filled += n-skipped;
if (v.filled<v.block.size() && result!=XmaDecoder::Read::end) return true;
std::fill(v.block.begin()+v.filled,v.block.end(),int16_t{});
std::memcpy(out,v.block.data(),256);
v.filled = 0;
const UINT next = (write+1)%count;
set_field(c,0,27,5,next);
if (next==field(c,9,0,5)%count) set_field(c,1,31,1,0);
if (result==XmaDecoder::Read::end) end_voice(c,v,4);
return !v.ended && field(c,1,31,1);
}
using Clock = std::chrono::steady_clock;
class Coprocessor final : public Device {
public:
HRESULT WINAPI Connect(UINT commands,UINT messages) override {
if (!commands || !messages) return E_INVALIDARG;
std::lock_guard lock(mutex_);
if (connected_) return E_UNEXPECTED;
try {
messages_.assign(messages,Message{});
if (!worker_.joinable()) worker_ = std::thread(&Coprocessor::run,this);
} catch (const std::bad_alloc&) { return E_OUTOFMEMORY; }
catch (const std::system_error&) { return E_FAIL; }
head_ = queued_ = mask_ = lost_ = 0;
connected_ = true;
start_ = Clock::now();
return S_OK;
}
HRESULT WINAPI Disconnect() override {
std::lock_guard lock(mutex_);
if (!connected_) return E_UNEXPECTED;
post(0x80,{});
connected_ = false;
voices_.clear();
generation_ = 0;
return S_OK;
}
HRESULT WINAPI Submit(UINT cmd,UINT64 id,UINT frame,const void* data,UINT notice) override {
if (!data) return E_INVALIDARG;
if (frame!=UINT_MAX && frame!=0) return E_NOTIMPL;
std::lock_guard lock(mutex_);
if (!connected_) return E_UNEXPECTED;
auto& mem = audio_memory();
std::lock_guard memory_lock(mem.mutex);
auto* signal = notice ? static_cast<UINT*>(mem.resolve(notice,sizeof(UINT))) : nullptr;
if (notice && (!signal || (notice&3u))) return E_INVALIDARG;
const auto* words = static_cast<const UINT*>(data);
const UINT value = words[0];
if (cmd==2) mask_ |= value;
else if (cmd==3) mask_ &= ~value;
else {
if (cmd!=5 && cmd!=7 && cmd!=18) return E_NOTIMPL;
if (!mem.contexts || value>=mem.count) return E_INVALIDARG;
if (generation_!=mem.generation) {
try {
voices_.clear();
voices_.resize(mem.count);
} catch (const std::bad_alloc&) { return E_OUTOFMEMORY; }
generation_ = mem.generation;
}
auto& c = mem.contexts[value];
auto& v = voices_[value];
if (cmd==5) {
if (field(c,0,12,8)) return E_NOTIMPL; // Loop endpoints are bit-addressed.
if (!field(c,0,22,5)) return E_INVALIDARG;
auto voice = std::unique_ptr<Voice>(new(std::nothrow) Voice);
if (!voice) return E_OUTOFMEMORY;
constexpr UINT rates[]{24000,32000,44100,48000};
const UINT channels = 1+field(c,1,29,1);
if (!voice->decoder.open(channels,rates[field(c,1,27,2)])) return E_FAIL;
voice->skip = size_t(field(c,1,24,3))*128*channels;
set_field(c,2,26,6,0);
v = std::move(voice);
} else if (cmd==7) v.reset();
else if (cmd==18) {
const UINT count = field(c,0,22,5);
if (!count) return E_INVALIDARG;
const UINT blocks = words[1];
set_field(c,9,0,5,(field(c,9,0,5)+blocks%count)%count);
if (blocks) set_field(c,1,31,1,1);
} else return E_NOTIMPL;
work_ = true;
cv_.notify_one();
}
if (id) {
std::array<BYTE,16> body{};
const UINT current_frame = static_cast<UINT>(elapsed()*375/1000000);
std::memcpy(body.data(),&cmd,4);
std::memcpy(body.data()+4,&id,8);
std::memcpy(body.data()+12,¤t_frame,4);
post(0x10,body);
}
if (signal) std::atomic_ref(*signal).store(1);
return S_OK;
}
bool WINAPI Pop(Message* out) override {
std::lock_guard lock(mutex_);
if (!out || !queued_) return false;
*out = messages_[head_];
head_ = (head_+1)%messages_.size();
--queued_;
return true;
}
UINT WINAPI Count() override {
std::lock_guard lock(mutex_);
return static_cast<UINT>(queued_);
}
void WINAPI Release() override {
{
std::lock_guard lock(mutex_);
stop_ = true;
}
cv_.notify_one();
if (worker_.joinable()) worker_.join();
delete this;
}
private:
UINT64 elapsed() const {
return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now()-start_).count();
}
void post(UINT kind,const std::array<BYTE,16>& body) {
if (!(kind&mask_)) return;
if (queued_==messages_.size()) {
if (lost_!=UINT_MAX) ++lost_;
return;
}
messages_[(head_+queued_)%messages_.size()] = {kind,lost_,static_cast<UINT>(elapsed()),body};
++queued_;
lost_ = 0;
}
void run() {
std::unique_lock lock(mutex_);
for (;;) {
cv_.wait(lock,[&]{return work_ || stop_;});
if (stop_) return;
work_ = false;
for (size_t i=0;i<voices_.size();++i) {
if (!voices_[i]) continue;
{
auto& mem = audio_memory();
std::lock_guard memory_lock(mem.mutex);
if (!mem.contexts || generation_!=mem.generation) { voices_.clear(); break; }
if (pump(mem,mem.contexts[i],*voices_[i])) work_ = true;
}
lock.unlock();
lock.lock();
if (stop_) return;
}
}
}
std::mutex mutex_;
std::condition_variable cv_;
std::thread worker_;
bool connected_{}, work_{}, stop_{};
UINT mask_{},lost_{};
size_t head_{},queued_{};
Clock::time_point start_;
std::vector<Message> messages_;
UINT64 generation_{};
std::vector<std::unique_ptr<Voice>> voices_;
};
}
}
HRESULT WINAPI AcpHalCreate(rage::acp::Device** out) {
if (!out) return E_POINTER;
*out = new(std::nothrow) rage::acp::Coprocessor;
return *out ? S_OK : E_OUTOFMEMORY;
}