sva-engine 0.7.5

Renders a resolved graph into per-node buffers a query can be asked of
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Concern: one stored node value, tagged with the store's format, and one staged run, as bytes and back | Non-concern: where the bytes live or when | IO: (Stored or Buffer) <-> bytes, a bad one None

use sva_formula::Codomain;
use sva_samples::{
    Buffer, Cost, Detail, Dropped, Extent, Grid, Label, PSYCHOACOUSTIC_V1, Rule, Source,
};

use sva_formula::Hash;

use super::Stored;
use super::stored::{Laid, Samples};

/// Bumped by, and only by, a change to a stored value's bytes.
pub const STORE_FORMAT: u32 = 7;

/// Every entry opens with its format, so one another format wrote is never read as a value,
/// even where a wipe left it.
fn entry_tag() -> Vec<u8> {
    [&b"SVAh"[..], &STORE_FORMAT.to_le_bytes()].concat()
}

const STAGED_RUN: &[u8; 4] = b"SVAc";

/// Samples per chunk of a run: what one checksum covers, and the least a read loads. Samples
/// stay f64, as the render computes them: a stored value is read by others, and f32 would
/// change the bits of every value that reads it.
pub(crate) const CHUNK: usize = 4096;

/// A header's length, then the header, then each run's samples chunk by chunk, every plane of
/// a chunk together, so a stretch of a run is one read.
pub(crate) fn entry(stored: &Stored, runs: &[Buffer]) -> Vec<u8> {
    let mut laid = Vec::new();
    let mut body = Vec::new();
    for run in runs {
        let mut sums = Vec::new();
        let at = body.len() as u64;
        for from in (0..run.len()).step_by(CHUNK) {
            let to = (from + CHUNK).min(run.len());
            let chunk = body.len();
            for plane in &run.planes {
                for sample in &plane[from..to] {
                    word(&mut body, sample.to_bits());
                }
            }
            sums.push(checksum(&body[chunk..]));
        }
        laid.push(Laid {
            rate: run.rate,
            start: run.start,
            width: run.width,
            len: run.len(),
            at,
            sums,
        });
    }
    let head = sealed(header(stored, &laid));
    let mut out = Vec::with_capacity(8 + head.len() + body.len());
    word(&mut out, head.len() as u64);
    out.extend_from_slice(&head);
    out.extend_from_slice(&body);
    out
}

fn header(stored: &Stored, laid: &[Laid]) -> Vec<u8> {
    let mut out = entry_tag();
    word(&mut out, stored.key.0);
    word(&mut out, stored.key.1);
    labelled(&mut out, &stored.label);
    out.push(stored.width);
    out.push(match stored.codomain {
        Codomain::Real => 0,
        Codomain::Complex => 1,
    });
    maybe(&mut out, stored.rate.map(u64::from));
    word(&mut out, u64::from(stored.grid.rate));
    out.extend_from_slice(&stored.grid.a.to_le_bytes());
    out.extend_from_slice(&stored.grid.d.to_le_bytes());
    word(&mut out, stored.support.start as u64);
    word(&mut out, stored.support.end as u64);
    out.extend_from_slice(&stored.priced.to_le_bytes());
    word(&mut out, stored.moved.to_bits());
    out.push(u8::from(stored.readable));
    match stored.samples {
        Samples::Of { key, by } => {
            out.push(1);
            word(&mut out, key.0);
            word(&mut out, key.1);
            word(&mut out, by as u64);
        }
        _ => out.push(0),
    }
    word(&mut out, laid.len() as u64);
    for run in laid {
        word(&mut out, u64::from(run.rate));
        word(&mut out, run.start as u64);
        word(&mut out, run.width as u64);
        word(&mut out, run.len as u64);
        for sum in &run.sums {
            word(&mut out, *sum);
        }
    }
    out
}

/// How many of an entry's first bytes its header takes, off its first eight.
pub(crate) fn head_len(first: &[u8]) -> Option<usize> {
    let len = u64::from_le_bytes(first.get(..8)?.try_into().ok()?);
    8usize.checked_add(usize::try_from(len).ok()?)
}

/// The meta an entry's header states, its samples laid in `file`, and the entry's length; a
/// truncated, corrupt or foreign header is `None`, never a partial value.
pub(crate) fn read_head(bytes: &[u8], file: Hash) -> Option<(Stored, u64)> {
    let span = head_len(bytes)?;
    let mut r = Reader(opened(bytes.get(8..span)?, &entry_tag())?);
    let key = Hash(r.word()?, r.word()?);
    let label = r.label()?;
    let width = r.byte()?;
    let codomain = match r.byte()? {
        0 => Codomain::Real,
        1 => Codomain::Complex,
        _ => return None,
    };
    let rate = match r.maybe()? {
        Some(rate) => Some(u32::try_from(rate).ok()?),
        None => None,
    };
    let grid = Grid {
        rate: u32::try_from(r.word()?).ok()?,
        a: r.wide()? as i128,
        d: r.wide()? as i128,
    };
    let (start, end) = (r.word()? as i64, r.word()? as i64);
    let support = (start <= end).then(|| Extent::new(start, end))?;
    let priced = r.wide()?;
    let moved = f64::from_bits(r.word()?);
    let readable = match r.byte()? {
        0 => false,
        1 => true,
        _ => return None,
    };
    let of = match r.byte()? {
        0 => None,
        1 => Some(Samples::Of {
            key: Hash(r.word()?, r.word()?),
            by: r.word()? as i64,
        }),
        _ => return None,
    };
    let count = r.word()? as usize;
    let mut runs = Vec::new();
    let mut at = span as u64;
    for _ in 0..count.min(r.0.len()) {
        let rate = u32::try_from(r.word()?).ok()?;
        let start = r.word()? as i64;
        let (width, len) = (r.word()? as usize, r.word()? as usize);
        let sums = (0..len.div_ceil(CHUNK).min(r.0.len()))
            .map(|_| r.word())
            .collect::<Option<Vec<u64>>>()?;
        if sums.len() != len.div_ceil(CHUNK) {
            return None;
        }
        runs.push(Laid {
            rate,
            start,
            width,
            len,
            at,
            sums,
        });
        at = at.checked_add((width.checked_mul(len)?.checked_mul(8)?) as u64)?;
    }
    let samples = match (of, runs.is_empty()) {
        (Some(of), true) => of,
        (Some(_), false) => return None,
        (None, true) => Samples::None,
        (None, false) => Samples::Entry {
            file,
            runs,
            shift: 0,
        },
    };
    let stored = Stored {
        key,
        label,
        width,
        codomain,
        rate,
        grid,
        support,
        priced,
        moved,
        readable,
        samples,
    };
    (runs_whole(count, &stored.samples) && r.0.is_empty()).then_some((stored, at))
}

fn runs_whole(count: usize, samples: &Samples) -> bool {
    match samples {
        Samples::Entry { runs, .. } => runs.len() == count,
        _ => count == 0,
    }
}

/// Where in its file a run's chunks `from..to` lie, and how many bytes.
pub(crate) fn span_of(run: &Laid, from: usize, to: usize) -> (u64, u64) {
    let bytes = |chunks: usize| ((chunks * CHUNK).min(run.len) * run.width * 8) as u64;
    (run.at + bytes(from), bytes(to) - bytes(from))
}

/// A run's chunks `from..to` off the bytes `span_of` names; `None` where a checksum fails.
pub(crate) fn read_chunks(bytes: &[u8], run: &Laid, from: usize, to: usize) -> Option<Buffer> {
    let first = from * CHUNK;
    let len = (to * CHUNK).min(run.len).checked_sub(first)?;
    if bytes.len() != len * run.width * 8 {
        return None;
    }
    let mut planes = vec![Vec::with_capacity(len); run.width];
    let mut at = 0;
    for (k, sum) in run.sums[from..to].iter().enumerate() {
        let n = (CHUNK).min(run.len - first - k * CHUNK);
        let chunk = &bytes[at..at + n * run.width * 8];
        if checksum(chunk) != *sum {
            return None;
        }
        for (c, plane) in planes.iter_mut().enumerate() {
            let own = &chunk[c * n * 8..(c + 1) * n * 8];
            plane.extend(
                own.chunks_exact(8).map(|b| {
                    f64::from_bits(u64::from_le_bytes(b.try_into().expect("eight bytes")))
                }),
            );
        }
        at += chunk.len();
    }
    let mut out = Buffer::of_planes(run.rate, planes);
    out.start = run.start + first as i64;
    Some(out)
}

pub(crate) fn chunk(samples: &Buffer) -> Vec<u8> {
    let mut out = STAGED_RUN.to_vec();
    segments(&mut out, std::slice::from_ref(samples));
    sealed(out)
}

pub(crate) fn read_chunk(bytes: &[u8]) -> Option<Buffer> {
    let mut r = Reader(opened(bytes, STAGED_RUN)?);
    let mut parts = r.segments()?;
    (r.0.is_empty() && parts.len() == 1).then(|| parts.remove(0))
}

fn sealed(mut out: Vec<u8>) -> Vec<u8> {
    let sum = checksum(&out);
    word(&mut out, sum);
    out
}

/// The body past `magic`, where the checksum holds.
fn opened<'b>(bytes: &'b [u8], magic: &[u8]) -> Option<&'b [u8]> {
    let body = bytes.len().checked_sub(8)?;
    let (body, sum) = bytes.split_at(body);
    (checksum(body).to_le_bytes() == sum && body.starts_with(magic)).then(|| &body[magic.len()..])
}

fn segments(out: &mut Vec<u8>, parts: &[Buffer]) {
    word(out, parts.len() as u64);
    for part in parts {
        word(out, u64::from(part.rate));
        word(out, part.start as u64);
        word(out, part.width as u64);
        word(out, part.len() as u64);
        for plane in &part.planes {
            for sample in plane {
                word(out, sample.to_bits());
            }
        }
    }
}

fn checksum(bytes: &[u8]) -> u64 {
    bytes.iter().fold(0xcbf2_9ce4_8422_2325, |h, b| {
        (h ^ u64::from(*b)).wrapping_mul(0x0100_0000_01b3)
    })
}

fn word(out: &mut Vec<u8>, w: u64) {
    out.extend_from_slice(&w.to_le_bytes());
}

fn float(out: &mut Vec<u8>, v: Option<f64>) {
    maybe(out, v.map(f64::to_bits));
}

fn maybe(out: &mut Vec<u8>, w: Option<u64>) {
    match w {
        None => out.push(0),
        Some(w) => {
            out.push(1);
            word(out, w);
        }
    }
}

fn text(out: &mut Vec<u8>, s: &str) {
    word(out, s.len() as u64);
    out.extend_from_slice(s.as_bytes());
}

fn labelled(out: &mut Vec<u8>, label: &Label) {
    out.push(match label.source {
        Source::Exact => 0,
        Source::Measured => 1,
    });
    text(out, label.profile);
    word(out, u64::from(label.rate));
    detailed(out, &label.detail);
    match label.cost {
        None => out.push(0),
        Some(Cost { flops, budget }) => {
            out.push(1);
            out.extend_from_slice(&flops.to_le_bytes());
            out.extend_from_slice(&budget.to_le_bytes());
        }
    }
    float(out, label.moved);
    match &label.pruned {
        None => out.push(0),
        Some(pruned) => {
            out.push(1);
            word(out, pruned.db.to_bits());
            word(out, pruned.cuts.len() as u64);
            for (node, at) in &pruned.cuts {
                text(out, node);
                word(out, *at as u64);
            }
        }
    }
}

fn detailed(out: &mut Vec<u8>, detail: &Detail) {
    let rule = |out: &mut Vec<u8>, rule: &Rule| text(out, rule.as_str());
    match detail {
        Detail::Lines {
            rule: r,
            placed,
            summed,
            dropped,
            dropped_more,
            terms,
            tail_db,
        } => {
            out.push(0);
            rule(out, r);
            word(out, *placed as u64);
            word(out, *summed as u64);
            word(out, dropped.len() as u64);
            for d in dropped {
                word(out, d.hz.to_bits());
                word(out, d.db.to_bits());
            }
            word(out, *dropped_more as u64);
            maybe(out, terms.map(|t| t as u64));
            float(out, *tail_db);
        }
        Detail::Continuous { rule: r } => {
            out.push(1);
            rule(out, r);
        }
        Detail::Cropped { rule: r, tail_db } => {
            out.push(2);
            rule(out, r);
            float(out, *tail_db);
        }
        Detail::Point { rule: r, alias_db } => {
            out.push(3);
            rule(out, r);
            float(out, *alias_db);
        }
        Detail::Spectrum { rule: r, wrap_db } => {
            out.push(4);
            rule(out, r);
            word(out, wrap_db.to_bits());
        }
        Detail::Roundtrip { rule: r, edited } => {
            out.push(5);
            rule(out, r);
            out.push(u8::from(*edited));
        }
        Detail::Reading { rule: r } => {
            out.push(6);
            rule(out, r);
        }
        Detail::Added { parts } => {
            out.push(7);
            word(out, parts.len() as u64);
            for part in parts {
                detailed(out, part);
            }
        }
    }
}

struct Reader<'b>(&'b [u8]);

impl Reader<'_> {
    fn take(&mut self, n: usize) -> Option<&[u8]> {
        if self.0.len() < n {
            return None;
        }
        let (head, rest) = self.0.split_at(n);
        self.0 = rest;
        Some(head)
    }

    fn byte(&mut self) -> Option<u8> {
        self.take(1).map(|b| b[0])
    }

    fn word(&mut self) -> Option<u64> {
        Some(u64::from_le_bytes(self.take(8)?.try_into().ok()?))
    }

    fn segments(&mut self) -> Option<Vec<Buffer>> {
        let count = self.word()? as usize;
        let mut parts = Vec::new();
        for _ in 0..count.min(self.0.len()) {
            let rate = u32::try_from(self.word()?).ok()?;
            let start = self.word()? as i64;
            let (width, len) = (self.word()? as usize, self.word()? as usize);
            if width.saturating_mul(len).saturating_mul(8) > self.0.len() {
                return None;
            }
            let planes = (0..width)
                .map(|_| (0..len).map(|_| self.word().map(f64::from_bits)).collect())
                .collect::<Option<Vec<Vec<f64>>>>()?;
            let mut part = Buffer::of_planes(rate, planes);
            part.start = start;
            parts.push(part);
        }
        (parts.len() == count).then_some(parts)
    }

    fn wide(&mut self) -> Option<u128> {
        Some(u128::from_le_bytes(self.take(16)?.try_into().ok()?))
    }

    fn maybe(&mut self) -> Option<Option<u64>> {
        match self.byte()? {
            0 => Some(None),
            1 => Some(Some(self.word()?)),
            _ => None,
        }
    }

    fn float(&mut self) -> Option<Option<f64>> {
        Some(self.maybe()?.map(f64::from_bits))
    }

    fn text(&mut self) -> Option<&str> {
        let len = self.word()? as usize;
        std::str::from_utf8(self.take(len)?).ok()
    }

    fn rule(&mut self) -> Option<Rule> {
        Rule::named(self.text()?)
    }

    fn label(&mut self) -> Option<Label> {
        let source = match self.byte()? {
            0 => Source::Exact,
            1 => Source::Measured,
            _ => return None,
        };
        let profile = match self.text()? {
            name if name == PSYCHOACOUSTIC_V1.name => PSYCHOACOUSTIC_V1.name,
            _ => return None,
        };
        let rate = u32::try_from(self.word()?).ok()?;
        let detail = self.detail()?;
        let cost = match self.byte()? {
            0 => None,
            1 => Some(Cost {
                flops: self.wide()?,
                budget: self.wide()?,
            }),
            _ => return None,
        };
        let moved = self.float()?;
        let pruned = match self.byte()? {
            0 => None,
            1 => {
                let db = f64::from_bits(self.word()?);
                let count = usize::try_from(self.word()?).ok()?;
                let cuts = (0..count)
                    .map(|_| Some((self.text()?.to_string(), self.word()? as i64)))
                    .collect::<Option<Vec<_>>>()?;
                Some(sva_samples::Pruned { db, cuts })
            }
            _ => return None,
        };
        Some(Label {
            source,
            profile,
            rate,
            detail,
            cost,
            moved,
            pruned,
        })
    }

    fn detail(&mut self) -> Option<Detail> {
        Some(match self.byte()? {
            0 => {
                let rule = self.rule()?;
                let (placed, summed) = (self.word()? as usize, self.word()? as usize);
                let count = self.word()? as usize;
                let dropped = (0..count.min(self.0.len()))
                    .map(|_| {
                        Some(Dropped {
                            hz: f64::from_bits(self.word()?),
                            db: f64::from_bits(self.word()?),
                        })
                    })
                    .collect::<Option<Vec<_>>>()?;
                if dropped.len() != count {
                    return None;
                }
                Detail::Lines {
                    rule,
                    placed,
                    summed,
                    dropped,
                    dropped_more: self.word()? as usize,
                    terms: self.maybe()?.map(|t| t as usize),
                    tail_db: self.float()?,
                }
            }
            1 => Detail::Continuous { rule: self.rule()? },
            2 => Detail::Cropped {
                rule: self.rule()?,
                tail_db: self.float()?,
            },
            3 => Detail::Point {
                rule: self.rule()?,
                alias_db: self.float()?,
            },
            4 => Detail::Spectrum {
                rule: self.rule()?,
                wrap_db: f64::from_bits(self.word()?),
            },
            5 => Detail::Roundtrip {
                rule: self.rule()?,
                edited: match self.byte()? {
                    0 => false,
                    1 => true,
                    _ => return None,
                },
            },
            6 => Detail::Reading { rule: self.rule()? },
            7 => {
                let count = self.word()? as usize;
                let parts = (0..count.min(self.0.len()))
                    .map(|_| self.detail())
                    .collect::<Option<Vec<_>>>()?;
                if parts.len() != count {
                    return None;
                }
                Detail::Added { parts }
            }
            _ => return None,
        })
    }
}