The gain search brackets instead of stepping one unit
Browse files at this commit59a18cfe38c54355a7f74267c53707c6ee81d832Parent: 19b1017- perf
- the gain search brackets instead of stepping one unit
1 changed file +71 -22
- crates/ec-mp3/src/encode.rs +71 -22
crates/ec-mp3/src/encode.rsmodified+71 -22
| @@ -1322,10 +1322,8 @@ fn code_with( | |||
| 1322 | let mut gain = start.clamp(0, 255); | 1322 | let mut gain = start.clamp(0, 255); |
| 1323 | let mut ix = [0i32; 576]; | 1323 | let mut ix = [0i32; 576]; |
| 1324 | let mut coding = [0i32; 576]; | 1324 | let mut coding = [0i32; 576]; |
| 1325 | - let mut trial: [i32; 576]; | 1325 | + // A line that saturates the coding range is not "cheap", it is clipped; |
| 1326 | - // The rate loop runs both ways: down while there are bits to spare, up | 1326 | + // the gain search below reads saturation as "does not fit". |
| 1327 | - // while the granule is too big for them. A line that saturates the coding | ||
| 1328 | - // range is not "cheap", it is clipped, so that check comes first. | ||
| 1329 | let saturated = |ix: &[i32]| ix.iter().any(|v| v.abs() > 8191); | 1327 | let saturated = |ix: &[i32]| ix.iter().any(|v| v.abs() > 8191); |
| 1330 | let mut plan_of = |ix: &[i32; 576]| -> SpectrumPlan { | 1328 | let mut plan_of = |ix: &[i32; 576]| -> SpectrumPlan { |
| 1331 | recode_into(ix, block_type, sample_rate, &mut coding); | 1329 | recode_into(ix, block_type, sample_rate, &mut coding); |
| @@ -1344,28 +1342,79 @@ fn code_with( | |||
| 1344 | // depend on gain. | 1342 | // depend on gain. |
| 1345 | gain = 0; | 1343 | gain = 0; |
| 1346 | } else { | 1344 | } else { |
| 1347 | - loop { | 1345 | + // The rate loop wants the smallest gain whose granule fits the |
| 1348 | - if (saturated(&ix) || coded.cost > target_bits) && gain < 255 { | 1346 | + // budget without clipping. A higher gain shrinks every quantised |
| 1349 | - gain += 1; | 1347 | + // line, and a smaller spectrum costs no more to code, so "fits" is |
| 1350 | - quantise(xr, gain, scalefac, sample_rate, block_type, &mut ix); | 1348 | + // monotone in the gain. This shipped as a one-step-at-a-time walk |
| 1351 | - coded = plan_of(&ix); | 1349 | + // from the start estimate and paid ~31 quantise-plus-plan |
| 1352 | - continue; | 1350 | + // evaluations per granule; now the start estimate seeds a geometric |
| 1351 | + // straddle (doubling steps, so a boundary `d` gains away costs | ||
| 1352 | + // about 2·log2 d probes instead of d) followed by a bisection of | ||
| 1353 | + // the bracket. The walk's two hard stops stay: gain 255 stands even | ||
| 1354 | + // when it does not fit (the low-pass loop below trims the | ||
| 1355 | + // spectrum), and gain 0 is the quietest legal gain. | ||
| 1356 | + let mut probe_of = |g: i32, out: &mut [i32; 576]| -> (bool, SpectrumPlan) { | ||
| 1357 | + quantise(xr, g, scalefac, sample_rate, block_type, out); | ||
| 1358 | + let plan = plan_of(out); | ||
| 1359 | + (plan.cost <= target_bits && !saturated(out), plan) | ||
| 1360 | + }; | ||
| 1361 | + let mut probe = [0i32; 576]; | ||
| 1362 | + // `floor` is the largest gain known to miss, `ceil` the smallest | ||
| 1363 | + // known to fit; −1 stands for "even 0 misses would not matter", the | ||
| 1364 | + // gain-0 floor. | ||
| 1365 | + let mut floor = -1i32; | ||
| 1366 | + let mut ceil = 255i32; | ||
| 1367 | + let mut capped = false; | ||
| 1368 | + let first = start.clamp(0, 255); | ||
| 1369 | + let (fits, _) = probe_of(first, &mut probe); | ||
| 1370 | + if fits { | ||
| 1371 | + // The boundary is at or below the estimate: stride down until a | ||
| 1372 | + // gain misses. | ||
| 1373 | + ceil = first; | ||
| 1374 | + let mut step = 1i32; | ||
| 1375 | + while ceil > 0 { | ||
| 1376 | + let next = (ceil - step).max(0); | ||
| 1377 | + let (next_fits, _) = probe_of(next, &mut probe); | ||
| 1378 | + if !next_fits { | ||
| 1379 | + floor = next; | ||
| 1380 | + break; | ||
| 1381 | + } | ||
| 1382 | + ceil = next; | ||
| 1383 | + step *= 2; | ||
| 1353 | } | 1384 | } |
| 1354 | - if saturated(&ix) || coded.cost > target_bits || gain == 0 { | 1385 | + } else { |
| 1355 | - break; | 1386 | + // The boundary is above the estimate: stride up until a gain |
| 1387 | + // fits, honouring the walk's 255 cap. | ||
| 1388 | + floor = first; | ||
| 1389 | + let mut step = 1i32; | ||
| 1390 | + loop { | ||
| 1391 | + let next = (floor + step).min(255); | ||
| 1392 | + let (next_fits, _) = probe_of(next, &mut probe); | ||
| 1393 | + if next_fits { | ||
| 1394 | + ceil = next; | ||
| 1395 | + break; | ||
| 1396 | + } | ||
| 1397 | + floor = next; | ||
| 1398 | + if next == 255 { | ||
| 1399 | + capped = true; | ||
| 1400 | + break; | ||
| 1401 | + } | ||
| 1402 | + step *= 2; | ||
| 1356 | } | 1403 | } |
| 1357 | - // Try one step quieter; keep it only if it still fits and stays | 1404 | + } |
| 1358 | - // inside the coding range. | 1405 | + while ceil - floor > 1 { |
| 1359 | - trial = ix; | 1406 | + let mid = (floor + ceil) / 2; |
| 1360 | - quantise(xr, gain - 1, scalefac, sample_rate, block_type, &mut trial); | 1407 | + let (mid_fits, _) = probe_of(mid, &mut probe); |
| 1361 | - let trial_plan = plan_of(&trial); | 1408 | + if mid_fits { |
| 1362 | - if trial_plan.cost > target_bits || saturated(&trial) { | 1409 | + ceil = mid; |
| 1363 | - break; | 1410 | + } else { |
| 1411 | + floor = mid; | ||
| 1364 | } | 1412 | } |
| 1365 | - gain -= 1; | ||
| 1366 | - ix = trial; | ||
| 1367 | - coded = trial_plan; | ||
| 1368 | } | 1413 | } |
| 1414 | + gain = if capped { 255 } else { ceil }; | ||
| 1415 | + let (_, plan) = probe_of(gain, &mut probe); | ||
| 1416 | + ix = probe; | ||
| 1417 | + coded = plan; | ||
| 1369 | } | 1418 | } |
| 1370 | // Last resort: if even the quietest legal gain will not fit the bits we | 1419 | // Last resort: if even the quietest legal gain will not fit the bits we |
| 1371 | // were given, drop the top of the spectrum until it does. A granule that | 1420 | // were given, drop the top of the spectrum until it does. A granule that |