Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mp4/blob/main/src/box.om # omega-mp4 omega-mp4: ovideo, an MP4 reader written in Omega (Apache-2.0) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mp4 / src / box.om` 1173 lines ยท 26653 bytes Raw Blame History 1 note omega-mp4 (command ovideo), src/box.om: the box parser. Entry: bparse f (after openin arg f). 2 note Helpers, constants and globals start with b / B. Sources: ISO/IEC 14496-12 (ISO base media file 3 note format), cited "14496-12 n.n.n" (clause numbers of editions 5 and 6); the QuickTime File Format 4 note uses the same box (atom) syntax for every box read here; ISO/IEC 23001-7 (common encryption) for 5 note pssh and senc. bparse fills the file fields fl (fset), one track slot per trak (tset), the edit 6 note entries eld/elm/elr and the sample arrays soff/ssz/sdts/scto/sdur/ssync, then advances ns. 7 note Refusals (exit code, io.om text): 8 note 20 ebox box header below its header length, past its parent or the file, a box version or a 9 note fixed field the standard does not allow (timescale 0, track_ID 0, data reference 0) 10 note 22 emiss a required box missing or repeated (ftyp, moov, mvhd, trak, tkhd, mdia, mdhd, hdlr, 11 note minf, stbl, stsd, stts, stsc, stsz, stco/co64; also edts, elst, dinf, dref and the 12 note minf media header when repeated) 13 note 23 etable sample table counts disagree, a count exceeds its box, stss/stsc order, a sample 14 note description index other than 1, a decode time or offset past 2^64 - 1, or a chunk 15 note whose samples do not lie inside one mdat payload 16 note 30 efrag fragmented: moof, mfra, styp, sidx or ssix at top level, mvex in moov 17 note 31 eenc protected: sample entry encv, enca, enct, encs, encf, encm or resv, pssh in moov, 18 note senc in stbl 19 note 33 eunsup stz2, more than one sample description, a data reference without the self-contained 20 note flag, an edit list entry with media_time below -1 or a media edit at a rate other 21 note than 1 (any list of empty edits and rate-1 media edits is accepted; see bedit) 22 note 34 ebound/edepth more than 8 traks, 128 track slots, 64 edit entries, `1048576` samples or 23 note chunks or table entries, 65536 boxes visited, a 1 MiB sample description box 24 note 60 eio read failure (io.om need) 25 note Every count read from a box is checked against the bytes of that box before a loop uses it, every 26 note index is checked against its bound before the array is indexed, and every sum (decode time, 27 note sample offset) is checked against 2^64 - 1 before it is formed. Box walks follow box sizes from 28 note the top of the file; nothing is found by searching bytes for a type. 29 30 note Box types: the four ASCII bytes of the type read as one big-endian 32-bit number (14496-12 4.2). 31 const BFTYP ulong `1718909296` 32 const BMOOV ulong `1836019574` 33 const BMDAT ulong `1835295092` 34 const BMOOF ulong `1836019558` 35 const BMFRA ulong `1835430497` 36 const BSTYP ulong `1937013104` 37 const BSIDX ulong `1936286840` 38 const BSSIX ulong `1936943480` 39 const BMVEX ulong `1836475768` 40 const BMVHD ulong `1836476516` 41 const BTRAK ulong `1953653099` 42 const BTKHD ulong `1953196132` 43 const BEDTS ulong `1701082227` 44 const BELST ulong `1701606260` 45 const BMDIA ulong `1835297121` 46 const BMDHD ulong `1835296868` 47 const BHDLR ulong `1751411826` 48 const BMINF ulong `1835626086` 49 const BSTBL ulong `1937007212` 50 const BSTSD ulong `1937011556` 51 const BSTTS ulong `1937011827` 52 const BCTTS ulong `1668576371` 53 const BSTSS ulong `1937011571` 54 const BSTSC ulong `1937011555` 55 const BSTSZ ulong `1937011578` 56 const BSTZ2 ulong `1937013298` 57 const BSTCO ulong `1937007471` 58 const BCO64 ulong `1668232756` 59 const BVMHD ulong `1986881636` 60 const BSMHD ulong `1936549988` 61 const BHMHD ulong `1752000612` 62 const BNMHD ulong `1852663908` 63 const BSTHD ulong `1937008740` 64 const BGMHD ulong `1735223396` 65 const BDINF ulong `1684631142` 66 const BDREF ulong `1685218662` 67 const BUUID ulong `1970628964` 68 const BPSSH ulong `1886614376` 69 const BSENC ulong `1936027235` 70 const BENCV ulong `1701733238` 71 const BENCA ulong `1701733217` 72 const BENCT ulong `1701733236` 73 const BENCS ulong `1701733235` 74 const BENCF ulong `1701733222` 75 const BENCM ulong `1701733229` 76 const BRESV ulong `1919251318` 77 const BVIDE ulong `1986618469` 78 const BSOUN ulong `1936684398` 79 80 note bhd: the last header read by bhdr: type, header length, box size, payload length. 81 global bhd ulong 4 82 note bmdp, bmde: payload start and end of each mdat box of the input being parsed, in file order 83 note (so both ascend); bnmd their count (at most 65536: every mdat is a visited box). 84 global bmdp ulong 65536 85 global bmde ulong 65536 86 global bnmd ulong 1 87 note bdrc: entry count of the dref box of the track being parsed, 0 when its minf has none. 88 global bdrc ulong 1 89 note bch: the chunk offsets (stco/co64) of the track being parsed. 90 global bch ulong `1048576` 91 note bdt: the running decode time of bstts. bsc: the stsc walk (0 the next track sample, 1 the 92 note previous entry's first_chunk, 2 its samples_per_chunk). 93 global bdt ulong 1 94 global bsc ulong 3 95 96 note ---- box headers ---- 97 note bhdr: the box at p inside a parent that ends at e (14496-12 4.2): size (32), type (32); size 1: 98 note largesize (64) follows; size 0: the box runs to the end of the file, accepted only when its 99 note parent ends there; type uuid: a 16-byte extended type follows. The size must cover the header 100 note and stay inside the parent. Counts one visited box against NB. 101 fn bhdr f ulong p ulong e ulong to void 102 put nbox 0 add get nbox 0 1 103 if gt get nbox 0 NB 104 quit 34 edepth 105 end 106 if gt p e 107 quit 20 ebox 108 end 109 if lt sub e p 8 110 quit 20 ebox 111 end 112 s = rd32 f p 113 t = rd32 f add p 4 114 h = ulong 8 115 if eq s 1 116 if lt sub e p 16 117 quit 20 ebox 118 end 119 s = rd64 f add p 8 120 h = 16 121 else 122 if eq s 0 123 if ne e fget f FSZ 124 quit 20 ebox 125 end 126 s = sub e p 127 end 128 end 129 if eq t BUUID 130 h = add h 16 131 end 132 if lt s h 133 quit 20 ebox 134 end 135 if gt s sub e p 136 quit 20 ebox 137 end 138 put bhd 0 t 139 put bhd 1 h 140 put bhd 2 s 141 put bhd 3 sub s h 142 end 143 144 note bneed: a payload of h bytes must hold at least n (the fixed fields of its box). 145 fn bneed n ulong h ulong to void 146 if lt h n 147 quit 20 ebox 148 end 149 end 150 151 note bonce: a box that may appear once was already seen (v is its recorded size, nonzero once seen). 152 fn bonce v ulong to void 153 if ne v 0 154 quit 22 emiss 155 end 156 end 157 158 note breq: the box recorded at size field k of track slot sl is required. 159 fn breq sl ulong k ulong to void 160 if eq tg sl k 0 161 quit 22 emiss 162 end 163 end 164 165 note bloc: record the box at a, size z, at location field k (size field k + 1) of slot sl, once. 166 fn bloc sl ulong k ulong a ulong z ulong to void 167 bonce tg sl add k 1 168 tset sl k a 169 tset sl add k 1 z 170 end 171 172 note bopen: re-read the whole box recorded at fields k, k + 1 of slot sl; its payload start (the 173 note payload length is in bhd 3). 174 fn bopen f ulong sl ulong k ulong to ulong 175 p = tg sl k 176 bhdr f p add p tg sl add k 1 177 ret add p get bhd 1 178 end 179 180 note bcnt: a table with 8 bytes of version, flags and entry_count, then c entries of w bytes: c must 181 note fit the payload (code 23) and the `1048576` entry bound (code 34). 182 fn bcnt c ulong w ulong to void 183 if lt get bhd 3 8 184 quit 20 ebox 185 end 186 if gt c div sub get bhd 3 8 w 187 quit 23 etable 188 end 189 if gt c NS 190 quit 34 ebound 191 end 192 end 193 194 note bsx32: a 32-bit two's complement field (read unsigned) as a signed value. 195 fn bsx32 x ulong to long 196 y = bitcast long x 197 if ge x `2147483648` 198 y = sub y `4294967296` 199 end 200 ret y 201 end 202 203 note bfragt: top-level boxes of fragmented or segmented files: moof (8.8.4), mfra (8.8.9), styp 204 note (8.16.2), sidx (8.16.3), ssix (8.16.4). 205 fn bfragt t ulong to ulong 206 if or eq t BMOOF eq t BMFRA 207 ret 1 208 end 209 if or eq t BSTYP eq t BSIDX 210 ret 1 211 end 212 if eq t BSSIX 213 ret 1 214 end 215 ret 0 216 end 217 218 note benct: protected sample entry types: encv, enca, enct, encs (14496-12 8.12: the original 219 note format moves into sinf and the entry type signals protection), encf and encm (also refused as 220 note protected, conservatively), resv (restricted scheme, 14496-12 8.15). 221 fn benct t ulong to ulong 222 if or eq t BENCV eq t BENCA 223 ret 1 224 end 225 if or eq t BENCT eq t BENCS 226 ret 1 227 end 228 if or eq t BENCF eq t BENCM 229 ret 1 230 end 231 if eq t BRESV 232 ret 1 233 end 234 ret 0 235 end 236 237 note bmhdt: media header boxes of minf: vmhd (12.1.2), smhd (12.2.2), hmhd (12.4.2), nmhd (8.4.5.2), 238 note sthd (12.6.2) and the QuickTime base media header gmhd. 239 fn bmhdt t ulong to ulong 240 if or eq t BVMHD eq t BSMHD 241 ret 1 242 end 243 if or eq t BHMHD eq t BNMHD 244 ret 1 245 end 246 if or eq t BSTHD eq t BGMHD 247 ret 1 248 end 249 ret 0 250 end 251 252 note ---- entry ---- 253 note bparse: walk the top level, then moov. The caller has run openin for file f. 254 fn bparse f ulong to void 255 if ge f NF 256 quit 34 ebound 257 end 258 put nbox 0 0 259 put bnmd 0 0 260 btop f 261 bmoov f 262 end 263 264 note btop: the top-level boxes in file order (14496-12 4.2, 6.2): ftyp (4.3) and moov (8.2.1) once 265 note each and required; every mdat (8.1.1) payload extent recorded; fragment boxes refused 30; any 266 note other box (free, skip, wide, udta, meta, uuid ...) is skipped by its size. 267 fn btop f ulong to void 268 z = fget f FSZ 269 p = ulong 0 270 while lt p z bound 65537 271 bhdr f p z 272 t = get bhd 0 273 h = get bhd 1 274 s = get bhd 2 275 if eq t BFTYP 276 bonce fget f FFTS 277 fset f FFTP p 278 fset f FFTS s 279 end 280 if eq t BMOOV 281 bonce fget f FMOOVS 282 fset f FMOOVP p 283 fset f FMOOVS s 284 end 285 if eq t BMDAT 286 bmdat f add p h add p s 287 end 288 if eq bfragt t 1 289 fset f FFRAG 1 290 quit 30 efrag 291 end 292 p = add p s 293 end 294 if eq fget f FFTS 0 295 quit 22 emiss 296 end 297 if eq fget f FMOOVS 0 298 quit 22 emiss 299 end 300 end 301 302 note bmdat: one mdat payload [a, b); FMDATP is the first payload, FNMDAT the count. 303 fn bmdat f ulong a ulong b ulong to void 304 n = get bnmd 0 305 if ge n NB 306 quit 34 edepth 307 end 308 put bmdp n a 309 put bmde n b 310 if eq n 0 311 fset f FMDATP a 312 end 313 put bnmd 0 add n 1 314 fset f FNMDAT add n 1 315 end 316 317 note ---- moov (8.2.1) ---- 318 note bmoov: mvhd once (required), trak at least once (at most 8), mvex (8.8.1) refused 30, pssh 319 note (23001-7 8.1) refused 31; other children (udta, meta, iods ...) skipped. Then mvhd, then each 320 note trak into the next track slot; FTK0 is the first slot of this file, FNTK the count. 321 fn bmoov f ulong to void 322 a = fget f FMOOVP 323 e = add a fget f FMOOVS 324 ta = new ulong 8 0 325 tz = new ulong 8 0 326 bhdr f a e 327 q = add a get bhd 1 328 n = ulong 0 329 while lt q e bound 65537 330 bhdr f q e 331 t = get bhd 0 332 z = get bhd 2 333 if eq t BMVHD 334 bonce fget f FMVS 335 fset f FMVP q 336 fset f FMVS z 337 end 338 if eq t BTRAK 339 if ge n NTF 340 quit 34 ebound 341 end 342 put ta n q 343 put tz n z 344 n = add n 1 345 end 346 if eq t BMVEX 347 fset f FFRAG 1 348 quit 30 efrag 349 end 350 if eq t BPSSH 351 quit 31 eenc 352 end 353 q = add q z 354 end 355 if eq fget f FMVS 0 356 quit 22 emiss 357 end 358 if eq n 0 359 quit 22 emiss 360 end 361 bmvhd f 362 fset f FTK0 get ntk 0 363 for i ulong n bound 8 364 btrak f get ta i get tz i 365 end 366 fset f FNTK n 367 end 368 369 note bmvhd (8.2.2): FullBox; version 1: creation_time (64), modification_time (64), timescale (32), 370 note duration (64); version 0: 32, 32, 32, 32; then rate, volume, reserved, matrix, pre_defined, 371 note next_track_ID (80 bytes). Payload at least 112 (v1) or 100 (v0) bytes; timescale not 0. 372 fn bmvhd f ulong to void 373 a = fget f FMVP 374 bhdr f a add a fget f FMVS 375 q = add a get bhd 1 376 bneed 4 get bhd 3 377 v = rd8 f q 378 ts = ulong 0 379 d = ulong 0 380 if eq v 1 381 bneed 112 get bhd 3 382 ts = rd32 f add q 20 383 d = rd64 f add q 24 384 else 385 if ne v 0 386 quit 20 ebox 387 end 388 bneed 100 get bhd 3 389 ts = rd32 f add q 12 390 d = rd32 f add q 16 391 end 392 if eq ts 0 393 quit 20 ebox 394 end 395 fset f FMTS ts 396 fset f FMDUR d 397 fset f FMVV v 398 end 399 400 note ---- trak (8.3.1) ---- 401 note btrak: claim the next track slot (fields and edit entries zeroed), then tkhd once (required), 402 note edts once (optional), mdia once (required); tref, udta, meta and others skipped. 403 fn btrak f ulong a ulong s ulong to void 404 sl = get ntk 0 405 if ge sl NT 406 quit 34 ebound 407 end 408 for k ulong 64 409 tset sl k 0 410 put eld add mul sl 64 k 0 411 put elm add mul sl 64 k 0 412 put elr add mul sl 64 k 0 413 end 414 put ntk 0 add sl 1 415 tset sl KFILE f 416 tset sl KTRKP a 417 tset sl KTRKS s 418 e = add a s 419 bhdr f a e 420 q = add a get bhd 1 421 ea = ulong 0 422 ez = ulong 0 423 ma = ulong 0 424 mz = ulong 0 425 while lt q e bound 65537 426 bhdr f q e 427 t = get bhd 0 428 z = get bhd 2 429 if eq t BTKHD 430 bonce tg sl KTKS 431 tset sl KTKP q 432 tset sl KTKS z 433 end 434 if eq t BEDTS 435 bonce ez 436 ea = q 437 ez = z 438 end 439 if eq t BMDIA 440 bonce mz 441 ma = q 442 mz = z 443 end 444 q = add q z 445 end 446 breq sl KTKS 447 if eq mz 0 448 quit 22 emiss 449 end 450 btkhd f sl 451 if ne ez 0 452 bedts f sl ea ez 453 end 454 bedit sl 455 bmdia f sl ma mz 456 end 457 458 note btkhd (8.3.2): FullBox; version 1: creation_time (64), modification_time (64), track_ID (32), 459 note reserved (32), duration (64); version 0: 32 each; then reserved, layer, alternate_group, 460 note volume, reserved, matrix, width, height (60 bytes). Payload at least 96 (v1) or 84 (v0) bytes; 461 note track_ID cannot be 0 (8.3.2.3). KTKD is the duration in movie timescale units. 462 fn btkhd f ulong sl ulong to void 463 q = bopen f sl KTKP 464 bneed 4 get bhd 3 465 v = rd8 f q 466 id = ulong 0 467 d = ulong 0 468 if eq v 1 469 bneed 96 get bhd 3 470 id = rd32 f add q 20 471 d = rd64 f add q 28 472 else 473 if ne v 0 474 quit 20 ebox 475 end 476 bneed 84 get bhd 3 477 id = rd32 f add q 12 478 d = rd32 f add q 20 479 end 480 if eq id 0 481 quit 20 ebox 482 end 483 tset sl KTID id 484 tset sl KTKD d 485 end 486 487 note bedts (8.6.5): the edit box holds at most one edit list box. 488 fn bedts f ulong sl ulong a ulong s ulong to void 489 e = add a s 490 bhdr f a e 491 q = add a get bhd 1 492 while lt q e bound 65537 493 bhdr f q e 494 t = get bhd 0 495 z = get bhd 2 496 if eq t BELST 497 bonce tg sl KELS 498 tset sl KELP q 499 tset sl KELS z 500 end 501 q = add q z 502 end 503 if ne tg sl KELS 0 504 belst f sl 505 end 506 end 507 508 note belst (8.6.6): FullBox; entry_count (32); per entry, version 1: segment_duration (64, movie 509 note timescale), media_time (signed 64, media timescale, -1 = empty edit); version 0: 32 and signed 510 note 32; then media_rate_integer (16) and media_rate_fraction (16), kept as one fixed 16.16 value. 511 note The count must fit the payload (code 20) and the 64-entry bound (code 34). 512 fn belst f ulong sl ulong to void 513 o = bopen f sl KELP 514 bneed 8 get bhd 3 515 v = rd8 f o 516 w = ulong 12 517 if eq v 1 518 w = 20 519 else 520 if ne v 0 521 quit 20 ebox 522 end 523 end 524 c = rd32 f add o 4 525 if gt c div sub get bhd 3 8 w 526 quit 20 ebox 527 end 528 if gt c NE 529 quit 34 ebound 530 end 531 tset sl KNE c 532 tset sl KEDM 1 533 b = mul sl 64 534 o = add o 8 535 for i ulong c bound 64 536 d = ulong 0 537 m = long 0 538 if eq v 1 539 d = rd64 f o 540 m = bitcast long rd64 f add o 8 541 o = add o 16 542 else 543 d = rd32 f o 544 m = bsx32 rd32 f add o 4 545 o = add o 8 546 end 547 put eld add b i d 548 put elm add b i m 549 put elr add b i rd32 f o 550 o = add o 4 551 end 552 end 553 554 note bedit: classify the edit list (8.6.6) into KEDM and the normalized fields KE0, KMT, KED. 555 note Every entry must be an empty edit (media_time -1, rate not checked) or a media edit (media_time 556 note >= 0) at rate 1.0 (fixed 16.16 65536); any other entry is refused 33. Then: 557 note KEDM 0 no edit list: KE0 0, KMT 0, KED the tkhd duration. 558 note KEDM 1 simple: [one empty edit] then exactly one media edit: KE0 the empty edit's 559 note segment_duration (0 without one), KMT the media edit's media_time, KED its 560 note segment_duration. 561 note KEDM 2 complex: any other accepted list (no entries, only empty edits, several media edits, 562 note an empty edit after a media edit): KE0, KMT and KED are 0; the entries stay in 563 note eld/elm/elr. info and dump accept it; trim and join refuse it (plan.om, code 33). 564 fn bedit sl ulong to void 565 if eq tg sl KEDM 0 566 tset sl KE0 0 567 tset sl KMT 0 568 tset sl KED tg sl KTKD 569 ret 570 end 571 c = tg sl KNE 572 b = mul sl 64 573 for i ulong c bound 64 574 m = get elm add b i 575 if lt m -1 576 quit 33 eunsup 577 end 578 if ge m 0 579 if ne get elr add b i 65536 580 quit 33 eunsup 581 end 582 end 583 end 584 tset sl KE0 0 585 tset sl KMT 0 586 tset sl KED 0 587 j = ulong 0 588 e0 = ulong 0 589 if gt c 0 590 if eq get elm b -1 591 e0 = get eld b 592 j = 1 593 end 594 end 595 if ne sub c j 1 596 tset sl KEDM 2 597 ret 598 end 599 k = add b j 600 if lt get elm k 0 601 tset sl KEDM 2 602 ret 603 end 604 tset sl KE0 e0 605 tset sl KMT bitcast ulong get elm k 606 tset sl KED get eld k 607 end 608 609 note ---- mdia (8.4.1) ---- 610 note bmdia: mdhd, hdlr and minf, each exactly once. 611 fn bmdia f ulong sl ulong a ulong s ulong to void 612 e = add a s 613 bhdr f a e 614 q = add a get bhd 1 615 ma = ulong 0 616 mz = ulong 0 617 while lt q e bound 65537 618 bhdr f q e 619 t = get bhd 0 620 z = get bhd 2 621 if eq t BMDHD 622 bonce tg sl KMDHS 623 tset sl KMDHP q 624 tset sl KMDHS z 625 end 626 if eq t BHDLR 627 bonce tg sl KHDLS 628 tset sl KHDLP q 629 tset sl KHDLS z 630 end 631 if eq t BMINF 632 bonce mz 633 ma = q 634 mz = z 635 end 636 q = add q z 637 end 638 breq sl KMDHS 639 breq sl KHDLS 640 if eq mz 0 641 quit 22 emiss 642 end 643 bmdhd f sl 644 bhdlr f sl 645 bminf f sl ma mz 646 end 647 648 note bmdhd (8.4.2): FullBox; version 1: creation_time (64), modification_time (64), timescale (32), 649 note duration (64); version 0: 32 each; then pad + language (16), pre_defined (16). Payload at least 650 note 36 (v1) or 24 (v0) bytes; timescale not 0. 651 fn bmdhd f ulong sl ulong to void 652 q = bopen f sl KMDHP 653 bneed 4 get bhd 3 654 v = rd8 f q 655 ts = ulong 0 656 d = ulong 0 657 if eq v 1 658 bneed 36 get bhd 3 659 ts = rd32 f add q 20 660 d = rd64 f add q 24 661 else 662 if ne v 0 663 quit 20 ebox 664 end 665 bneed 24 get bhd 3 666 ts = rd32 f add q 12 667 d = rd32 f add q 16 668 end 669 if eq ts 0 670 quit 20 ebox 671 end 672 tset sl KTS ts 673 tset sl KMDUR d 674 end 675 676 note bhdlr (8.4.3): FullBox; pre_defined (32); handler_type (32); reserved (3 x 32); name. The 677 note QuickTime handler (component type, subtype, manufacturer, flags, mask, name) has its subtype at 678 note the same offset. KKEEP is 1 for vide and soun. 679 fn bhdlr f ulong sl ulong to void 680 q = bopen f sl KHDLP 681 bneed 24 get bhd 3 682 h = rd32 f add q 8 683 tset sl KHDLR h 684 k = ulong 0 685 if or eq h BVIDE eq h BSOUN 686 k = 1 687 end 688 tset sl KKEEP k 689 end 690 691 note bminf (8.4.4): one media header (bmhdt; optional, recorded once as KMINP/KMINS/KMINH), dinf 692 note (8.7.1, optional, once), stbl (8.5.1, required, once). A QuickTime data handler hdlr here is 693 note skipped: the media handler is the one in mdia. 694 fn bminf f ulong sl ulong a ulong s ulong to void 695 e = add a s 696 bhdr f a e 697 q = add a get bhd 1 698 put bdrc 0 0 699 dn = ulong 0 700 while lt q e bound 65537 701 bhdr f q e 702 t = get bhd 0 703 z = get bhd 2 704 if eq bmhdt t 1 705 bonce tg sl KMINS 706 tset sl KMINP q 707 tset sl KMINS z 708 tset sl KMINH t 709 end 710 if eq t BDINF 711 bonce dn 712 dn = 1 713 bdinf f q z 714 end 715 if eq t BSTBL 716 bonce tg sl KSTBS 717 tset sl KSTBP q 718 tset sl KSTBS z 719 end 720 q = add q z 721 end 722 breq sl KSTBS 723 bstbl f sl 724 end 725 726 note bdinf (8.7.1): at most one dref. 727 fn bdinf f ulong a ulong s ulong to void 728 e = add a s 729 bhdr f a e 730 q = add a get bhd 1 731 dn = ulong 0 732 while lt q e bound 65537 733 bhdr f q e 734 t = get bhd 0 735 z = get bhd 2 736 if eq t BDREF 737 bonce dn 738 dn = 1 739 bdref f q z 740 end 741 q = add q z 742 end 743 end 744 745 note bdref (8.7.2): FullBox; entry_count (32), at least 1 and each entry a FullBox of at least 12 746 note bytes (url , urn , QuickTime alis); flag 1 means the media data is in this file. An entry 747 note without it is an external data reference: refused 33. 748 fn bdref f ulong a ulong s ulong to void 749 e = add a s 750 bhdr f a e 751 q = add a get bhd 1 752 bneed 8 get bhd 3 753 c = rd32 f add q 4 754 if eq c 0 755 quit 20 ebox 756 end 757 if gt c div sub get bhd 3 8 12 758 quit 20 ebox 759 end 760 if gt c NB 761 quit 34 edepth 762 end 763 o = add q 8 764 for i ulong c bound 65536 765 bhdr f o e 766 if lt get bhd 3 4 767 quit 20 ebox 768 end 769 g = rd32 f add o get bhd 1 770 if eq and g 1 0 771 quit 33 eunsup 772 end 773 o = add o get bhd 2 774 end 775 put bdrc 0 c 776 end 777 778 note ---- stbl (8.5.1) ---- 779 note bstbl: record stsd, stts, ctts, stss, stsc, stsz, stco or co64 (each once; stco and co64 share 780 note one location, so both present is a repeat); stz2 (8.7.3.3) refused 33; senc (23001-7 7.2) 781 note refused 31; other children (sdtp, sbgp, sgpd, subs, cslg, stps, saiz, saio) skipped. Then the 782 note tables in an order where each pass reads one table front to back (the input window is 1 MiB). 783 fn bstbl f ulong sl ulong to void 784 q = bopen f sl KSTBP 785 e = add tg sl KSTBP tg sl KSTBS 786 while lt q e bound 65537 787 bhdr f q e 788 t = get bhd 0 789 z = get bhd 2 790 if eq t BSTSD 791 bloc sl KSDP q z 792 end 793 if eq t BSTTS 794 bloc sl KSTTP q z 795 end 796 if eq t BCTTS 797 bloc sl KCTP q z 798 end 799 if eq t BSTSS 800 bloc sl KSSP q z 801 end 802 if eq t BSTSC 803 bloc sl KSCP q z 804 end 805 if eq t BSTSZ 806 bloc sl KSZP q z 807 end 808 if eq t BSTCO 809 bloc sl KCOP q z 810 tset sl KCO 32 811 end 812 if eq t BCO64 813 bloc sl KCOP q z 814 tset sl KCO 64 815 end 816 if eq t BSTZ2 817 quit 33 eunsup 818 end 819 if eq t BSENC 820 tset sl KENC 1 821 quit 31 eenc 822 end 823 q = add q z 824 end 825 breq sl KSDS 826 breq sl KSTTS 827 breq sl KSCS 828 breq sl KSZS 829 breq sl KCOS 830 bstsd f sl 831 bstsz f sl 832 bstts f sl 833 bctts f sl 834 bstss f sl 835 bstco f sl 836 bstsc f sl 837 put ns 0 add tg sl KSB tg sl KNS 838 end 839 840 note bstsd (8.5.2): FullBox; entry_count (32), exactly 1 (0: 22, more: 33); the entry is a box 841 note (8.5.2.2 SampleEntry: 6 reserved bytes, data_reference_index (16), format fields). Protected 842 note or restricted entry types refuse 31. data_reference_index is 1 .. the dref entry count. The 843 note whole stsd box is at most SDCAP bytes. KCC is the entry type. 844 fn bstsd f ulong sl ulong to void 845 if gt tg sl KSDS SDCAP 846 quit 34 ebound 847 end 848 q = bopen f sl KSDP 849 e = add tg sl KSDP tg sl KSDS 850 bneed 8 get bhd 3 851 c = rd32 f add q 4 852 if eq c 0 853 quit 22 emiss 854 end 855 if gt c 1 856 quit 33 eunsup 857 end 858 o = add q 8 859 bhdr f o e 860 t = get bhd 0 861 h = get bhd 1 862 if eq benct t 1 863 tset sl KENC 1 864 quit 31 eenc 865 end 866 bneed 8 get bhd 3 867 r = rd16 f add add o h 6 868 if eq r 0 869 quit 20 ebox 870 end 871 d = get bdrc 0 872 if ne d 0 873 if gt r d 874 quit 20 ebox 875 end 876 end 877 tset sl KCC t 878 end 879 880 note bstsz (8.7.3.2): FullBox; sample_size (32); sample_count (32); when sample_size is 0, 881 note sample_count entry_size values (32) follow. KNS is sample_count, KSB the first global sample. 882 fn bstsz f ulong sl ulong to void 883 o = bopen f sl KSZP 884 bneed 12 get bhd 3 885 k = rd32 f add o 4 886 c = rd32 f add o 8 887 if eq k 0 888 if gt c div sub get bhd 3 12 4 889 quit 23 etable 890 end 891 end 892 b = get ns 0 893 if gt c sub NS b 894 quit 34 ebound 895 end 896 tset sl KNS c 897 tset sl KSB b 898 o = add o 12 899 for i ulong c bound `1048576` 900 x = k 901 if eq k 0 902 x = rd32 f o 903 o = add o 4 904 end 905 put ssz add b i uint x 906 end 907 end 908 909 note bstts (8.6.1.2): FullBox; entry_count (32); entries sample_count (32), sample_delta (32). The 910 note counts add up to the stsz sample count; DT(n+1) = DT(n) + delta(n), DT(0) = 0. 911 fn bstts f ulong sl ulong to void 912 o = bopen f sl KSTTP 913 bneed 8 get bhd 3 914 c = rd32 f add o 4 915 bcnt c 8 916 o = add o 8 917 n = tg sl KNS 918 b = tg sl KSB 919 j = ulong 0 920 put bdt 0 0 921 for i ulong c bound `1048576` 922 a = rd32 f o 923 if gt a sub n j 924 quit 23 etable 925 end 926 bdts add b j a rd32 f add o 4 927 j = add j a 928 o = add o 8 929 end 930 if ne j n 931 quit 23 etable 932 end 933 end 934 935 note bdts: global samples s .. s + a - 1 get duration x and decode times from bdt; bdt advances. 936 fn bdts s ulong a ulong x ulong to void 937 t = get bdt 0 938 for k ulong a bound `1048576` 939 put sdts add s k t 940 put sdur add s k uint x 941 if gt x sub `18446744073709551615` t 942 quit 23 etable 943 end 944 t = add t x 945 end 946 put bdt 0 t 947 end 948 949 note bctts (8.6.1.3): FullBox (version 0 or 1); entry_count (32); entries sample_count (32), 950 note sample_offset (version 0 unsigned 32, version 1 signed 32); CT(n) = DT(n) + offset(n). The 951 note counts add up to the sample count. No ctts: every offset is 0 (KCTTS 0). 952 fn bctts f ulong sl ulong to void 953 n = tg sl KNS 954 b = tg sl KSB 955 if eq tg sl KCTS 0 956 bcto b n 0 957 ret 958 end 959 o = bopen f sl KCTP 960 bneed 8 get bhd 3 961 v = rd8 f o 962 if gt v 1 963 quit 20 ebox 964 end 965 tset sl KCTTS 1 966 tset sl KCTV v 967 c = rd32 f add o 4 968 bcnt c 8 969 o = add o 8 970 j = ulong 0 971 for i ulong c bound `1048576` 972 a = rd32 f o 973 if gt a sub n j 974 quit 23 etable 975 end 976 m = bitcast long rd32 f add o 4 977 if eq v 1 978 m = bsx32 rd32 f add o 4 979 end 980 bcto add b j a m 981 j = add j a 982 o = add o 8 983 end 984 if ne j n 985 quit 23 etable 986 end 987 end 988 989 note bcto: global samples s .. s + a - 1 get composition offset m. 990 fn bcto s ulong a ulong m long to void 991 for k ulong a bound `1048576` 992 put scto add s k m 993 end 994 end 995 996 note bsyn: global samples s .. s + a - 1 get sync flag y. 997 fn bsyn s ulong a ulong y uchar to void 998 for k ulong a bound `1048576` 999 put ssync add s k y 1000 end 1001 end 1002 1003 note bstss (8.6.2): FullBox; entry_count (32); sample_number (32, 1-based) values in strictly 1004 note increasing order, each at most the sample count. No stss: every sample is a sync sample. 1005 fn bstss f ulong sl ulong to void 1006 n = tg sl KNS 1007 b = tg sl KSB 1008 if eq tg sl KSSS 0 1009 bsyn b n 1 1010 tset sl KNSYN n 1011 ret 1012 end 1013 bsyn b n 0 1014 tset sl KSTSS 1 1015 o = bopen f sl KSSP 1016 bneed 8 get bhd 3 1017 c = rd32 f add o 4 1018 bcnt c 4 1019 if gt c n 1020 quit 23 etable 1021 end 1022 o = add o 8 1023 l = ulong 0 1024 for k ulong c bound `1048576` 1025 x = rd32 f o 1026 if le x l 1027 quit 23 etable 1028 end 1029 if gt x n 1030 quit 23 etable 1031 end 1032 put ssync add b sub x 1 1 1033 l = x 1034 o = add o 4 1035 end 1036 tset sl KNSYN c 1037 end 1038 1039 note bstco (8.7.5): FullBox; entry_count (32); chunk_offset values of 32 bits (stco) or 64 bits 1040 note (co64), file offsets, read into bch. KNCH is the chunk count (at most NC). 1041 fn bstco f ulong sl ulong to void 1042 o = bopen f sl KCOP 1043 bneed 8 get bhd 3 1044 c = rd32 f add o 4 1045 w = ulong 4 1046 if eq tg sl KCO 64 1047 w = 8 1048 end 1049 bcnt c w 1050 if gt c NC 1051 quit 34 ebound 1052 end 1053 o = add o 8 1054 for i ulong c bound `1048576` 1055 x = ulong 0 1056 if eq w 8 1057 x = rd64 f o 1058 else 1059 x = rd32 f o 1060 end 1061 put bch i x 1062 o = add o w 1063 end 1064 tset sl KNCH c 1065 end 1066 1067 note bstsc (8.7.4): FullBox; entry_count (32); entries first_chunk (32, 1-based), 1068 note samples_per_chunk (32), sample_description_index (32). The first entry's first_chunk is 1, they 1069 note increase strictly and stay within the chunk count; each entry covers its first_chunk up to the 1070 note next entry's first_chunk - 1, the last one through the last chunk. The samples, in decode 1071 note order, fill the chunks in order and must number exactly the sample count. The only sample 1072 note description index allowed is 1 (bstsd accepts one entry). 1073 fn bstsc f ulong sl ulong to void 1074 o = bopen f sl KSCP 1075 bneed 8 get bhd 3 1076 c = rd32 f add o 4 1077 bcnt c 12 1078 o = add o 8 1079 put bsc 0 0 1080 put bsc 1 0 1081 put bsc 2 0 1082 for i ulong c bound `1048576` 1083 bsce f sl o i 1084 o = add o 12 1085 end 1086 if gt c 0 1087 brun sl sub get bsc 1 1 tg sl KNCH get bsc 2 1088 end 1089 if ne get bsc 0 tg sl KNS 1090 quit 23 etable 1091 end 1092 end 1093 1094 note bsce: stsc entry i at o. Its first_chunk is checked (1 for the first entry, above the previous 1095 note one, at most the chunk count) before the previous entry's chunks are laid out. 1096 fn bsce f ulong sl ulong o ulong i ulong to void 1097 a = rd32 f o 1098 if ne rd32 f add o 8 1 1099 quit 23 etable 1100 end 1101 if gt a tg sl KNCH 1102 quit 23 etable 1103 end 1104 l = get bsc 1 1105 if eq i 0 1106 if ne a 1 1107 quit 23 etable 1108 end 1109 else 1110 if le a l 1111 quit 23 etable 1112 end 1113 brun sl sub l 1 sub a 1 get bsc 2 1114 end 1115 put bsc 1 a 1116 put bsc 2 rd32 f add o 4 1117 end 1118 1119 note brun: chunks c0 .. c1 - 1 (0-based, c1 <= KNCH) of slot sl hold m samples each, from track 1120 note sample bsc 0, which advances. Each chunk with samples must lie inside one mdat payload. 1121 fn brun sl ulong c0 ulong c1 ulong m ulong to void 1122 b = tg sl KSB 1123 n = tg sl KNS 1124 k = get bsc 0 1125 for c ulong sub c1 c0 bound `1048576` 1126 if gt m sub n k 1127 quit 23 etable 1128 end 1129 o = get bch add c0 c 1130 if gt m 0 1131 bmdin o bchunk add b k m o 1132 end 1133 k = add k m 1134 end 1135 put bsc 0 k 1136 end 1137 1138 note bchunk: global samples s .. s + m - 1 lie back to back from file offset o (8.7.4.1: a sample's 1139 note offset is its chunk offset plus the sizes of the earlier samples of the chunk); the end offset. 1140 fn bchunk s ulong m ulong o ulong to ulong 1141 a = o 1142 for i ulong m bound `1048576` 1143 x = ulong get ssz add s i 1144 put soff add s i a 1145 if gt x sub `18446744073709551615` a 1146 quit 23 etable 1147 end 1148 a = add a x 1149 end 1150 ret a 1151 end 1152 1153 note bmdin: the bytes [a, z) of one chunk must lie inside one mdat payload (binary search over the 1154 note ascending payload starts). The samples of a chunk are contiguous and the header of the next box 1155 note separates two payloads, so this is the same as checking every sample of the chunk. 1156 fn bmdin a ulong z ulong to void 1157 lo = ulong 0 1158 hi = get bnmd 0 1159 while lt lo hi bound 64 1160 m = div add lo hi 2 1161 if le get bmdp m a 1162 lo = add m 1 1163 else 1164 hi = m 1165 end 1166 end 1167 if eq lo 0 1168 quit 23 etable 1169 end 1170 if gt z get bmde sub lo 1 1171 quit 23 etable 1172 end 1173 end