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/plan.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 / plan.om` 641 lines ยท 14183 bytes Raw Blame History 1 note omega-mp4 src/plan.om: the output plan for trim and join (writer owner, names p*/P*). 2 note ptrim and pjoin read only what bparse stored (io.om representation), check every value they 3 note use, and fill the plan below. Every refusal happens here or in write.om before the output is 4 note created (wcreate), so a refused plan never leaves a file. 5 note 6 note Time model (14496-12 8.6.5, 8.6.6). Movie time is in the mvhd timescale (8.2.2), media time in 7 note the mdhd timescale (8.4.2). A track is presented over the movie interval [KE0, KE0 + KED); at 8 note movie time KE0 + u it shows media time KMT + u * mdhd_timescale / mvhd_timescale. A sample's 9 note composition (presentation) time is its decode time plus its composition offset (8.6.1.3). 10 note 11 note Output plan: per output track 64 ulong fields in pt (P* constants), its samples as indices into 12 note the parsed sample arrays (pidx, with the input file in pfil), its edits in pel/pem, and the 13 note mdat layout (pord: output samples in file order; pofs: byte offset of each sample in mdat). 14 note One sample per chunk (8.7.4): the writer emits one stsc entry {1, 1, 1} for every track. 15 16 const PSLOT ulong 0 17 const PSB ulong 1 18 const PNS ulong 2 19 const PTS ulong 3 20 const PNE ulong 4 21 const PMDUR ulong 5 22 const PTKD ulong 6 23 const PCTV ulong 7 24 const PNSYN ulong 8 25 const PSZ ulong 9 26 27 note PSLOT source track slot whose tkhd, mdhd, hdlr, media header and stsd are written 28 note PSB first output sample (index into pidx); PNS output sample count; PTS media timescale 29 note PNE output edit count; PMDUR sum of sample durations (media ticks, mdhd duration) 30 note PTKD sum of output edit durations (movie ticks, tkhd duration, 8.3.2) 31 note PCTV 0 no ctts, 1 ctts version 0, 2 ctts version 1 (8.6.1.3); PNSYN sync samples (8.6.2) 32 note PSZ mdat bytes of the track 33 global pt ulong 512 34 global pnt ulong 1 35 global pidx ulong `1048576` 36 global pfil uchar `1048576` 37 global pn ulong 1 38 global pel ulong 512 39 global pem long 512 40 global pmts ulong 1 41 global pmdur ulong 1 42 global pmf ulong 1 43 global pord ulong `1048576` 44 global pofs ulong `1048576` 45 global ppay ulong 1 46 global pcur ulong 8 47 global pdts ulong 8 48 global pca uchar 65536 49 global pcb uchar 65536 50 51 fn pget t ulong k ulong to ulong 52 ret get pt add mul t 64 k 53 end 54 55 fn pset t ulong k ulong v ulong to void 56 put pt add mul t 64 k v 57 end 58 59 note ---- checked arithmetic (unsigned wrap is refused, signed conversion is checked first) ---- 60 fn padd a ulong c ulong to ulong 61 if gt a sub `18446744073709551615` c 62 quit 34 ebound 63 end 64 ret add a c 65 end 66 67 fn pmul a ulong c ulong to ulong 68 if ne a 0 69 if gt c div `18446744073709551615` a 70 quit 34 ebound 71 end 72 end 73 ret mul a c 74 end 75 76 fn plong v ulong to long 77 if gt v `9223372036854775807` 78 quit 34 ebound 79 end 80 ret long v 81 end 82 83 fn pladd a long c long to long 84 if gt c 0 85 if gt a sub `9223372036854775807` c 86 quit 34 ebound 87 end 88 end 89 if lt c 0 90 if lt a sub -`9223372036854775808` c 91 quit 34 ebound 92 end 93 end 94 ret add a c 95 end 96 97 note pmd: x * y / z rounded down (up false) or up (up true), exactly, without a 128-bit product: 98 note x = q*z + r gives x*y/z = q*y + r*y/z, and r*y < z*y fits because timescales are 32-bit. 99 fn pmd x ulong y ulong z ulong up bool to ulong 100 if eq z 0 101 quit 23 etable 102 end 103 q = div x z 104 r = rem x z 105 lo = pmul r y 106 v = padd pmul q y div lo z 107 if up 108 if ne rem lo z 0 109 v = padd v 1 110 end 111 end 112 ret v 113 end 114 115 note pct: composition time of parsed sample g = decode time + composition offset (8.6.1.3). 116 fn pct g ulong to long 117 ret pladd plong get sdts g get scto g 118 end 119 120 note prange: a track slot's samples lie inside the parsed arrays and its timescale is a nonzero 121 note 32-bit value (8.4.2 declares timescale unsigned int(32)). 122 note A complex edit list (KEDM 2: several media edits, 8.6.6) is refused 33 for trim and join. 123 fn prange s ulong to void 124 e = padd tg s KSB tg s KNS 125 if gt e get ns 0 126 quit 23 etable 127 end 128 ts = tg s KTS 129 if eq ts 0 130 quit 23 etable 131 end 132 if gt ts `4294967295` 133 quit 23 etable 134 end 135 if eq tg s KKEEP 0 136 quit 32 eother 137 end 138 if eq tg s KEDM 2 139 quit 33 eunsup 140 end 141 end 142 143 fn pinit f ulong to void 144 put pnt 0 0 145 put pn 0 0 146 put pmdur 0 0 147 put ppay 0 0 148 put pmf 0 f 149 m = fget f FMTS 150 if eq m 0 151 quit 23 etable 152 end 153 if gt m `4294967295` 154 quit 23 etable 155 end 156 put pmts 0 m 157 end 158 159 fn pnew s ulong to ulong 160 t = get pnt 0 161 if ge t NTF 162 quit 34 ebound 163 end 164 for k ulong 64 165 pset t k 0 166 end 167 pset t PSLOT s 168 pset t PSB get pn 0 169 pset t PTS tg s KTS 170 put pnt 0 add t 1 171 ret t 172 end 173 174 note pedit: append an output edit (8.6.6): duration in movie ticks, media time m (-1 = empty edit). 175 fn pedit t ulong d ulong m long to void 176 e = pget t PNE 177 if ge e NE 178 quit 34 ebound 179 end 180 put pel add mul t 64 e d 181 put pem add mul t 64 e m 182 pset t PNE add e 1 183 end 184 185 note ptake: append samples [x0, x1) of track slot s, in decode order, to output track t. 186 fn ptake t ulong s ulong x0 ulong x1 ulong to void 187 f = tg s KFILE 188 gs = tg s KSB 189 m = sub x1 x0 190 for j ulong m bound `1048576` 191 g = add gs add x0 j 192 k = get pn 0 193 if ge k NS 194 quit 34 ebound 195 end 196 put pidx k g 197 put pfil k uchar f 198 put pn 0 add k 1 199 pset t PNS add pget t PNS 1 200 pset t PMDUR padd pget t PMDUR ulong get sdur g 201 pset t PSZ padd pget t PSZ ulong get ssz g 202 end 203 end 204 205 note ---- trim ---- 206 note pfirst: first decode sample to keep. whole (the trim starts at or before the track's 207 note presentation): sample 0, so encoder priming and leading samples stay. Otherwise c is the 208 note sample shown at media time mta (largest composition time <= mta). Audio keeps one packet 209 note before c as decoder pre-roll (14496-12 10.1 roll recovery; AAC frames overlap their 210 note predecessor); every audio sample is a sync sample here. Other tracks start at the latest sync 211 note sample (8.6.2) at or before c in decode order whose own composition time is <= mta, so every 212 note sample presented from mta on is decoded from that sync sample forward, including reordered 213 note (B) frames that decode after a later sync sample. No such sample: start at 0. 214 fn pfirst s ulong mta ulong whole bool to ulong 215 if whole 216 ret 0 217 end 218 gs = tg s KSB 219 n = tg s KNS 220 lim = plong mta 221 c = n 222 cbest = long 0 223 for j ulong n bound `1048576` 224 ct = pct add gs j 225 if le ct lim 226 if eq c n 227 c = j 228 cbest = ct 229 end 230 if gt ct cbest 231 c = j 232 cbest = ct 233 end 234 end 235 end 236 if eq c n 237 ret 0 238 end 239 if eq tg s KHDLR 0x736f756e 240 if gt c 0 241 ret sub c 1 242 end 243 ret 0 244 end 245 k = add c 1 246 for j ulong k bound `1048576` 247 x = sub c j 248 if ne get ssync add gs x 0 249 if le pct add gs x lim 250 ret x 251 end 252 end 253 end 254 ret 0 255 end 256 257 note plast: end (exclusive) of the contiguous decode run. whole (the trim ends at or after the 258 note track's presentation end): every sample, so the tail stays. Otherwise one past the last 259 note decode sample from x0 on whose composition time is below mtb; the run is contiguous in 260 note decode order, so every reference a kept sample needs (earlier in decode order) is kept. 261 fn plast s ulong x0 ulong mtb ulong whole bool to ulong 262 n = tg s KNS 263 if le n x0 264 quit 36 eempty 265 end 266 if whole 267 ret n 268 end 269 gs = tg s KSB 270 lim = plong mtb 271 e = x0 272 m = sub n x0 273 for j ulong m bound `1048576` 274 if lt pct add gs add x0 j lim 275 e = add add x0 j 1 276 end 277 end 278 if eq e x0 279 quit 36 eempty 280 end 281 ret e 282 end 283 284 note pcut: output track for slot s presented over movie [a, b) of the trim [start, ...). 285 note Media time at a is rounded down and at b up: composition times are integers, so the sample 286 note shown at movie time a is the same as at the exact (fractional) media time, and the output 287 note edit keeps the exact movie-tick START (empty edit a - start, then b - a ticks of media). 288 note The output decode timeline starts at the first kept sample (decode time r0 becomes 0), so 289 note the edit's media time is mta - r0; negative (a composition offset before the first decoded 290 note sample) is refused 33. 291 fn pmt s ulong x ulong up bool to ulong 292 d = sub x tg s KE0 293 ret padd tg s KMT pmd d tg s KTS get pmts 0 up 294 end 295 296 note pend: end of the kept decode run of slot s for a trim ending at movie time b. 297 fn pend s ulong x0 ulong b ulong to ulong 298 we = padd tg s KE0 tg s KED 299 mtb = pmt s b true 300 ret plast s x0 mtb eq b we 301 end 302 303 note pmedia: the output edit's media time, mta less the first kept sample's decode time. 304 fn pmedia s ulong x0 ulong mta ulong to long 305 d0 = plong get sdts add tg s KSB x0 306 m = sub plong mta d0 307 if lt m 0 308 quit 33 eunsup 309 end 310 ret m 311 end 312 313 fn pcut s ulong a ulong b ulong start ulong to void 314 mta = pmt s a false 315 x0 = pfirst s mta eq a tg s KE0 316 x1 = pend s x0 b 317 t = pnew s 318 ptake t s x0 x1 319 m = pmedia s x0 mta 320 if gt a start 321 pedit t sub a start -1 322 end 323 pedit t sub b a m 324 pset t PTKD sub b start 325 end 326 327 fn ptrack s ulong start ulong finish ulong to void 328 prange s 329 ws = tg s KE0 330 we = padd ws tg s KED 331 a = start 332 if gt ws a 333 a = ws 334 end 335 b = finish 336 if lt we b 337 b = we 338 end 339 if ge a b 340 quit 36 eempty 341 end 342 pcut s a b start 343 end 344 345 note ptrim: START and END in movie ticks of file f. Refused 35 unless START < END and START is 346 note below the movie duration; END may pass the duration (each track ends at its own end). 347 note Every track is kept (main refuses 32 for non-media handlers; prange checks KKEEP too). 348 fn ptrim f ulong start ulong finish ulong to void 349 pinit f 350 if ge start finish 351 quit 35 erange 352 end 353 if ge start fget f FMDUR 354 quit 35 erange 355 end 356 nt = fget f FNTK 357 if eq nt 0 358 quit 22 emiss 359 end 360 if gt nt NTF 361 quit 34 ebound 362 end 363 base = fget f FTK0 364 for t ulong nt bound 8 365 ptrack add base t start finish 366 end 367 pfin 368 end 369 370 note ---- join ---- 371 note pconv: movie ticks of file k to output movie ticks (file 0's mvhd timescale), exactly; 372 note an inexact conversion is refused 43 (timescales differ). 373 fn pconv k ulong v ulong to ulong 374 mk = fget k FMTS 375 m0 = get pmts 0 376 if eq mk m0 377 ret v 378 end 379 lo = pmd v m0 mk false 380 if ne lo pmd v m0 mk true 381 quit 43 ejts 382 end 383 ret lo 384 end 385 386 note plen: the presentation length of file k: its movie duration or its longest track 387 note presentation (KE0 + KED), whichever is longer, in output movie ticks. 388 fn plen k ulong to ulong 389 v = fget k FMDUR 390 s0 = fget k FTK0 391 for t ulong fget k FNTK bound 8 392 e = padd tg add s0 t KE0 tg add s0 t KED 393 if gt e v 394 v = e 395 end 396 end 397 ret pconv k v 398 end 399 400 note pdiff: true when the two sample description boxes (stsd, 8.5.2) are not byte-equal. 401 fn pdiff a ulong c ulong to bool 402 n = tg a KSDS 403 if ne n tg c KSDS 404 ret true 405 end 406 if gt n SDCAP 407 quit 34 ebound 408 end 409 o = ulong 0 410 while lt o n bound `1048576` 411 m = sub n o 412 if gt m 65536 413 m = 65536 414 end 415 pldsa a o m 416 pldsb c o m 417 if pcmp m 418 ret true 419 end 420 o = add o m 421 end 422 ret false 423 end 424 425 note pldsa / pldsb: m bytes of slot s's stsd from offset o into pca / pcb. 426 fn pldsa s ulong o ulong m ulong to void 427 p = plong padd tg s KSDP o 428 r = pread get ffd tg s KFILE pca 0 m p 429 if ne r plong m 430 quit 60 eio 431 end 432 end 433 434 fn pldsb s ulong o ulong m ulong to void 435 p = plong padd tg s KSDP o 436 r = pread get ffd tg s KFILE pcb 0 m p 437 if ne r plong m 438 quit 60 eio 439 end 440 end 441 442 fn pcmp m ulong to bool 443 for j ulong m bound 65536 444 if ne get pca j get pcb j 445 ret true 446 end 447 end 448 ret false 449 end 450 451 note pjcheck: track s of a later input must match track s0 of input 0: handler 41, media 452 note timescale 43, sample description bytes 42 (the copied stsd describes every part). 453 fn pjcheck s0 ulong s ulong to void 454 prange s 455 if ne tg s KHDLR tg s0 KHDLR 456 quit 41 ejhdlr 457 end 458 if ne tg s KTS tg s0 KTS 459 quit 43 ejts 460 end 461 if pdiff s0 s 462 quit 42 ejdesc 463 end 464 end 465 466 note pjpart: append part k (track slot s) starting at output movie time p after the edits that 467 note end at cur. Its own edit is kept: an empty edit fills any gap, then its media edit moves by 468 note the media already written (PMDUR) less the part's first decode time. Returns the new end. 469 fn pjpart tn ulong s ulong k ulong p ulong cur ulong to ulong 470 st = padd p pconv k tg s KE0 471 if lt st cur 472 quit 23 etable 473 end 474 if gt st cur 475 pedit tn sub st cur -1 476 end 477 ed = pconv k tg s KED 478 d0 = ulong 0 479 if gt tg s KNS 0 480 d0 = get sdts tg s KSB 481 end 482 m = sub plong padd pget tn PMDUR tg s KMT plong d0 483 if lt m 0 484 quit 33 eunsup 485 end 486 if gt ed 0 487 pedit tn ed m 488 end 489 ptake tn s 0 tg s KNS 490 ret padd st ed 491 end 492 493 fn pjtrack count ulong t ulong to void 494 s0 = add fget 0 FTK0 t 495 prange s0 496 tn = pnew s0 497 cur = ulong 0 498 p = ulong 0 499 for k ulong count bound 16 500 s = add fget k FTK0 t 501 pjcheck s0 s 502 cur = pjpart tn s k p cur 503 p = padd p plen k 504 end 505 pset tn PTKD cur 506 end 507 508 note pjoin: concatenate already parsed inputs [0, count). Tracks pair by position; every input 509 note must have the same track count (40). Output headers come from input 0. 510 fn pjoin count ulong to void 511 if lt count 1 512 quit 1 eusage 513 end 514 if gt count NF 515 quit 34 ebound 516 end 517 pinit 0 518 nt = fget 0 FNTK 519 if eq nt 0 520 quit 22 emiss 521 end 522 if gt nt NTF 523 quit 34 ebound 524 end 525 for k ulong count bound 16 526 if ne fget k FNTK nt 527 quit 40 ejcount 528 end 529 end 530 for t ulong nt bound 8 531 pjtrack count t 532 end 533 pfin 534 end 535 536 note ---- finish: ctts version, sync count, movie duration, mdat order ---- 537 note pscan: ctts (8.6.1.3) version 0 holds unsigned offsets, version 1 signed; both 32-bit. 538 note Offsets that do not fit are refused 33. All offsets zero: no ctts. 539 fn pscan t ulong to void 540 q0 = pget t PSB 541 n = pget t PNS 542 lo = long 0 543 hi = long 0 544 nsyn = ulong 0 545 for j ulong n bound `1048576` 546 g = get pidx add q0 j 547 c = get scto g 548 if lt c lo 549 lo = c 550 end 551 if gt c hi 552 hi = c 553 end 554 if ne get ssync g 0 555 nsyn = add nsyn 1 556 end 557 end 558 pset t PNSYN nsyn 559 pset t PCTV 0 560 if lt lo 0 561 if lt lo -`2147483648` 562 quit 33 eunsup 563 end 564 if gt hi `2147483647` 565 quit 33 eunsup 566 end 567 pset t PCTV 2 568 ret 569 end 570 if gt hi 0 571 if gt hi `4294967295` 572 quit 33 eunsup 573 end 574 pset t PCTV 1 575 end 576 end 577 578 note pkey: decode time as seconds * 2^20 (saturating), for interleaving tracks in mdat. 579 fn pkey d ulong ts ulong to ulong 580 s = div d ts 581 if gt s `1099511627775` 582 s = `1099511627775` 583 end 584 ret add shl s 20 div shl rem d ts 20 ts 585 end 586 587 fn pnext to ulong 588 nt = get pnt 0 589 sel = nt 590 sk = ulong 0 591 for t ulong nt bound 8 592 if lt get pcur t pget t PNS 593 kv = pkey get pdts t pget t PTS 594 if eq sel nt 595 sel = t 596 sk = kv 597 end 598 if lt kv sk 599 sel = t 600 sk = kv 601 end 602 end 603 end 604 if eq sel nt 605 quit 23 etable 606 end 607 ret sel 608 end 609 610 note porder: mdat order interleaves tracks by decode time (one sample per chunk, 8.7.4); pofs 611 note is each sample's offset from the first mdat payload byte. 612 fn porder to void 613 for t ulong 8 614 put pcur t 0 615 put pdts t 0 616 end 617 ofs = ulong 0 618 for k ulong get pn 0 bound `1048576` 619 t = pnext 620 j = add pget t PSB get pcur t 621 g = get pidx j 622 put pord k j 623 put pofs j ofs 624 ofs = padd ofs ulong get ssz g 625 put pdts t padd get pdts t ulong get sdur g 626 put pcur t add get pcur t 1 627 end 628 put ppay 0 ofs 629 end 630 631 fn pfin to void 632 md = ulong 0 633 for t ulong get pnt 0 bound 8 634 pscan t 635 if gt pget t PTKD md 636 md = pget t PTKD 637 end 638 end 639 put pmdur 0 md 640 porder 641 end