rusty_esp_dsp 0.1.2

ESP-DSP remade for the Janus family: the scalar kernel home (pixel conversions and downscales, PCM reductions and sample-format conversion, the H.264 block costs, the work counters and ceiling probe) where every kernel is the oracle for a PIE twin. no_std, forbid(unsafe), no allocator, no drivers.
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
//! Sample-format conversion with fixed, documented rules — the ones ffmpeg's
//! `swresample` uses, so the oracle test in `rusty_esp_audio-esp` can demand
//! byte identity.
//!
//! | from → to | rule |
//! |---|---|
//! | I16 → I32 / I24In32 | `x << 16` |
//! | I32 / I24In32 → I16 | `x >> 16` (truncate toward −∞) |
//! | I32 → I24In32 | clear the low 8 bits |
//! | I24In32 → I32 | bit copy |
//! | I16 → F32 | `x / 32768` |
//! | I32 / I24In32 → F32 | `x / 2^31` |
//! | F32 → I16 | `round_ties_even(x · 32768)` clamped to the i16 range |
//! | F32 → I32 | `round_ties_even(x · 2^31)` clamped (in f64) to the i32 range |
//! | F32 → I24In32 | as I32, then clear the low 8 bits |
//!
//! Moved verbatim from `rusty_esp_audio-core::codec::pcm` (D0, 2026-09-02).

use rusty_esp_core::error::{Error, Result};
use rusty_esp_core::pcm::{PcmBlock, SampleFormat, as_f32, as_f32_mut, as_i16, as_i16_mut};

/// Bytes `convert` writes for `input_bytes` of `from` samples going to `to`.
#[must_use]
pub const fn output_bytes(from: SampleFormat, to: SampleFormat, input_bytes: usize) -> usize {
    input_bytes / from.bytes() * to.bytes()
}

// `ROUND_F32` moved to `rusty_esp_core::pcm::rint_sat_i16` along with the
// only function that used it. `ROUND_F64` stays: `f32_to_i32` below still
// rounds in f64 here, and has not been given the same treatment because the
// probe does not measure an i32 pair -- an unmeasured change is not a change
// worth making.
/// `1.5 · 2^52`, the same trick in `f64` for magnitudes under `2^51`.
const ROUND_F64: f64 = 6_755_399_441_055_744.0;

#[inline]
fn f32_to_i16(x: f32) -> i16 {
    // One definition, in the crate every consumer already speaks. The range
    // test there earns an unchecked final conversion, where the `as i16` this
    // used to end on was a SATURATING cast still emitting its own range and
    // NaN tests after these had already been done. Same bits for every input;
    // `rounding_matches_libm_exhaustively` below is unchanged and still
    // sweeps the whole f32 space.
    rusty_esp_core::pcm::rint_sat_i16(x * 32768.0)
}

#[inline]
fn f32_to_i32(x: f32) -> i32 {
    let v = f64::from(x) * 2_147_483_648.0;
    if v >= 2_147_483_647.0 {
        i32::MAX
    } else if v <= -2_147_483_648.0 {
        i32::MIN
    } else if v.is_nan() {
        0
    } else {
        ((v + ROUND_F64) - ROUND_F64) as i32
    }
}

/// Convert one sample given as its little-endian bytes.
#[inline]
fn convert_sample(from: SampleFormat, to: SampleFormat, i: &[u8], o: &mut [u8]) {
    use SampleFormat::{F32, I16, I24In32, I32};

    match (from, to) {
        (I16, I32) | (I16, I24In32) => {
            let v = i32::from(i16::from_le_bytes([i[0], i[1]])) << 16;
            o.copy_from_slice(&v.to_le_bytes());
        }
        (I32, I16) | (I24In32, I16) => {
            let v = i32::from_le_bytes([i[0], i[1], i[2], i[3]]) >> 16;
            o.copy_from_slice(&(v as i16).to_le_bytes());
        }
        (I32, I24In32) => {
            o.copy_from_slice(&[0, i[1], i[2], i[3]]);
        }
        (I24In32, I32) => {
            o.copy_from_slice(i);
        }
        // Dividing by a power of two is multiplying by its exact reciprocal,
        // bit for bit (no subnormal can arise from an integer this size), and
        // a multiply is what a chip's FPU has where a divide is a routine.
        (I16, F32) => {
            let v = f32::from(i16::from_le_bytes([i[0], i[1]])) * (1.0 / 32768.0);
            o.copy_from_slice(&v.to_le_bytes());
        }
        (I32, F32) | (I24In32, F32) => {
            let v = (i32::from_le_bytes([i[0], i[1], i[2], i[3]]) as f32) * (1.0 / 2_147_483_648.0);
            o.copy_from_slice(&v.to_le_bytes());
        }
        (F32, I16) => {
            let v = f32_to_i16(f32::from_le_bytes([i[0], i[1], i[2], i[3]]));
            o.copy_from_slice(&v.to_le_bytes());
        }
        (F32, I32) => {
            let v = f32_to_i32(f32::from_le_bytes([i[0], i[1], i[2], i[3]]));
            o.copy_from_slice(&v.to_le_bytes());
        }
        (F32, I24In32) => {
            let v = f32_to_i32(f32::from_le_bytes([i[0], i[1], i[2], i[3]])) & !0xFF;
            o.copy_from_slice(&v.to_le_bytes());
        }
        // Same format: a copy. `from`/`to` are `#[non_exhaustive]`, so a new
        // variant arriving in core lands here too and stays a copy only when
        // the sizes agree (checked by the caller).
        _ => o.copy_from_slice(i),
    }
}

/// Re-encode every sample of `input` as `to` into `out`; returns bytes
/// written. Same-format input is a byte copy.
pub fn convert(input: PcmBlock<'_>, to: SampleFormat, out: &mut [u8]) -> Result<usize> {
    let from = input.format.sample;
    let n = output_bytes(from, to, input.data.len());
    if out.len() < n {
        return Err(Error::BufferTooSmall { needed: n });
    }
    if from == to {
        out[..n].copy_from_slice(input.data);
        return Ok(n);
    }
    if from.bytes() != to.bytes() && !is_known_pair(from, to) {
        return Err(Error::Unsupported);
    }
    let (fb, tb) = (from.bytes(), to.bytes());
    // Resolve the format pair ONCE and run a loop that knows it, instead of
    // re-matching `(from, to)` for every sample. The per-sample arithmetic is
    // unchanged -- `convert_sample` stays as the single definition of each
    // pair and the oracle for this fast path -- only the dispatch moves out
    // of the loop.
    macro_rules! each {
        (|$i:ident, $o:ident| $body:block) => {{
            for ($i, $o) in input
                .data
                .chunks_exact(fb)
                .zip(out[..n].chunks_exact_mut(tb))
            {
                $body
            }
        }};
    }
    use SampleFormat::{F32, I16, I24In32, I32};

    // FAST ARMS for the two pairs a voice path actually runs, taken when both
    // buffers view as samples. The table below stays the oracle and takes
    // every misaligned or big-endian case.
    //
    // These pairs pay the byte marshalling TWICE: an i16 out of a `&[u8]` is
    // two byte loads plus a shift and an or, and an f32 is FOUR byte loads
    // plus three shifts and three ors. Settling both alignments once per call
    // leaves one load and one store per sample.
    match (from, to) {
        (I16, F32) => {
            if let (Some(si), Some(so)) = (as_i16(input.data), as_f32_mut(&mut out[..n])) {
                for (i, o) in si.iter().zip(so.iter_mut()) {
                    *o = f32::from(*i) * (1.0 / 32768.0);
                }
                return Ok(n);
            }
        }
        (F32, I16) => {
            if let (Some(si), Some(so)) = (as_f32(input.data), as_i16_mut(&mut out[..n])) {
                for (i, o) in si.iter().zip(so.iter_mut()) {
                    *o = f32_to_i16(*i);
                }
                return Ok(n);
            }
        }
        _ => {}
    }

    match (from, to) {
        (I16, I32) | (I16, I24In32) => each!(|i, o| {
            let v = i32::from(i16::from_le_bytes([i[0], i[1]])) << 16;
            o.copy_from_slice(&v.to_le_bytes());
        }),
        (I32, I16) | (I24In32, I16) => each!(|i, o| {
            let v = i32::from_le_bytes([i[0], i[1], i[2], i[3]]) >> 16;
            o.copy_from_slice(&(v as i16).to_le_bytes());
        }),
        (I16, F32) => each!(|i, o| {
            let v = f32::from(i16::from_le_bytes([i[0], i[1]])) * (1.0 / 32768.0);
            o.copy_from_slice(&v.to_le_bytes());
        }),
        (I32, F32) | (I24In32, F32) => each!(|i, o| {
            let v = (i32::from_le_bytes([i[0], i[1], i[2], i[3]]) as f32) * (1.0 / 2_147_483_648.0);
            o.copy_from_slice(&v.to_le_bytes());
        }),
        (F32, I16) => each!(|i, o| {
            let v = f32_to_i16(f32::from_le_bytes([i[0], i[1], i[2], i[3]]));
            o.copy_from_slice(&v.to_le_bytes());
        }),
        (F32, I32) => each!(|i, o| {
            let v = f32_to_i32(f32::from_le_bytes([i[0], i[1], i[2], i[3]]));
            o.copy_from_slice(&v.to_le_bytes());
        }),
        // The rarer pairs keep the shared per-sample definition.
        _ => each!(|i, o| {
            convert_sample(from, to, i, o);
        }),
    }
    Ok(n)
}

/// Every pair the table above lists.
const fn is_known_pair(from: SampleFormat, to: SampleFormat) -> bool {
    use SampleFormat::{F32, I16, I24In32, I32};
    matches!(
        (from, to),
        (I16, I32)
            | (I16, I24In32)
            | (I16, F32)
            | (I32, I16)
            | (I32, I24In32)
            | (I32, F32)
            | (I24In32, I16)
            | (I24In32, I32)
            | (I24In32, F32)
            | (F32, I16)
            | (F32, I32)
            | (F32, I24In32)
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use rusty_esp_core::pcm::PcmFormat;
    use rusty_esp_core::time::Micros;

    fn block<'a>(sample: SampleFormat, data: &'a [u8]) -> PcmBlock<'a> {
        PcmBlock::new(
            PcmFormat::new(16_000, 1, sample).unwrap(),
            Micros::ZERO,
            data,
        )
        .unwrap()
    }

    #[test]
    fn integer_widths() {
        let mut out = [0u8; 8];
        let i16s = [0x01, 0x00, 0x00, 0x80]; // 1, -32768
        assert_eq!(
            convert(block(SampleFormat::I16, &i16s), SampleFormat::I32, &mut out).unwrap(),
            8
        );
        assert_eq!(out, [0, 0, 1, 0, 0, 0, 0, 0x80]);
        let mut back = [0u8; 4];
        convert(block(SampleFormat::I32, &out), SampleFormat::I16, &mut back).unwrap();
        assert_eq!(back, i16s);
        // Truncation: 0x0000_FFFF >> 16 = 0; 0xFFFF_0001 (negative) >> 16 = -1.
        let odd = [0xFF, 0xFF, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF];
        convert(block(SampleFormat::I32, &odd), SampleFormat::I16, &mut back).unwrap();
        assert_eq!(back, [0, 0, 0xFF, 0xFF]);
        // I32 → I24In32 clears the low byte; I24In32 → I32 is a copy.
        let mut o24 = [0u8; 8];
        convert(
            block(SampleFormat::I32, &odd),
            SampleFormat::I24In32,
            &mut o24,
        )
        .unwrap();
        assert_eq!(o24, [0, 0xFF, 0, 0, 0, 0, 0xFF, 0xFF]);
        let mut o32 = [0u8; 8];
        convert(
            block(SampleFormat::I24In32, &o24),
            SampleFormat::I32,
            &mut o32,
        )
        .unwrap();
        assert_eq!(o32, o24);
    }

    #[test]
    fn float_rounds_ties_even_and_clamps() {
        let f = [1.0f32, -1.0, 0.5, 1.5 / 32768.0, 2.5 / 32768.0, -0.00001];
        let mut bytes = [0u8; 24];
        for (b, v) in bytes.chunks_exact_mut(4).zip(f) {
            b.copy_from_slice(&v.to_le_bytes());
        }
        let mut out = [0u8; 12];
        convert(
            block(SampleFormat::F32, &bytes),
            SampleFormat::I16,
            &mut out,
        )
        .unwrap();
        let v: [i16; 6] =
            core::array::from_fn(|i| i16::from_le_bytes([out[i * 2], out[i * 2 + 1]]));
        assert_eq!(v, [32767, -32768, 16384, 2, 2, 0]);
        // And back: exact for values that came from integers.
        let mut f_out = [0u8; 24];
        convert(
            block(SampleFormat::I16, &out),
            SampleFormat::F32,
            &mut f_out,
        )
        .unwrap();
        let back = f32::from_le_bytes([f_out[8], f_out[9], f_out[10], f_out[11]]);
        assert_eq!(back, 0.5);
        // F32 → I32 clamps at both ends; 0.5 is exactly 2^30.
        let mut o32 = [0u8; 24];
        convert(
            block(SampleFormat::F32, &bytes),
            SampleFormat::I32,
            &mut o32,
        )
        .unwrap();
        assert_eq!(
            i32::from_le_bytes([o32[0], o32[1], o32[2], o32[3]]),
            i32::MAX
        );
        assert_eq!(
            i32::from_le_bytes([o32[4], o32[5], o32[6], o32[7]]),
            i32::MIN
        );
        assert_eq!(
            i32::from_le_bytes([o32[8], o32[9], o32[10], o32[11]]),
            1 << 30
        );
    }

    /// The libm twins the rounding replaced, kept here as the oracle.
    fn libm_f32_to_i16(x: f32) -> i16 {
        let v = libm::rintf(x * 32768.0);
        if v >= 32767.0 {
            i16::MAX
        } else if v <= -32768.0 {
            i16::MIN
        } else {
            v as i16
        }
    }

    fn libm_f32_to_i32(x: f32) -> i32 {
        let v = libm::rint(f64::from(x) * 2_147_483_648.0);
        if v >= 2_147_483_647.0 {
            i32::MAX
        } else if v <= -2_147_483_648.0 {
            i32::MIN
        } else {
            v as i32
        }
    }

    const EDGES: [f32; 22] = [
        0.0,
        -0.0,
        f32::NAN,
        f32::INFINITY,
        f32::NEG_INFINITY,
        f32::MIN_POSITIVE,
        -f32::MIN_POSITIVE,
        1e-45,
        -1e-45,
        1.0,
        -1.0,
        0.5,
        -0.5,
        0.999_969_5,
        -0.999_969_5,
        0.999_984_74,
        1.5 / 32768.0,
        2.5 / 32768.0,
        -1.5 / 32768.0,
        -2.5 / 32768.0,
        32766.5 / 32768.0,
        -32767.5 / 32768.0,
    ];

    #[test]
    fn rounding_matches_libm_on_the_edges_and_a_corpus() {
        for &x in &EDGES {
            assert_eq!(f32_to_i16(x), libm_f32_to_i16(x), "{x}");
            assert_eq!(f32_to_i32(x), libm_f32_to_i32(x), "{x}");
        }
        // every bit pattern class: an LCG over the raw bits, ten million of them
        let mut state = 0x0F32_0F32_0F32_0F32u64;
        for _ in 0..10_000_000u32 {
            state = state
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            let x = f32::from_bits((state >> 32) as u32);
            assert_eq!(
                f32_to_i16(x),
                libm_f32_to_i16(x),
                "{x} ({:#x})",
                x.to_bits()
            );
            assert_eq!(
                f32_to_i32(x),
                libm_f32_to_i32(x),
                "{x} ({:#x})",
                x.to_bits()
            );
        }
        // the reciprocal multiplies are the divisions, bit for bit
        for v in i16::MIN..=i16::MAX {
            assert_eq!(
                (f32::from(v) * (1.0 / 32768.0)).to_bits(),
                (f32::from(v) / 32768.0).to_bits()
            );
        }
        let mut state = 0x1332_1332u64;
        for _ in 0..10_000_000u32 {
            state = state
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            let v = (state >> 32) as i32;
            assert_eq!(
                ((v as f32) * (1.0 / 2_147_483_648.0)).to_bits(),
                ((v as f32) / 2_147_483_648.0).to_bits()
            );
        }
    }

    /// Every `f32` there is, both conversions: `cargo test --release -p
    /// rusty_esp_dsp -- --ignored exhaustive`. Run once per change to the
    /// rounding; the ledger records the run.
    #[test]
    #[ignore]
    fn rounding_matches_libm_exhaustively() {
        for bits in 0..=u32::MAX {
            let x = f32::from_bits(bits);
            assert_eq!(f32_to_i16(x), libm_f32_to_i16(x), "{x} ({bits:#x})");
            assert_eq!(f32_to_i32(x), libm_f32_to_i32(x), "{x} ({bits:#x})");
        }
    }

    #[test]
    fn same_format_copies_and_small_out_is_reported() {
        let data = [1u8, 2, 3, 4];
        let mut out = [0u8; 4];
        assert_eq!(
            convert(block(SampleFormat::I16, &data), SampleFormat::I16, &mut out).unwrap(),
            4
        );
        assert_eq!(out, data);
        let mut tiny = [0u8; 2];
        assert_eq!(
            convert(
                block(SampleFormat::I16, &data),
                SampleFormat::I32,
                &mut tiny
            )
            .err(),
            Some(Error::BufferTooSmall { needed: 8 })
        );
    }
}