interplane.Get Omega
Read in your language

omega-mp4

omega-mp4: ovideo, an MP4 reader written in Omega (Apache-2.0)

Brought by drakestapleton

branch main Branches Tags History

omega-mp4 / tests / mkcases.om

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