The 3/4-power curve as cube and two square roots
Browse files at this commit2d2451a65d36c0aed7d7a59820e7d97d0eceba95Parent: d97b1ac- perf
- the 3/4-power curve as cube and two square roots
1 changed file +42 -9
- crates/ec-mp3/src/encode.rs +42 -9
crates/ec-mp3/src/encode.rsmodified+42 -9
| @@ -765,7 +765,16 @@ impl Mp3Encode { | |||
| 765 | let count = (granules * channels).max(1); | 765 | let count = (granules * channels).max(1); |
| 766 | let demand: Vec<f64> = xrs | 766 | let demand: Vec<f64> = xrs |
| 767 | .iter() | 767 | .iter() |
| 768 | - .map(|xr| xr.iter().map(|v| f64::from(v.abs()).powf(0.75)).sum()) | 768 | + .map(|xr| { |
| 769 | + xr.iter() | ||
| 770 | + .map(|v| { | ||
| 771 | + let x = f64::from(v.abs()); | ||
| 772 | + // |x|^(3/4) as a cube and two square roots, the same | ||
| 773 | + // shape the quantiser uses. | ||
| 774 | + (x * x * x).sqrt().sqrt() | ||
| 775 | + }) | ||
| 776 | + .sum() | ||
| 777 | + }) | ||
| 769 | .collect(); | 778 | .collect(); |
| 770 | let demand_total: f64 = demand.iter().sum(); | 779 | let demand_total: f64 = demand.iter().sum(); |
| 771 | let demand_share = |budget: usize, index: usize| -> u32 { | 780 | let demand_share = |budget: usize, index: usize| -> u32 { |
| @@ -1369,9 +1378,14 @@ fn code_with( | |||
| 1369 | } | 1378 | } |
| 1370 | let mut noise = [0.0f32; SFB_LONG]; | 1379 | let mut noise = [0.0f32; SFB_LONG]; |
| 1371 | let power = power43(); | 1380 | let power = power43(); |
| 1381 | + // One band gain per band, not one per line: the residual ran an exp2 for | ||
| 1382 | + // every spectral line of the granule. | ||
| 1383 | + let mut scales = [0.0f32; SFB_LONG]; | ||
| 1384 | + for (band, slot) in scales.iter_mut().enumerate() { | ||
| 1385 | + *slot = band_gain(gain, scalefac[band], block_type); | ||
| 1386 | + } | ||
| 1372 | let residual = |i: usize, band: usize| -> f32 { | 1387 | let residual = |i: usize, band: usize| -> f32 { |
| 1373 | - let scale = band_gain(gain, scalefac[band], block_type); | 1388 | + let magnitude = power[(ix[i].unsigned_abs() as usize).min(MAX_QUANT)] * scales[band]; |
| 1374 | - let magnitude = power[(ix[i].unsigned_abs() as usize).min(MAX_QUANT)] * scale; | ||
| 1375 | let reconstructed = if ix[i] < 0 { -magnitude } else { magnitude }; | 1389 | let reconstructed = if ix[i] < 0 { -magnitude } else { magnitude }; |
| 1376 | (xr[i] - reconstructed).powi(2) | 1390 | (xr[i] - reconstructed).powi(2) |
| 1377 | }; | 1391 | }; |
| @@ -1442,18 +1456,37 @@ fn quantise( | |||
| 1442 | ix: &mut [i32], | 1456 | ix: &mut [i32], |
| 1443 | ) { | 1457 | ) { |
| 1444 | let quantise_line = |xr: f32, scale: f32| -> i32 { | 1458 | let quantise_line = |xr: f32, scale: f32| -> i32 { |
| 1445 | - let value = (xr.abs() * scale).powf(0.75); | 1459 | + // |xr·scale|^(3/4) as a cube and two square roots: the libm `powf` |
| 1446 | - let magnitude = (value + 0.4054).min(MAX_QUANT as f32) as i32; | 1460 | + // this replaces was a tenth of the encoder's runtime all by itself. |
| 1461 | + // The two forms differ by an ulp or two, which next to the level | ||
| 1462 | + // boundary offset below can flip the rare line by one quantisation | ||
| 1463 | + // step and nothing more. | ||
| 1464 | + let x = f64::from(xr.abs()) * f64::from(scale); | ||
| 1465 | + let value = (x * x * x).sqrt().sqrt(); | ||
| 1466 | + let magnitude = (value + 0.4054).min(MAX_QUANT as f64) as i32; | ||
| 1447 | if xr < 0.0 { -magnitude } else { magnitude } | 1467 | if xr < 0.0 { -magnitude } else { magnitude } |
| 1448 | }; | 1468 | }; |
| 1449 | if block_type == 2 { | 1469 | if block_type == 2 { |
| 1450 | // Short blocks: twelve bands, each with its own scalefactor shared by | 1470 | // Short blocks: twelve bands, each with its own scalefactor shared by |
| 1451 | // the three windows. Without them the quantiser's noise is flat across | 1471 | // the three windows. Without them the quantiser's noise is flat across |
| 1452 | // the spectrum, which is what made a switched granule the worst-coded | 1472 | // the spectrum, which is what made a switched granule the worst-coded |
| 1453 | - // one in the stream. | 1473 | + // one in the stream. One band gain per band, not one per line: the |
| 1454 | - for (i, slot) in ix.iter_mut().enumerate().take(576) { | 1474 | + // interleaved layout means a line's band search ran 576 exp2 calls |
| 1455 | - let scale = 1.0 / band_gain(gain, scalefac[short_band(i, sample_rate)], block_type); | 1475 | + // and 576 linear band searches per quantisation. |
| 1456 | - *slot = quantise_line(xr[i], scale); | 1476 | + let short = short_starts(sample_rate); |
| 1477 | + let mut scales = [0.0f32; 13]; | ||
| 1478 | + for (band, slot) in scales.iter_mut().enumerate() { | ||
| 1479 | + *slot = 1.0 / band_gain(gain, scalefac[band], block_type); | ||
| 1480 | + } | ||
| 1481 | + for band in 0..13 { | ||
| 1482 | + let (from, to) = ( | ||
| 1483 | + usize::from(short[band]) * 3, | ||
| 1484 | + usize::from(short[band + 1]) * 3, | ||
| 1485 | + ); | ||
| 1486 | + let scale = scales[band]; | ||
| 1487 | + for i in from..to.min(576) { | ||
| 1488 | + ix[i] = quantise_line(xr[i], scale); | ||
| 1489 | + } | ||
| 1457 | } | 1490 | } |
| 1458 | return; | 1491 | return; |
| 1459 | } | 1492 | } |