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 / src / io.om

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