omega-mp4
omega-mp4: ovideo, an MP4 reader written in Omega (Apache-2.0)
Brought by drakestapleton
omega-mp4 / tests / mkcases.om
| 1 | om2 |
| 2 | note tests/mkcases.om: builds the 64-bit corpus cases and the refusal fixtures from a moov-at-end MP4 |
| 3 | note generated by ffmpeg. It shares no code with ovideo. ISO/IEC 14496-12: 4.2 box header (size 1 = |
| 4 | note 64-bit largesize), 8.7.5 stco and co64, 8.2.2 mvhd, 8.3.2 tkhd, 8.4.2 mdhd, 8.4.3 hdlr, 8.5.2 stsd, |
| 5 | note 8.6.1.2 stts, 8.7.3.2 stsz and 8.6.6 elst (version 1 = 64-bit times). |
| 6 | note Usage: mkcases IN OUT MODE. Every mode rewrites each stco as co64 (same offsets plus the shift). |
| 7 | note 0 co64 only |
| 8 | note 1 sparse file above 4 GiB: ftyp, then a free box with largesize 4294967296 whose body is a hole, |
| 9 | note then the rest of IN; every chunk offset grows by 2^32 |
| 10 | note 2 version 1 mvhd/tkhd/mdhd/elst; largesize headers on moov, trak, mdia, minf, stbl and edts |
| 11 | note a (ovideo exit 20) the mvhd size grows by 65536, past its parent |
| 12 | note b (22) every stsz box is renamed free, so a required box is missing |
| 13 | note c (23) the first stts entry counts one sample more than stsz has |
| 14 | note d (31) the avc1 sample entry is renamed encv (a protected entry, 14496-12 8.12) |
| 15 | note e (33) the first edit of every elst gets media rate 2.0 |
| 16 | note f (34) every trak box is written five times, so there are more than 8 tracks |
| 17 | note g (41 in join) the hdlr of the video track says soun |
| 18 | note h (42 in join) the last byte of the avc1 sample entry (in its btrt box) is changed |
| 19 | note IN must be at most 1 MiB, start with ftyp, end with moov, and carry version 0 headers. |
| 20 | note The output is created exclusively; on failure it is removed and the exit status is 1. |
| 21 | global gb uchar 1048576 |
| 22 | global gc uchar 2097152 |
| 23 | global gn ulong 1 |
| 24 | global go ulong 1 |
| 25 | global gd ulong 1 |
| 26 | global gm ulong 1 |
| 27 | global gh uchar 16 |
| 28 | const MV1 ulong 50 |
| 29 | const MSP ulong 49 |
| 30 | const MTRK ulong 102 |
| 31 | const TTRAK ulong 0x7472616b |
| 32 | const TAVC1 ulong 0x61766331 |
| 33 | fn fail to void |
| 34 | exit 1 |
| 35 | end |
| 36 | fn gu p ulong to ulong |
| 37 | if gt p sub get gn 0 4 |
| 38 | fail |
| 39 | end |
| 40 | v = ulong 0 |
| 41 | for i ulong 4 |
| 42 | v = or shl v 8 ulong get gb add p i |
| 43 | end |
| 44 | ret v |
| 45 | end |
| 46 | fn gco p ulong to ulong |
| 47 | if gt p sub get go 0 4 |
| 48 | fail |
| 49 | end |
| 50 | v = ulong 0 |
| 51 | for i ulong 4 |
| 52 | v = or shl v 8 ulong get gc add p i |
| 53 | end |
| 54 | ret v |
| 55 | end |
| 56 | fn gout v ulong to void |
| 57 | if ge get go 0 len gc |
| 58 | fail |
| 59 | end |
| 60 | put gc get go 0 uchar and v 255 |
| 61 | put go 0 add get go 0 1 |
| 62 | end |
| 63 | fn g32 v ulong to void |
| 64 | for i ulong 4 |
| 65 | gout shr v mul sub 3 i 8 |
| 66 | end |
| 67 | end |
| 68 | fn g64 v ulong to void |
| 69 | g32 shr v 32 |
| 70 | g32 and v 4294967295 |
| 71 | end |
| 72 | fn gcopy p ulong n ulong to void |
| 73 | if gt n get gn 0 |
| 74 | fail |
| 75 | end |
| 76 | if gt p sub get gn 0 n |
| 77 | fail |
| 78 | end |
| 79 | for i ulong n bound 1048576 |
| 80 | gout ulong get gb add p i |
| 81 | end |
| 82 | end |
| 83 | fn gpatch p ulong v ulong to void |
| 84 | if gt p sub get go 0 4 |
| 85 | fail |
| 86 | end |
| 87 | for i ulong 4 |
| 88 | put gc add p i uchar and shr v mul sub 3 i 8 255 |
| 89 | end |
| 90 | end |
| 91 | note gcont: moov, trak, mdia, minf, stbl, edts (the containers on the path to the tables). |
| 92 | fn gcont t ulong to bool |
| 93 | if or eq t 0x6d6f6f76 eq t TTRAK |
| 94 | ret true |
| 95 | end |
| 96 | if or eq t 0x6d646961 eq t 0x6d696e66 |
| 97 | ret true |
| 98 | end |
| 99 | ret or eq t 0x7374626c eq t 0x65647473 |
| 100 | end |
| 101 | note ghd: mvhd, tkhd or mdhd. |
| 102 | fn ghd t ulong to bool |
| 103 | if or eq t 0x6d766864 eq t 0x746b6864 |
| 104 | ret true |
| 105 | end |
| 106 | ret eq t 0x6d646864 |
| 107 | end |
| 108 | note ghdr: a version 0 mvhd, tkhd or mdhd box body (n bytes at p) written as version 1. |
| 109 | fn ghdr p ulong t ulong n ulong to void |
| 110 | if ne get gb add p 8 0 |
| 111 | fail |
| 112 | end |
| 113 | gout 1 |
| 114 | gcopy add p 9 3 |
| 115 | g32 0 |
| 116 | gcopy add p 12 4 |
| 117 | g32 0 |
| 118 | gcopy add p 16 4 |
| 119 | if eq t 0x746b6864 |
| 120 | gcopy add p 20 8 |
| 121 | g32 0 |
| 122 | gcopy add p 28 sub n 28 |
| 123 | else |
| 124 | gcopy add p 20 4 |
| 125 | g32 0 |
| 126 | gcopy add p 24 sub n 24 |
| 127 | end |
| 128 | end |
| 129 | fn gstco p ulong n ulong to void |
| 130 | if lt n 16 |
| 131 | fail |
| 132 | end |
| 133 | c = gu add p 12 |
| 134 | if ne n add 16 mul c 4 |
| 135 | fail |
| 136 | end |
| 137 | gcopy add p 8 8 |
| 138 | for i ulong c bound 262144 |
| 139 | v = gu add add p 16 mul i 4 |
| 140 | g64 add v get gd 0 |
| 141 | end |
| 142 | end |
| 143 | fn gelst p ulong n ulong to void |
| 144 | if lt n 16 |
| 145 | fail |
| 146 | end |
| 147 | if ne get gb add p 8 0 |
| 148 | fail |
| 149 | end |
| 150 | c = gu add p 12 |
| 151 | if ne n add 16 mul c 12 |
| 152 | fail |
| 153 | end |
| 154 | gout 1 |
| 155 | gcopy add p 9 7 |
| 156 | for i ulong c bound 64 |
| 157 | x = add add p 16 mul i 12 |
| 158 | g64 gu x |
| 159 | v = gu add x 4 |
| 160 | if ge v 2147483648 |
| 161 | g64 or v 18446744069414584320 |
| 162 | else |
| 163 | g64 v |
| 164 | end |
| 165 | gcopy add x 8 4 |
| 166 | end |
| 167 | end |
| 168 | note gleaf: the body of a box that is not a container on the path to the tables. |
| 169 | fn gleaf p ulong n ulong t ulong to void |
| 170 | if eq t 0x7374636f |
| 171 | gstco p n |
| 172 | ret |
| 173 | end |
| 174 | v1 = eq get gm 0 MV1 |
| 175 | if and v1 ghd t |
| 176 | ghdr p t n |
| 177 | ret |
| 178 | end |
| 179 | if and v1 eq t 0x656c7374 |
| 180 | gelst p n |
| 181 | ret |
| 182 | end |
| 183 | gcopy add p 8 sub n 8 |
| 184 | end |
| 185 | note gpost: the refusal-fixture change to the box just written at q (size bytes, source type t). |
| 186 | fn gpost q ulong size ulong t ulong to void |
| 187 | m = get gm 0 |
| 188 | if and eq m 97 eq t 0x6d766864 |
| 189 | gpatch q add size 65536 |
| 190 | end |
| 191 | if and eq m 98 eq t 0x7374737a |
| 192 | gpatch add q 4 0x66726565 |
| 193 | end |
| 194 | if and eq m 99 eq t 0x73747473 |
| 195 | if ge size 24 |
| 196 | gpatch add q 16 add gco add q 16 1 |
| 197 | end |
| 198 | end |
| 199 | if and eq m 100 eq t 0x73747364 |
| 200 | if and ge size 24 eq gco add q 20 TAVC1 |
| 201 | gpatch add q 20 0x656e6376 |
| 202 | end |
| 203 | end |
| 204 | if and eq m 101 eq t 0x656c7374 |
| 205 | if ge size 28 |
| 206 | gpatch add q 24 131072 |
| 207 | end |
| 208 | end |
| 209 | if and eq m 103 eq t 0x68646c72 |
| 210 | if and ge size 20 eq gco add q 16 0x76696465 |
| 211 | gpatch add q 16 0x736f756e |
| 212 | end |
| 213 | end |
| 214 | if and eq m 104 eq t 0x73747364 |
| 215 | if and ge size 24 eq gco add q 20 TAVC1 |
| 216 | k = sub add q size 1 |
| 217 | put gc k xor get gc k 1 |
| 218 | end |
| 219 | end |
| 220 | end |
| 221 | fn gbox p ulong lim ulong depth ulong to ulong |
| 222 | if ge depth 16 |
| 223 | fail |
| 224 | end |
| 225 | if lt sub lim p 8 |
| 226 | fail |
| 227 | end |
| 228 | n = gu p |
| 229 | t = gu add p 4 |
| 230 | if lt n 8 |
| 231 | fail |
| 232 | end |
| 233 | if gt n sub lim p |
| 234 | fail |
| 235 | end |
| 236 | q = get go 0 |
| 237 | large = and eq get gm 0 MV1 gcont t |
| 238 | if large |
| 239 | g32 1 |
| 240 | g32 t |
| 241 | g64 0 |
| 242 | else |
| 243 | g32 0 |
| 244 | if eq t 0x7374636f |
| 245 | g32 0x636f3634 |
| 246 | else |
| 247 | g32 t |
| 248 | end |
| 249 | end |
| 250 | if gcont t |
| 251 | gkids p n depth |
| 252 | else |
| 253 | gleaf p n t |
| 254 | end |
| 255 | size = sub get go 0 q |
| 256 | if large |
| 257 | gpatch add q 8 shr size 32 |
| 258 | gpatch add q 12 and size 4294967295 |
| 259 | else |
| 260 | gpatch q size |
| 261 | end |
| 262 | gpost q size t |
| 263 | ret add p n |
| 264 | end |
| 265 | note gkids: the children of the container at p (n bytes); in mode f each trak is written five times. |
| 266 | fn gkids p ulong n ulong depth ulong to void |
| 267 | x = add p 8 |
| 268 | e = add p n |
| 269 | while lt x e bound 65536 |
| 270 | c = gu add x 4 |
| 271 | if and eq get gm 0 MTRK eq c TTRAK |
| 272 | for k ulong 4 |
| 273 | y = gbox x e add depth 1 |
| 274 | end |
| 275 | end |
| 276 | x = gbox x e add depth 1 |
| 277 | end |
| 278 | end |
| 279 | note gload: the input file into gb (at most 1 MiB - 1 bytes); it must begin with ftyp. |
| 280 | fn gload path view uchar * to void |
| 281 | fd = fopen path 0 len path 0 0 |
| 282 | if lt fd 0 |
| 283 | fail |
| 284 | end |
| 285 | n = read int fd gb 0 len gb |
| 286 | z = fclose int fd |
| 287 | if lt n 16 |
| 288 | fail |
| 289 | end |
| 290 | if ge n long len gb |
| 291 | fail |
| 292 | end |
| 293 | put gn 0 ulong n |
| 294 | if ne gu 4 0x66747970 |
| 295 | fail |
| 296 | end |
| 297 | end |
| 298 | note gfindm: the offset of the top-level moov, which must be the last box. |
| 299 | fn gfindm to ulong |
| 300 | n = get gn 0 |
| 301 | p = ulong 0 |
| 302 | while lt p n bound 65536 |
| 303 | size = gu p |
| 304 | if lt size 8 |
| 305 | fail |
| 306 | end |
| 307 | if gt size sub n p |
| 308 | fail |
| 309 | end |
| 310 | if eq gu add p 4 0x6d6f6f76 |
| 311 | if ne add p size n |
| 312 | fail |
| 313 | end |
| 314 | ret p |
| 315 | end |
| 316 | p = add p size |
| 317 | end |
| 318 | fail |
| 319 | ret 0 |
| 320 | end |
| 321 | fn gput fd int p ulong n ulong dst ulong to bool |
| 322 | w = pwrite fd gb p n bitcast long dst |
| 323 | ret eq w bitcast long n |
| 324 | end |
| 325 | note gemit: create path exclusively and write the output; remove it on failure. |
| 326 | fn gemit path view uchar * fs ulong moov ulong to bool |
| 327 | od = fopen path 0 len path 193 420 |
| 328 | if lt od 0 |
| 329 | ret false |
| 330 | end |
| 331 | o = int od |
| 332 | ok = true |
| 333 | if eq get gm 0 MSP |
| 334 | put gh 3 1 |
| 335 | put gh 4 102 |
| 336 | put gh 5 114 |
| 337 | put gh 6 101 |
| 338 | put gh 7 101 |
| 339 | put gh 11 1 |
| 340 | ok = gput o 0 fs 0 |
| 341 | w = pwrite o gh 0 16 bitcast long fs |
| 342 | if ne w 16 |
| 343 | ok = false |
| 344 | end |
| 345 | if ok |
| 346 | ok = gput o fs sub moov fs add fs get gd 0 |
| 347 | end |
| 348 | else |
| 349 | ok = gput o 0 moov 0 |
| 350 | end |
| 351 | if ok |
| 352 | w = pwrite o gc 0 get go 0 bitcast long add moov get gd 0 |
| 353 | if ne w bitcast long get go 0 |
| 354 | ok = false |
| 355 | end |
| 356 | end |
| 357 | z = fclose o |
| 358 | if not ok |
| 359 | z = unlink path 0 len path |
| 360 | end |
| 361 | ret ok |
| 362 | end |
| 363 | fn gmode m ulong to bool |
| 364 | if and ge m 48 le m 50 |
| 365 | ret true |
| 366 | end |
| 367 | ret and ge m 97 le m 104 |
| 368 | end |
| 369 | fn main to int |
| 370 | pa = new uchar 4096 0 |
| 371 | pb = new uchar 4096 0 |
| 372 | arg = new uchar 4 0 |
| 373 | if ne argcnt 4 |
| 374 | ret 1 |
| 375 | end |
| 376 | r = argread 1 pa 0 4096 |
| 377 | s = argread 2 pb 0 4096 |
| 378 | m = argread 3 arg 0 4 |
| 379 | if or le r 0 le s 0 |
| 380 | ret 1 |
| 381 | end |
| 382 | if ne m 1 |
| 383 | ret 1 |
| 384 | end |
| 385 | mode = ulong get arg 0 |
| 386 | if not gmode mode |
| 387 | ret 1 |
| 388 | end |
| 389 | put gm 0 mode |
| 390 | if eq mode MSP |
| 391 | put gd 0 4294967296 |
| 392 | end |
| 393 | gload part pa 0 ulong r |
| 394 | fs = gu 0 |
| 395 | moov = gfindm |
| 396 | if lt moov fs |
| 397 | ret 1 |
| 398 | end |
| 399 | e = gbox moov get gn 0 0 |
| 400 | if not gemit part pb 0 ulong s fs moov |
| 401 | ret 1 |
| 402 | end |
| 403 | ret 0 |
| 404 | end |