edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB70849e0Tahinli2 months ago3122 commits
| 1 | //! TrueHD/MLP access-unit framing: the 16-bit length word, major sync and the |
| 2 | //! per-substream directory. |
| 3 | //! |
| 4 | //! Every field parsed here is calibrated against a real Blu-ray 7.1 TrueHD |
| 5 | //! remux (`~/Downloads/.../ReelR ... TrueHD 7 1 ...mkv`, see |
| 6 | //! `tests/library.rs`): the access-unit length, the major sync's sample-rate |
| 7 | //! codes, and the substream directory's end pointers all reproduce ffprobe's |
| 8 | //! own 48000 Hz / 8-channel / 24-bit report on that file, AU for AU. |
| 9 | //! |
| 10 | //! **Bit layout implemented here** (byte offsets are from the start of the |
| 11 | //! access unit, i.e. from `data[0]`): |
| 12 | //! |
| 13 | //! - `data[0..2]` big-endian: top 4 bits a check/parity nibble (not |
| 14 | //! validated — no public description of its generator was available to |
| 15 | //! check against), low 12 bits the access-unit length in 16-bit words |
| 16 | //! *including this 4-byte header*. [`frame_length`] returns that length in |
| 17 | //! bytes. |
| 18 | //! - `data[2..4]`: input timing, carried on [`AccessUnitHeader::input_timing`] |
| 19 | //! uninterpreted — a decoder does not need it to reconstruct samples. |
| 20 | //! - `data[4..8]`: `0xF8726FBA` (TrueHD) or `0xF8726FBB` (MLP) names a major |
| 21 | //! sync at this access unit; absent on most access units, present roughly |
| 22 | //! once per video frame. |
| 23 | //! - When a major sync is present, it occupies exactly 28 bytes starting at |
| 24 | //! `data[4]` (format id + 24 more bytes, ending where the substream |
| 25 | //! directory begins): `data[8]` packs two 4-bit sample-rate codes, |
| 26 | //! `group1` in the high nibble and `group2` (a secondary/backward-compat |
| 27 | //! rate, `0xF` when unused) in the low nibble. A code maps to a rate via |
| 28 | //! [`sample_rate`]; codes 0-2 are the 48 kHz family (`48000 * 2^n`), codes |
| 29 | //! 8-10 the 44.1 kHz family (`44100 * 2^n`). The rest of the major sync |
| 30 | //! (channel-assignment bitfields, flags) is skipped: this build does not |
| 31 | //! independently verify their bit positions, so [`ChannelLayout`] is |
| 32 | //! derived from the substream *directory* instead (see below), which is |
| 33 | //! fully verified against the sample file. |
| 34 | //! - The substream directory starts right after the major sync (or right |
| 35 | //! after the 4-byte access-unit header, on an access unit with none), one |
| 36 | //! [`SubstreamInfo`] entry per substream: a 2-byte word whose top 4 bits |
| 37 | //! are flags (bit `0x8` says a second 2-byte word — a checksum/parity/DRC |
| 38 | //! word, [`SubstreamInfo::extra`] — follows) and whose low 12 bits are the |
| 39 | //! substream's *end pointer*: the cumulative byte offset, in 16-bit words |
| 40 | //! from the end of the whole directory, where this substream's coded data |
| 41 | //! ends. There is no explicit substream-count field; a directory entry is |
| 42 | //! the *last* one exactly when `directory_end + end_pointer_bytes` lands |
| 43 | //! on the access unit's own total length — which is how |
| 44 | //! [`AccessUnitHeader::parse`] finds the count, bounded at 4 entries |
| 45 | //! (Dolby's own substream cap). |
| 46 | //! |
| 47 | //! Substream count maps to [`ChannelLayout`] by Dolby's own published |
| 48 | //! substream convention — substream 0 always carries a 2-ch presentation, |
| 49 | //! substream 1 (when present) adds a discrete 6-ch (5.1) presentation, |
| 50 | //! substream 2 (when present) adds a discrete 8-ch (7.1) presentation, and a |
| 51 | //! 4th substream carries an unrelated object-audio payload — rather than |
| 52 | //! from the major sync's own channel-assignment bitfields, for the reason |
| 53 | //! above. |
| 54 | |
| 55 | use ; |
| 56 | use ; |
| 57 | use Packet; |
| 58 | use ; |
| 59 | |
| 60 | /// TrueHD's major sync word, immediately followed by the extension fields |
| 61 | /// TrueHD adds over plain MLP. |
| 62 | pub const MAJOR_SYNC_TRUEHD: u32 = 0xF872_6FBA; |
| 63 | /// MLP's major sync word (lossless-only, no TrueHD extensions). |
| 64 | pub const MAJOR_SYNC_MLP: u32 = 0xF872_6FBB; |
| 65 | |
| 66 | /// Bytes a major sync occupies, format id included, ending where the |
| 67 | /// substream directory begins. |
| 68 | const MAJOR_SYNC_LEN: usize = 28; |
| 69 | |
| 70 | /// Bytes the fixed part of an access unit's own header takes: the 16-bit |
| 71 | /// length/check word and the 16-bit input timing. |
| 72 | const AU_HEADER_LEN: usize = 4; |
| 73 | |
| 74 | /// Access-unit length in bytes, from the 16-bit length word at `data[0..2]`. |
| 75 | /// |
| 76 | /// A short buffer is [`Error::NeedMore`]; a length that could not even cover |
| 77 | /// its own 4-byte header is [`Error::Corrupt`]. |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | /// Which format a major sync names. |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | /// The format info an access unit's major sync carries. |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | /// A rate code's `44.1k*2^n` / `48k*2^n` value, or [`Error::Unsupported`] for |
| 118 | /// a code the format reserves. |
| 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 | /// One substream directory entry: where its coded data ends and whether a |
| 166 | /// checksum/parity/DRC word rides along with the end pointer. |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | /// One access unit's header: its own length, optional major sync, and the |
| 182 | /// substream directory that follows. |
| 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 | /// A TrueHD/MLP decoder for one stream: access units in, one interleaved |
| 299 | /// S32 (24-bit left-justified) [`AudioFrame`] of the highest presentation |
| 300 | /// out per access unit. See the crate-level docs for scope. |
| 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 |