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 / write.om

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