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/write.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 / write.om` 595 lines ยท 11149 bytes Raw Blame History 1 note omega-mp4 src/write.om: woutput writes the plan of plan.om as one new file (writer owner, 2 note names w*). Layout: ftyp (copied from the first input, 14496-12 4.3), moov (built fresh in mb, 3 note 8.2.1) before mdat (8.1.1), so the file plays progressively. Every sample is copied unchanged 4 note from its input; nothing is re-encoded. The whole moov is built, and every refusal made, 5 note before wcreate creates the output (O_EXCL); a later I/O failure calls quit, which removes it. 6 note 7 note Boxes written (14496-12 sections; QuickTime File Format names the same atoms): 8 note 4.2 box header: 32-bit size, or size 1 with a 64-bit largesize (used for mdat over 4 GiB) 9 note 8.2.2 mvhd, 8.3.2 tkhd, 8.4.2 mdhd: version 1 (64-bit times) only when a value needs it 10 note 8.4.3 hdlr, 8.4.5 vmhd/smhd media header, 8.5.2 stsd: copied byte for byte from the source 11 note 8.7.1 dinf, 8.7.2 dref: one self-contained entry ('url ' flags 1; QuickTime 'alis' flags 1) 12 note 8.6.5 edts, 8.6.6 elst: version 1 only when a duration or media time needs 64 bits 13 note 8.6.1.2 stts, 8.6.1.3 ctts, 8.6.2 stss, 8.7.3.2 stsz, 8.7.4 stsc (one entry {1, 1, 1}: one 14 note sample per chunk), 8.7.5 stco, or co64 when any chunk offset passes 2^32 - 1. 15 note Not written: udta, tref, meta and other optional boxes of the input. 16 17 global wsb uchar 4096 18 global wsn ulong 1 19 global wco ulong 1 20 global wbase ulong 1 21 global wqt ulong 1 22 23 note ---- the moov buffer mb (io.om) ---- 24 fn wm8 v ulong to void 25 n = get mbn 0 26 if ge n MBCAP 27 quit 34 ebound 28 end 29 put mb n uchar and v 255 30 put mbn 0 add n 1 31 end 32 33 fn wm32 v ulong to void 34 wm8 shr v 24 35 wm8 shr v 16 36 wm8 shr v 8 37 wm8 v 38 end 39 40 fn wm64 v ulong to void 41 wm32 shr v 32 42 wm32 v 43 end 44 45 fn wmput p ulong v ulong to void 46 put mb p uchar and shr v 24 255 47 put mb add p 1 uchar and shr v 16 255 48 put mb add p 2 uchar and shr v 8 255 49 put mb add p 3 uchar and v 255 50 end 51 52 note wmbox: open a box (4.2); wmend writes its 32-bit size (moov is at most 16 MiB). 53 fn wmbox ty ulong to ulong 54 p = get mbn 0 55 wm32 0 56 wm32 ty 57 ret p 58 end 59 60 note wmfull: open a full box (4.2 FullBox: 8-bit version, 24-bit flags). 61 fn wmfull ty ulong ver ulong fg ulong to ulong 62 p = wmbox ty 63 wm32 or shl ver 24 and fg `16777215` 64 ret p 65 end 66 67 fn wmend p ulong to void 68 wmput p sub get mbn 0 p 69 end 70 71 note wmcopy: n bytes of input f at p appended to mb unchanged. 72 fn wmcopy f ulong p ulong n ulong to void 73 m = get mbn 0 74 if gt n sub MBCAP m 75 quit 34 ebound 76 end 77 if gt padd p n fget f FSZ 78 quit 20 ebox 79 end 80 r = pread get ffd f mb m n plong p 81 if ne r plong n 82 quit 60 eio 83 end 84 put mbn 0 add m n 85 end 86 87 note ---- a source header box read into wsb ---- 88 fn wrd f ulong p ulong n ulong to void 89 if lt n 8 90 quit 20 ebox 91 end 92 if gt n 4096 93 quit 20 ebox 94 end 95 if gt padd p n fget f FSZ 96 quit 20 ebox 97 end 98 r = pread get ffd f wsb 0 n plong p 99 if ne r plong n 100 quit 60 eio 101 end 102 put wsn 0 n 103 end 104 105 fn wg8 o ulong to ulong 106 if ge o get wsn 0 107 quit 20 ebox 108 end 109 ret ulong get wsb o 110 end 111 112 fn wg32 o ulong to ulong 113 ret or or shl wg8 o 24 shl wg8 add o 1 16 or shl wg8 add o 2 8 wg8 add o 3 114 end 115 116 fn wg64 o ulong to ulong 117 ret or shl wg32 o 32 wg32 add o 4 118 end 119 120 note whdr: header length of the box in wsb: 16 when size is 1 (largesize follows), else 8 (4.2). 121 fn whdr to ulong 122 if eq wg32 0 1 123 ret 16 124 end 125 ret 8 126 end 127 128 note wver: the version of the source full box in wsb; only 0 and 1 are defined (8.2.2, 8.3.2, 129 note 8.4.2); any other is refused 20. 130 fn wver h ulong to ulong 131 v = wg8 h 132 if gt v 1 133 quit 20 ebox 134 end 135 ret v 136 end 137 138 note wneed: version 1 when one of three times does not fit 32 bits. 139 fn wneed a ulong c ulong d ulong to ulong 140 if gt a `4294967295` 141 ret 1 142 end 143 if gt c `4294967295` 144 ret 1 145 end 146 if gt d `4294967295` 147 ret 1 148 end 149 ret 0 150 end 151 152 fn wtime ver ulong v ulong to void 153 if eq ver 1 154 wm64 v 155 ret 156 end 157 wm32 v 158 end 159 160 fn wmraw o ulong n ulong to void 161 for i ulong n bound 4096 162 wm8 wg8 add o i 163 end 164 end 165 166 note ---- headers ---- 167 note wmvhd (8.2.2): creation and modification times, rate, volume, matrix copied from the source 168 note mvhd; timescale is the source's; duration is the longest output track (tkhd duration); 169 note next_track_ID follows the renumbered tracks 1 .. N. 170 fn wmvhd to void 171 f = get pmf 0 172 wrd f fget f FMVP fget f FMVS 173 h = whdr 174 v = wver h 175 o = add h 4 176 ct = wg32 o 177 mt = wg32 add o 4 178 o = add o 16 179 if eq v 1 180 ct = wg64 add h 4 181 mt = wg64 add h 12 182 o = add add h 4 28 183 end 184 d = get pmdur 0 185 ver = wneed ct mt d 186 p = wmfull 0x6d766864 ver and wg32 h `16777215` 187 wtime ver ct 188 wtime ver mt 189 wm32 get pmts 0 190 wtime ver d 191 wmraw o 76 192 wm32 add get pnt 0 1 193 wmend p 194 end 195 196 note wtkhd (8.3.2): flags, times, layer, alternate group, volume, matrix, width and height 197 note copied; track_ID is t + 1; duration is the output edit total in movie ticks. 198 fn wtkhd t ulong to void 199 s = pget t PSLOT 200 f = tg s KFILE 201 wrd f tg s KTKP tg s KTKS 202 h = whdr 203 v = wver h 204 o = add h 4 205 ct = wg32 o 206 mt = wg32 add o 4 207 o = add o 20 208 if eq v 1 209 ct = wg64 add h 4 210 mt = wg64 add h 12 211 o = add add h 4 32 212 end 213 d = pget t PTKD 214 ver = wneed ct mt d 215 p = wmfull 0x746b6864 ver and wg32 h `16777215` 216 wtime ver ct 217 wtime ver mt 218 wm32 add t 1 219 wm32 0 220 wtime ver d 221 wmraw o 60 222 wmend p 223 end 224 225 note wmdhd (8.4.2): times and language copied; timescale is the source's; duration is the sum of 226 note the output sample durations. 227 fn wmdhd t ulong to void 228 s = pget t PSLOT 229 f = tg s KFILE 230 wrd f tg s KMDHP tg s KMDHS 231 h = whdr 232 v = wver h 233 o = add h 4 234 ct = wg32 o 235 mt = wg32 add o 4 236 o = add o 16 237 if eq v 1 238 ct = wg64 add h 4 239 mt = wg64 add h 12 240 o = add add h 4 28 241 end 242 d = pget t PMDUR 243 ver = wneed ct mt d 244 p = wmfull 0x6d646864 ver and wg32 h `16777215` 245 wtime ver ct 246 wtime ver mt 247 wm32 pget t PTS 248 wtime ver d 249 wmraw o 4 250 wmend p 251 end 252 253 note welst (8.6.5, 8.6.6): the plan's edits; media rate 1.0 (integer 1, fraction 0); -1 is an 254 note empty edit. 255 fn welst t ulong to void 256 k = pget t PNE 257 if eq k 0 258 ret 259 end 260 b = mul t 64 261 ver = ulong 0 262 for e ulong k bound 64 263 if gt get pel add b e `4294967295` 264 ver = 1 265 end 266 if gt get pem add b e `2147483647` 267 ver = 1 268 end 269 end 270 pe = wmbox 0x65647473 271 p = wmfull 0x656c7374 ver 0 272 wm32 k 273 for e ulong k bound 64 274 d = get pel add b e 275 m = bitcast ulong get pem add b e 276 wtime ver d 277 if eq ver 1 278 wm64 m 279 end 280 if eq ver 0 281 wm32 and m `4294967295` 282 end 283 wm32 65536 284 end 285 wmend p 286 wmend pe 287 end 288 289 note wdinf (8.7.1, 8.7.2): one data reference with flags 1 (media in this file). QuickTime 290 note files (major brand 'qt ') get an 'alis' entry, others 'url '. 291 fn wdinf to void 292 pd = wmbox 0x64696e66 293 pr = wmfull 0x64726566 0 0 294 wm32 1 295 ty = ulong 0x75726c20 296 if eq get wqt 0 1 297 ty = 0x616c6973 298 end 299 pu = wmfull ty 0 1 300 wmend pu 301 wmend pr 302 wmend pd 303 end 304 305 note ---- sample tables ---- 306 fn wsamp t ulong j ulong to ulong 307 ret get pidx add pget t PSB j 308 end 309 310 note wstts (8.6.1.2): run-length sample durations. 311 fn wstts t ulong to void 312 n = pget t PNS 313 p = wmfull 0x73747473 0 0 314 pc = get mbn 0 315 wm32 0 316 cnt = ulong 0 317 rn = ulong 0 318 lv = ulong 0 319 for j ulong n bound `1048576` 320 d = ulong get sdur wsamp t j 321 if gt rn 0 322 if eq d lv 323 rn = add rn 1 324 continue 325 end 326 wm32 rn 327 wm32 lv 328 cnt = add cnt 1 329 end 330 lv = d 331 rn = 1 332 end 333 if gt rn 0 334 wm32 rn 335 wm32 lv 336 cnt = add cnt 1 337 end 338 wmput pc cnt 339 wmend p 340 end 341 342 note wctts (8.6.1.3): run-length composition offsets; version 1 (signed) when one is negative. 343 fn wctts t ulong to void 344 cv = pget t PCTV 345 if eq cv 0 346 ret 347 end 348 n = pget t PNS 349 p = wmfull 0x63747473 sub cv 1 0 350 pc = get mbn 0 351 wm32 0 352 cnt = ulong 0 353 rn = ulong 0 354 lv = ulong 0 355 for j ulong n bound `1048576` 356 d = and bitcast ulong get scto wsamp t j `4294967295` 357 if gt rn 0 358 if eq d lv 359 rn = add rn 1 360 continue 361 end 362 wm32 rn 363 wm32 lv 364 cnt = add cnt 1 365 end 366 lv = d 367 rn = 1 368 end 369 if gt rn 0 370 wm32 rn 371 wm32 lv 372 cnt = add cnt 1 373 end 374 wmput pc cnt 375 wmend p 376 end 377 378 note wstss (8.6.2): written only when some sample is not a sync sample (absent: all are sync). 379 fn wstss t ulong to void 380 n = pget t PNS 381 if eq pget t PNSYN n 382 ret 383 end 384 p = wmfull 0x73747373 0 0 385 wm32 pget t PNSYN 386 for j ulong n bound `1048576` 387 if ne get ssync wsamp t j 0 388 wm32 add j 1 389 end 390 end 391 wmend p 392 end 393 394 note wstsc (8.7.4): every chunk (from chunk 1) holds one sample of description 1. 395 fn wstsc t ulong to void 396 p = wmfull 0x73747363 0 0 397 if eq pget t PNS 0 398 wm32 0 399 wmend p 400 ret 401 end 402 wm32 1 403 wm32 1 404 wm32 1 405 wm32 1 406 wmend p 407 end 408 409 note wstsz (8.7.3.2): sample_size 0 and one size per sample. 410 fn wstsz t ulong to void 411 n = pget t PNS 412 p = wmfull 0x7374737a 0 0 413 wm32 0 414 wm32 n 415 for j ulong n bound `1048576` 416 wm32 ulong get ssz wsamp t j 417 end 418 wmend p 419 end 420 421 note wstco (8.7.5): one chunk per sample at wbase (first mdat payload byte) + its mdat offset. 422 fn wstco t ulong to void 423 n = pget t PNS 424 q0 = pget t PSB 425 ty = ulong 0x7374636f 426 if eq get wco 0 1 427 ty = 0x636f3634 428 end 429 p = wmfull ty 0 0 430 wm32 n 431 for j ulong n bound `1048576` 432 o = padd get wbase 0 get pofs add q0 j 433 if eq get wco 0 1 434 wm64 o 435 continue 436 end 437 if gt o `4294967295` 438 quit 34 ebound 439 end 440 wm32 o 441 end 442 wmend p 443 end 444 445 note ---- moov ---- 446 fn wstbl t ulong s ulong to void 447 p = wmbox 0x7374626c 448 wmcopy tg s KFILE tg s KSDP tg s KSDS 449 wstts t 450 wctts t 451 wstss t 452 wstsc t 453 wstsz t 454 wstco t 455 wmend p 456 end 457 458 fn wtrak t ulong to void 459 s = pget t PSLOT 460 f = tg s KFILE 461 pt0 = wmbox 0x7472616b 462 wtkhd t 463 welst t 464 pm = wmbox 0x6d646961 465 wmdhd t 466 wmcopy f tg s KHDLP tg s KHDLS 467 pn0 = wmbox 0x6d696e66 468 if gt tg s KMINS 0 469 wmcopy f tg s KMINP tg s KMINS 470 end 471 wdinf 472 wstbl t s 473 wmend pn0 474 wmend pm 475 wmend pt0 476 end 477 478 fn wmoov to void 479 put mbn 0 0 480 p = wmbox 0x6d6f6f76 481 wmvhd 482 for t ulong get pnt 0 bound 8 483 wtrak t 484 end 485 wmend p 486 end 487 488 note wbrand: wqt = 1 when the first input's ftyp major brand is 'qt ' (QuickTime). 489 fn wbrand to void 490 put wqt 0 0 491 f = get pmf 0 492 n = fget f FFTS 493 if lt n 12 494 ret 495 end 496 if gt n 32 497 n = 32 498 end 499 wrd f fget f FFTP n 500 o = whdr 501 if gt add o 4 n 502 ret 503 end 504 if eq wg32 o 0x71742020 505 put wqt 0 1 506 end 507 end 508 509 note wcheck: every planned sample lies inside its input file (the parser checked it too). 510 fn wcheck to void 511 for k ulong get pn 0 bound `1048576` 512 g = get pidx k 513 f = ulong get pfil k 514 if gt padd get soff g ulong get ssz g fget f FSZ 515 quit 23 etable 516 end 517 end 518 end 519 520 fn wmdhdr to ulong 521 if gt get ppay 0 `4294967287` 522 ret 16 523 end 524 ret 8 525 end 526 527 note wmdat: the mdat header (8.1.1): 32-bit size, or size 1 and a 64-bit largesize (4.2). 528 fn wmdat mh ulong to void 529 if eq mh 16 530 wbe32 1 531 wbe32 0x6d646174 532 msz = padd get ppay 0 16 533 wbe32 shr msz 32 534 wbe32 msz 535 ret 536 end 537 wbe32 add get ppay 0 8 538 wbe32 0x6d646174 539 end 540 541 note wsamps: every planned sample, unchanged, in mdat order (pord). 542 fn wsamps to void 543 for k ulong get pn 0 bound `1048576` 544 j = get pord k 545 g = get pidx j 546 f = ulong get pfil j 547 wfrom f get soff g ulong get ssz g 548 end 549 end 550 551 note wsize: ftyp + moov + mdat header, the offset of the first mdat payload byte. 552 fn wsize mh ulong to ulong 553 ret padd padd fget get pmf 0 FFTS get mbn 0 mh 554 end 555 556 note woutput: build the moov with chunk offsets from wbase = ftyp + moov + mdat header. moov's 557 note size does not depend on wbase (only on stco versus co64), so a first build measures it, co64 is 558 note chosen when the last offset would pass 2^32 - 1, and the final build has the real offsets. 559 fn woutput to void 560 if eq get pnt 0 0 561 quit 22 emiss 562 end 563 wcheck 564 wbrand 565 mh = wmdhdr 566 put wco 0 0 567 put wbase 0 0 568 wmoov 569 base = wsize mh 570 if gt padd base get ppay 0 `4294967296` 571 put wco 0 1 572 wmoov 573 base = wsize mh 574 end 575 put wbase 0 base 576 wmoov 577 if ne wsize mh base 578 quit 23 etable 579 end 580 tot = padd base get ppay 0 581 wcreate 582 f = get pmf 0 583 ft = fget f FFTS 584 if gt ft 0 585 wfrom f fget f FFTP ft 586 end 587 wmb 588 wmdat mh 589 wsamps 590 wflush 591 if ne get wtot 0 tot 592 quit 60 eio 593 end 594 wclose 595 end