edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBcfa93c0Tahinli3 months ago1659 commits
| 1 | //! Opus decoding (RFC 6716, with the RFC 8251 corrections). |
| 2 | //! |
| 3 | //! Opus is two codecs behind one entropy coder: a linear-prediction layer |
| 4 | //! (SILK) for speech up to 8 kHz of bandwidth, and an MDCT layer (CELT) for |
| 5 | //! everything above that and for music. A packet's table-of-contents byte picks |
| 6 | //! one of them or both, and this crate decodes all three cases plus the |
| 7 | //! multistream framing that carries 5.1 and 7.1. |
| 8 | //! |
| 9 | //! Contracts worth knowing before implementing against this crate: |
| 10 | //! |
| 11 | //! - **The FFT is never optional.** CELT synthesis runs an `N/4`-point FFT |
| 12 | //! through [`ec_dsp`], never a direct-form DFT. An incumbent Opus decoder in |
| 13 | //! this product shipped its FFT behind a default-off feature and ran at 0.95x |
| 14 | //! realtime on 5.1; there is no such switch here, and no code path that could |
| 15 | //! answer to one. (CELT's transform sizes are `15*2^k`, so [`ec_dsp::Mdct`] |
| 16 | //! itself — power-of-two only — cannot serve them; [`celt`] wraps |
| 17 | //! [`ec_dsp::Fft`] with one radix-3/radix-5 stage instead.) |
| 18 | //! - **Malformed input is an error, never a panic.** Framing rules `[R1]`..`[R7]` |
| 19 | //! are checked in [`packet`], and the range decoder feeds zeros past the end |
| 20 | //! of a truncated frame exactly as Section 4.1.2.1 requires. |
| 21 | //! - **Output is `f32`, interleaved, at the rate you asked for.** 48, 24, 16, |
| 22 | //! 12 and 8 kHz are supported; SILK's internal rate is resampled up (by the |
| 23 | //! normative resampler in [`silk`]) and CELT decimates on the way out. |
| 24 | //! - **There is no packet loss concealment.** A frame of one byte or none — |
| 25 | //! a dropped or DTX frame — decodes to silence, where the reference |
| 26 | //! extrapolates from the previous frame. This decoder is fed whole packets by |
| 27 | //! a container, never a gap, so the concealment would be dead weight; a |
| 28 | //! caller that needs it (an RTP receiver) has to add it. |
| 29 | //! |
| 30 | //! The crate also carries the family's Opus *encoder* — [`Encoder`] for one |
| 31 | //! stream, [`MultistreamEncoder`] for surround — CELT-only, fullband, mono |
| 32 | //! and stereo, every rate from 16 to 510 kbps, CBR and constrained VBR. |
| 33 | //! |
| 34 | //! **Why the incumbent encoder collapsed above ~128-165 kbps** (the measured |
| 35 | //! record this crate replaces; edith `engine/Cargo.toml:124-140` and |
| 36 | //! `export.rs:1834`): its packets above that rate were decodable only by its |
| 37 | //! *own* decoder — correlation 0.06 against the reference decoder at 256 kbps while its |
| 38 | //! internal round trip read 0.999 — and the rate the cliff sat at moved with |
| 39 | //! the content. That signature — self-consistent, world-divergent, at a |
| 40 | //! *bits-per-band* threshold — is a shared encode/decode convention error in |
| 41 | //! exactly the code CELT only exercises when a band's budget is large: the |
| 42 | //! over-32-bit-codeword band split (RFC 6716 Section 4.3.4.1: a band whose bit |
| 43 | //! demand exceeds its cache row is halved recursively, with an entropy-coded |
| 44 | //! angle and a rebalance of the leftover bits). An encoder whose split |
| 45 | //! bookkeeping (`tell`-derived budgets, rebalance, theta pdf selection) |
| 46 | //! drifts one bit from the normative derivation still decodes its own output |
| 47 | //! — both halves share the drift — but a conformant decoder desynchronises, |
| 48 | //! and the threshold moves with content because the split only triggers on |
| 49 | //! loud, flat bands. Content-dependence is also why mono broke at *every* |
| 50 | //! rate: half the coefficients means every band hits the split threshold at |
| 51 | //! half the bitrate. This encoder mirrors the reference's allocation |
| 52 | //! arithmetic symbol for symbol, and the tests hold the closed loop to it: |
| 53 | //! every packet is range-state-exact against this crate's own |
| 54 | //! RFC-vector-verified decoder, and the reference decoder (via the oracle) plus the reference |
| 55 | //! `opus_demo` decoder read the same packets at correlation >= 0.99 across |
| 56 | //! the whole rate table, mono, stereo and 5.1 alike. |
| 57 | //! |
| 58 | //! No unsafe, no allocation on the per-frame path beyond the codecs' own |
| 59 | //! buffers, no dependencies outside the family. |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | pub |
| 74 | |
| 75 | |
| 76 | pub use CeltDecoder; |
| 77 | pub use CeltFrameDiag; |
| 78 | pub use ; |
| 79 | pub use SilkFrameDiag; |
| 80 | pub use MultistreamDecoder; |
| 81 | pub use MultistreamEncoder; |
| 82 | pub use ; |
| 83 | pub use ; |
| 84 | pub use ; |
| 85 | pub use ; |
| 86 | |
| 87 | use ; |
| 88 | |
| 89 | /// One Opus stream: mono or stereo, any mode, any frame size. |
| 90 | /// |
| 91 | /// Feed whole Opus packets to [`Decoder::decode_float`]; the decoder tracks the |
| 92 | /// inter-frame state (energy history, MDCT overlap, LPC memory) that Opus needs |
| 93 | /// across packets, so packets must be delivered in order. |
| 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 | /// Cross-fades `b` over `a` across `overlap` samples using the CELT window, |
| 442 | /// the shape the reference uses for redundancy and mode transitions. |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 |