omega-mp4
omega-mp4: ovideo, an MP4 reader written in Omega (Apache-2.0)
Brought by drakestapleton
| 1 | om2 |
| 2 | note omega-mp4 (command ovideo), src/io.om: bounds, refusal, standard output, the input window and |
| 3 | note the output file. ovideo reads and writes the ISO base media file format (ISO/IEC 14496-12, cited |
| 4 | note below as "14496-12 n.n.n") and the QuickTime file format (the same box structure; QuickTime |
| 5 | note calls a box an atom). Units, in build order: io.om box.om info.om plan.om write.om main.om. |
| 6 | note |
| 7 | note Bounds. Each is checked before an array is indexed and refused with exit code 34: |
| 8 | note samples held (all tracks of all inputs together) 1048576 (NS) |
| 9 | note track boxes per input 8 (NTF); inputs per join 16 (NF); track slots 128 (NT) |
| 10 | note edit list entries per input track 64 (NE); output edit entries per track 64 |
| 11 | note output chunks (all tracks) 1048576 (NC) |
| 12 | note boxes visited per walk 65536 (NB); box nesting depth 16 (ND) |
| 13 | note sample description box 1048576 bytes; output moov 16777216 bytes (MBCAP) |
| 14 | note seconds in a time argument below 2^31; time denominators 1 .. 4294967295 |
| 15 | note Every loop over file structure is bounded by a count read from a checked box header (the count |
| 16 | note must fit in the box) or by the file size; the input window re-reads at most 1048576 bytes. |
| 17 | const NS ulong 1048576 |
| 18 | const NT ulong 128 |
| 19 | const NF ulong 16 |
| 20 | const NTF ulong 8 |
| 21 | const NE ulong 64 |
| 22 | const NC ulong 1048576 |
| 23 | const NB ulong 65536 |
| 24 | const ND ulong 16 |
| 25 | const MBCAP ulong 16777216 |
| 26 | const WBCAP ulong 4194304 |
| 27 | const IWCAP ulong 1048576 |
| 28 | const SDCAP ulong 1048576 |
| 29 | |
| 30 | note Samples, in decode order, track after track: file offset, size, decode time (media timescale), |
| 31 | note composition offset (14496-12 8.6.1.3), duration (8.6.1.2), sync flag (8.6.2). |
| 32 | global soff ulong 1048576 |
| 33 | global ssz uint 1048576 |
| 34 | global sdts ulong 1048576 |
| 35 | global scto long 1048576 |
| 36 | global sdur uint 1048576 |
| 37 | global ssync uchar 1048576 |
| 38 | global ns ulong 1 |
| 39 | |
| 40 | note Track slots: 64 ulong fields per slot (the K* constants), edit entries 64 per slot. |
| 41 | global tk ulong 8192 |
| 42 | global ntk ulong 1 |
| 43 | global eld ulong 8192 |
| 44 | global elm long 8192 |
| 45 | global elr ulong 8192 |
| 46 | const KFILE ulong 0 |
| 47 | const KTKP ulong 1 |
| 48 | const KTKS ulong 2 |
| 49 | const KMDHP ulong 3 |
| 50 | const KMDHS ulong 4 |
| 51 | const KHDLP ulong 5 |
| 52 | const KHDLS ulong 6 |
| 53 | const KMINP ulong 7 |
| 54 | const KMINS ulong 8 |
| 55 | const KSDP ulong 9 |
| 56 | const KSDS ulong 10 |
| 57 | const KHDLR ulong 11 |
| 58 | const KCC ulong 12 |
| 59 | const KTS ulong 13 |
| 60 | const KMDUR ulong 14 |
| 61 | const KNS ulong 15 |
| 62 | const KNSYN ulong 16 |
| 63 | const KCTTS ulong 17 |
| 64 | const KSTSS ulong 18 |
| 65 | const KCO ulong 19 |
| 66 | const KNE ulong 20 |
| 67 | const KE0 ulong 21 |
| 68 | const KMT ulong 22 |
| 69 | const KED ulong 23 |
| 70 | const KSB ulong 24 |
| 71 | const KKEEP ulong 25 |
| 72 | const KTID ulong 26 |
| 73 | const KTKD ulong 27 |
| 74 | const KNCH ulong 28 |
| 75 | const KSTTP ulong 29 |
| 76 | const KSTTS ulong 30 |
| 77 | const KCTP ulong 31 |
| 78 | const KCTS ulong 32 |
| 79 | const KSSP ulong 33 |
| 80 | const KSSS ulong 34 |
| 81 | const KSCP ulong 35 |
| 82 | const KSCS ulong 36 |
| 83 | const KSZP ulong 37 |
| 84 | const KSZS ulong 38 |
| 85 | const KCOP ulong 39 |
| 86 | const KCOS ulong 40 |
| 87 | const KELP ulong 41 |
| 88 | const KELS ulong 42 |
| 89 | const KMINH ulong 43 |
| 90 | const KTRKP ulong 44 |
| 91 | const KTRKS ulong 45 |
| 92 | const KSTBP ulong 46 |
| 93 | const KSTBS ulong 47 |
| 94 | const KENC ulong 48 |
| 95 | const KCTV ulong 49 |
| 96 | const KEDM ulong 50 |
| 97 | |
| 98 | note Input files: 32 ulong fields per file. |
| 99 | global fl ulong 512 |
| 100 | global nfl ulong 1 |
| 101 | global ffd int 16 |
| 102 | const FSZ ulong 0 |
| 103 | const FMTS ulong 1 |
| 104 | const FMDUR ulong 2 |
| 105 | const FMVP ulong 3 |
| 106 | const FMVS ulong 4 |
| 107 | const FMOOVP ulong 5 |
| 108 | const FMOOVS ulong 6 |
| 109 | const FFTP ulong 7 |
| 110 | const FFTS ulong 8 |
| 111 | const FMDATP ulong 9 |
| 112 | const FNTK ulong 10 |
| 113 | const FTK0 ulong 11 |
| 114 | const FFRAG ulong 12 |
| 115 | const FMVV ulong 13 |
| 116 | const FNMDAT ulong 14 |
| 117 | |
| 118 | global iw uchar 1048576 |
| 119 | global iwb ulong 1 |
| 120 | global iwl ulong 1 |
| 121 | global iwf ulong 1 |
| 122 | global ob uchar 65536 |
| 123 | global obn ulong 1 |
| 124 | global wb uchar 4194304 |
| 125 | global wbn ulong 1 |
| 126 | global wfd int 1 |
| 127 | global wmade ulong 1 |
| 128 | global wtot ulong 1 |
| 129 | global opath uchar 4096 |
| 130 | global opl ulong 1 |
| 131 | global nbox ulong 1 |
| 132 | global mb uchar 16777216 |
| 133 | global mbn ulong 1 |
| 134 | |
| 135 | text tpre "ovideo: " |
| 136 | text tnl "\n" |
| 137 | text tsp " " |
| 138 | text eusage "usage: ovideo info FILE [DEPTH] | dump FILE | trim IN START END OUT | join OUT A B ..." |
| 139 | text eopen "cannot open an input file" |
| 140 | text eexist "cannot create the output: it exists or its directory is not writable (exclusive creation)" |
| 141 | text ebox "malformed box structure: a box size is below its header or past its parent or the file" |
| 142 | text edepth "bound: boxes nested deeper than 16 or more than 65536 boxes" |
| 143 | text emiss "malformed file: a required box is missing or repeated (ftyp, moov, mvhd, trak, tkhd, mdia, mdhd, hdlr, minf, stbl, stsd, stts, stsz, stsc, stco/co64)" |
| 144 | text etable "malformed sample table: counts disagree, a count exceeds its box, or a sample lies outside the file" |
| 145 | text efrag "fragmented file (moov/mvex or moof): not supported in this slice" |
| 146 | text eenc "encrypted or protected track (sinf, encv, enca): not supported" |
| 147 | text eother "track handler other than vide or soun (timecode, text, metadata, hint): not supported by trim and join" |
| 148 | text eunsup "unsupported structure: several sample descriptions, an external data reference, stz2, an edit entry other than an empty edit or a media edit at rate 1, or (trim and join) an edit list other than one optional empty edit plus one media edit" |
| 149 | text ebound "bound exceeded: see the README (tracks 8 per input, samples 1048576, chunks 1048576, edit entries 64, inputs 16, moov 16 MiB)" |
| 150 | text erange "time range: START must be below END and below the movie duration" |
| 151 | text eempty "a track has no sample presented between START and END" |
| 152 | text ejcount "join: the inputs have different numbers of tracks" |
| 153 | text ejhdlr "join: track handler types differ" |
| 154 | text ejdesc "join: sample descriptions (stsd) are not byte-equal" |
| 155 | text ejts "join: track media timescales differ" |
| 156 | text eio "I/O error reading an input or writing the output" |
| 157 | |
| 158 | note wclean: a refusal after the output was created removes it (no partial output stays). |
| 159 | fn wclean to void |
| 160 | if eq get wmade 0 1 |
| 161 | r = fclose get wfd 0 |
| 162 | r = unlink opath 0 get opl 0 |
| 163 | put wmade 0 0 |
| 164 | end |
| 165 | end |
| 166 | |
| 167 | fn quit c int msg view uchar * to void |
| 168 | oflush |
| 169 | wclean |
| 170 | r = write 2 tpre 0 len tpre |
| 171 | r = write 2 msg 0 len msg |
| 172 | r = write 2 tnl 0 1 |
| 173 | exit c |
| 174 | end |
| 175 | |
| 176 | note ---- standard output ---- |
| 177 | note oflush: a failed write to standard output is an I/O refusal (60) with its one stderr line. |
| 178 | fn oflush to void |
| 179 | n = get obn 0 |
| 180 | o = ulong 0 |
| 181 | while lt o n bound 65536 |
| 182 | r = write 1 ob o sub n o |
| 183 | if le r 0 |
| 184 | put obn 0 0 |
| 185 | wclean |
| 186 | r = write 2 tpre 0 len tpre |
| 187 | r = write 2 eio 0 len eio |
| 188 | r = write 2 tnl 0 1 |
| 189 | exit 60 |
| 190 | end |
| 191 | o = add o ulong r |
| 192 | end |
| 193 | put obn 0 0 |
| 194 | end |
| 195 | |
| 196 | fn obyte c ulong to void |
| 197 | if ge get obn 0 len ob |
| 198 | oflush |
| 199 | end |
| 200 | put ob get obn 0 uchar and c 255 |
| 201 | put obn 0 add get obn 0 1 |
| 202 | end |
| 203 | |
| 204 | fn ostr t view uchar * to void |
| 205 | for i ulong len t bound 4096 |
| 206 | obyte ulong get t i |
| 207 | end |
| 208 | end |
| 209 | |
| 210 | fn onum v ulong to void |
| 211 | b = new uchar 24 0 |
| 212 | x = v |
| 213 | k = ulong 0 |
| 214 | while true bound 21 |
| 215 | put b k uchar add rem x 10 48 |
| 216 | x = div x 10 |
| 217 | k = add k 1 |
| 218 | if eq x 0 |
| 219 | break |
| 220 | end |
| 221 | end |
| 222 | while gt k 0 bound 21 |
| 223 | k = sub k 1 |
| 224 | obyte ulong get b k |
| 225 | end |
| 226 | end |
| 227 | |
| 228 | fn osnum v long to void |
| 229 | if lt v 0 |
| 230 | obyte 45 |
| 231 | onum add bitcast ulong not v 1 |
| 232 | else |
| 233 | onum bitcast ulong v |
| 234 | end |
| 235 | end |
| 236 | |
| 237 | note ofourcc: four bytes of a box type; bytes outside printable ASCII print as '?'. |
| 238 | fn ofourcc t ulong to void |
| 239 | for i ulong 4 |
| 240 | c = and shr t mul sub 3 i 8 255 |
| 241 | if or lt c 32 gt c 126 |
| 242 | c = 63 |
| 243 | end |
| 244 | obyte c |
| 245 | end |
| 246 | end |
| 247 | |
| 248 | note ---- input files and the read window ---- |
| 249 | fn fget f ulong k ulong to ulong |
| 250 | ret get fl add mul f 32 k |
| 251 | end |
| 252 | fn fset f ulong k ulong v ulong to void |
| 253 | put fl add mul f 32 k v |
| 254 | end |
| 255 | fn tg s ulong k ulong to ulong |
| 256 | ret get tk add mul s 64 k |
| 257 | end |
| 258 | fn tset s ulong k ulong v ulong to void |
| 259 | put tk add mul s 64 k v |
| 260 | end |
| 261 | |
| 262 | note need: make file f bytes [p, p + n) (n <= 16) readable in iw; the index of p in iw. Bytes past |
| 263 | note the end of the file are a malformed box structure (code 20). |
| 264 | fn need f ulong p ulong n ulong to ulong |
| 265 | z = fget f FSZ |
| 266 | if or gt n z gt p sub z n |
| 267 | quit 20 ebox |
| 268 | end |
| 269 | if eq get iwf 0 add f 1 |
| 270 | b = get iwb 0 |
| 271 | if and ge p b le add p n add b get iwl 0 |
| 272 | ret sub p b |
| 273 | end |
| 274 | end |
| 275 | m = sub z p |
| 276 | if gt m IWCAP |
| 277 | m = IWCAP |
| 278 | end |
| 279 | r = pread get ffd f iw 0 m bitcast long p |
| 280 | if ne r bitcast long m |
| 281 | quit 60 eio |
| 282 | end |
| 283 | put iwb 0 p |
| 284 | put iwl 0 m |
| 285 | put iwf 0 add f 1 |
| 286 | ret 0 |
| 287 | end |
| 288 | |
| 289 | fn rd8 f ulong p ulong to ulong |
| 290 | i = need f p 1 |
| 291 | ret ulong get iw i |
| 292 | end |
| 293 | fn rd16 f ulong p ulong to ulong |
| 294 | i = need f p 2 |
| 295 | ret or shl ulong get iw i 8 ulong get iw add i 1 |
| 296 | end |
| 297 | fn rd32 f ulong p ulong to ulong |
| 298 | i = need f p 4 |
| 299 | a = or shl ulong get iw i 24 shl ulong get iw add i 1 16 |
| 300 | ret or a or shl ulong get iw add i 2 8 ulong get iw add i 3 |
| 301 | end |
| 302 | fn rd64 f ulong p ulong to ulong |
| 303 | h = rd32 f p |
| 304 | l = rd32 f add p 4 |
| 305 | ret or shl h 32 l |
| 306 | end |
| 307 | |
| 308 | note openin: argument arg becomes input file f (read only); its size from the end offset. |
| 309 | fn openin arg ulong f ulong to void |
| 310 | pb = new uchar 4096 0 |
| 311 | r = argread arg pb 0 4096 |
| 312 | if le r 0 |
| 313 | quit 2 eopen |
| 314 | end |
| 315 | fd = fopen pb 0 ulong r 0 0 |
| 316 | if lt fd 0 |
| 317 | quit 2 eopen |
| 318 | end |
| 319 | z = fseek int fd 0 2 |
| 320 | if lt z 0 |
| 321 | quit 2 eopen |
| 322 | end |
| 323 | put ffd f int fd |
| 324 | for k ulong 32 |
| 325 | fset f k 0 |
| 326 | end |
| 327 | fset f FSZ bitcast ulong z |
| 328 | put iwf 0 0 |
| 329 | end |
| 330 | |
| 331 | note ---- output file ---- |
| 332 | note wcreate: O_WRONLY|O_CREAT|O_EXCL (193), mode 0644; an existing output is never opened. |
| 333 | fn wcreate to void |
| 334 | fd = fopen opath 0 get opl 0 193 420 |
| 335 | if lt fd 0 |
| 336 | quit 5 eexist |
| 337 | end |
| 338 | put wfd 0 int fd |
| 339 | put wmade 0 1 |
| 340 | put wbn 0 0 |
| 341 | put wtot 0 0 |
| 342 | end |
| 343 | |
| 344 | fn wflush to void |
| 345 | n = get wbn 0 |
| 346 | o = ulong 0 |
| 347 | while lt o n bound 4194304 |
| 348 | r = write get wfd 0 wb o sub n o |
| 349 | if le r 0 |
| 350 | quit 60 eio |
| 351 | end |
| 352 | o = add o ulong r |
| 353 | end |
| 354 | put wtot 0 add get wtot 0 n |
| 355 | put wbn 0 0 |
| 356 | end |
| 357 | |
| 358 | fn wbyte c ulong to void |
| 359 | if ge get wbn 0 WBCAP |
| 360 | wflush |
| 361 | end |
| 362 | put wb get wbn 0 uchar and c 255 |
| 363 | put wbn 0 add get wbn 0 1 |
| 364 | end |
| 365 | |
| 366 | fn wbe32 v ulong to void |
| 367 | wbyte shr v 24 |
| 368 | wbyte shr v 16 |
| 369 | wbyte shr v 8 |
| 370 | wbyte v |
| 371 | end |
| 372 | |
| 373 | note wfrom: copy n bytes of input f at p to the output (pread into the write buffer). |
| 374 | fn wfrom f ulong p ulong n ulong to void |
| 375 | q = p |
| 376 | k = n |
| 377 | while gt k 0 bound 1099511627776 |
| 378 | if ge get wbn 0 WBCAP |
| 379 | wflush |
| 380 | end |
| 381 | m = sub WBCAP get wbn 0 |
| 382 | if gt m k |
| 383 | m = k |
| 384 | end |
| 385 | r = pread get ffd f wb get wbn 0 m bitcast long q |
| 386 | if ne r bitcast long m |
| 387 | quit 60 eio |
| 388 | end |
| 389 | put wbn 0 add get wbn 0 m |
| 390 | q = add q m |
| 391 | k = sub k m |
| 392 | end |
| 393 | end |
| 394 | |
| 395 | note wmb: the moov buffer mb[0 .. mbn) to the output. |
| 396 | fn wmb to void |
| 397 | n = get mbn 0 |
| 398 | o = ulong 0 |
| 399 | while lt o n bound 16777216 |
| 400 | if ge get wbn 0 WBCAP |
| 401 | wflush |
| 402 | end |
| 403 | m = sub WBCAP get wbn 0 |
| 404 | if gt m sub n o |
| 405 | m = sub n o |
| 406 | end |
| 407 | for i ulong m bound 4194304 |
| 408 | put wb add get wbn 0 i get mb add o i |
| 409 | end |
| 410 | put wbn 0 add get wbn 0 m |
| 411 | o = add o m |
| 412 | end |
| 413 | end |
| 414 | |
| 415 | fn wclose to void |
| 416 | wflush |
| 417 | r = fsync get wfd 0 |
| 418 | if lt r 0 |
| 419 | quit 60 eio |
| 420 | end |
| 421 | r = fclose get wfd 0 |
| 422 | put wmade 0 0 |
| 423 | end |