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

641 lines · 14183 bytes Raw Blame History
1note omega-mp4 src/plan.om: the output plan for trim and join (writer owner, names p*/P*).
2note ptrim and pjoin read only what bparse stored (io.om representation), check every value they
3note use, and fill the plan below. Every refusal happens here or in write.om before the output is
4note created (wcreate), so a refused plan never leaves a file.
5note
6note Time model (14496-12 8.6.5, 8.6.6). Movie time is in the mvhd timescale (8.2.2), media time in
7note the mdhd timescale (8.4.2). A track is presented over the movie interval [KE0, KE0 + KED); at
8note movie time KE0 + u it shows media time KMT + u * mdhd_timescale / mvhd_timescale. A sample's
9note composition (presentation) time is its decode time plus its composition offset (8.6.1.3).
10note
11note Output plan: per output track 64 ulong fields in pt (P* constants), its samples as indices into
12note the parsed sample arrays (pidx, with the input file in pfil), its edits in pel/pem, and the
13note mdat layout (pord: output samples in file order; pofs: byte offset of each sample in mdat).
14note One sample per chunk (8.7.4): the writer emits one stsc entry {1, 1, 1} for every track.
15
16const PSLOT ulong 0
17const PSB ulong 1
18const PNS ulong 2
19const PTS ulong 3
20const PNE ulong 4
21const PMDUR ulong 5
22const PTKD ulong 6
23const PCTV ulong 7
24const PNSYN ulong 8
25const PSZ ulong 9
26
27note PSLOT source track slot whose tkhd, mdhd, hdlr, media header and stsd are written
28note PSB first output sample (index into pidx); PNS output sample count; PTS media timescale
29note PNE output edit count; PMDUR sum of sample durations (media ticks, mdhd duration)
30note PTKD sum of output edit durations (movie ticks, tkhd duration, 8.3.2)
31note PCTV 0 no ctts, 1 ctts version 0, 2 ctts version 1 (8.6.1.3); PNSYN sync samples (8.6.2)
32note PSZ mdat bytes of the track
33global pt ulong 512
34global pnt ulong 1
35global pidx ulong 1048576
36global pfil uchar 1048576
37global pn ulong 1
38global pel ulong 512
39global pem long 512
40global pmts ulong 1
41global pmdur ulong 1
42global pmf ulong 1
43global pord ulong 1048576
44global pofs ulong 1048576
45global ppay ulong 1
46global pcur ulong 8
47global pdts ulong 8
48global pca uchar 65536
49global pcb uchar 65536
50
51fn pget t ulong k ulong to ulong
52ret get pt add mul t 64 k
53end
54
55fn pset t ulong k ulong v ulong to void
56put pt add mul t 64 k v
57end
58
59note ---- checked arithmetic (unsigned wrap is refused, signed conversion is checked first) ----
60fn padd a ulong c ulong to ulong
61if gt a sub 18446744073709551615 c
62quit 34 ebound
63end
64ret add a c
65end
66
67fn pmul a ulong c ulong to ulong
68if ne a 0
69if gt c div 18446744073709551615 a
70quit 34 ebound
71end
72end
73ret mul a c
74end
75
76fn plong v ulong to long
77if gt v 9223372036854775807
78quit 34 ebound
79end
80ret long v
81end
82
83fn pladd a long c long to long
84if gt c 0
85if gt a sub 9223372036854775807 c
86quit 34 ebound
87end
88end
89if lt c 0
90if lt a sub -9223372036854775808 c
91quit 34 ebound
92end
93end
94ret add a c
95end
96
97note pmd: x * y / z rounded down (up false) or up (up true), exactly, without a 128-bit product:
98note x = q*z + r gives x*y/z = q*y + r*y/z, and r*y < z*y fits because timescales are 32-bit.
99fn pmd x ulong y ulong z ulong up bool to ulong
100if eq z 0
101quit 23 etable
102end
103q = div x z
104r = rem x z
105lo = pmul r y
106v = padd pmul q y div lo z
107if up
108if ne rem lo z 0
109v = padd v 1
110end
111end
112ret v
113end
114
115note pct: composition time of parsed sample g = decode time + composition offset (8.6.1.3).
116fn pct g ulong to long
117ret pladd plong get sdts g get scto g
118end
119
120note prange: a track slot's samples lie inside the parsed arrays and its timescale is a nonzero
121note 32-bit value (8.4.2 declares timescale unsigned int(32)).
122note A complex edit list (KEDM 2: several media edits, 8.6.6) is refused 33 for trim and join.
123fn prange s ulong to void
124e = padd tg s KSB tg s KNS
125if gt e get ns 0
126quit 23 etable
127end
128ts = tg s KTS
129if eq ts 0
130quit 23 etable
131end
132if gt ts 4294967295
133quit 23 etable
134end
135if eq tg s KKEEP 0
136quit 32 eother
137end
138if eq tg s KEDM 2
139quit 33 eunsup
140end
141end
142
143fn pinit f ulong to void
144put pnt 0 0
145put pn 0 0
146put pmdur 0 0
147put ppay 0 0
148put pmf 0 f
149m = fget f FMTS
150if eq m 0
151quit 23 etable
152end
153if gt m 4294967295
154quit 23 etable
155end
156put pmts 0 m
157end
158
159fn pnew s ulong to ulong
160t = get pnt 0
161if ge t NTF
162quit 34 ebound
163end
164for k ulong 64
165pset t k 0
166end
167pset t PSLOT s
168pset t PSB get pn 0
169pset t PTS tg s KTS
170put pnt 0 add t 1
171ret t
172end
173
174note pedit: append an output edit (8.6.6): duration in movie ticks, media time m (-1 = empty edit).
175fn pedit t ulong d ulong m long to void
176e = pget t PNE
177if ge e NE
178quit 34 ebound
179end
180put pel add mul t 64 e d
181put pem add mul t 64 e m
182pset t PNE add e 1
183end
184
185note ptake: append samples [x0, x1) of track slot s, in decode order, to output track t.
186fn ptake t ulong s ulong x0 ulong x1 ulong to void
187f = tg s KFILE
188gs = tg s KSB
189m = sub x1 x0
190for j ulong m bound 1048576
191g = add gs add x0 j
192k = get pn 0
193if ge k NS
194quit 34 ebound
195end
196put pidx k g
197put pfil k uchar f
198put pn 0 add k 1
199pset t PNS add pget t PNS 1
200pset t PMDUR padd pget t PMDUR ulong get sdur g
201pset t PSZ padd pget t PSZ ulong get ssz g
202end
203end
204
205note ---- trim ----
206note pfirst: first decode sample to keep. whole (the trim starts at or before the track's
207note presentation): sample 0, so encoder priming and leading samples stay. Otherwise c is the
208note sample shown at media time mta (largest composition time <= mta). Audio keeps one packet
209note before c as decoder pre-roll (14496-12 10.1 roll recovery; AAC frames overlap their
210note predecessor); every audio sample is a sync sample here. Other tracks start at the latest sync
211note sample (8.6.2) at or before c in decode order whose own composition time is <= mta, so every
212note sample presented from mta on is decoded from that sync sample forward, including reordered
213note (B) frames that decode after a later sync sample. No such sample: start at 0.
214fn pfirst s ulong mta ulong whole bool to ulong
215if whole
216ret 0
217end
218gs = tg s KSB
219n = tg s KNS
220lim = plong mta
221c = n
222cbest = long 0
223for j ulong n bound 1048576
224ct = pct add gs j
225if le ct lim
226if eq c n
227c = j
228cbest = ct
229end
230if gt ct cbest
231c = j
232cbest = ct
233end
234end
235end
236if eq c n
237ret 0
238end
239if eq tg s KHDLR 0x736f756e
240if gt c 0
241ret sub c 1
242end
243ret 0
244end
245k = add c 1
246for j ulong k bound 1048576
247x = sub c j
248if ne get ssync add gs x 0
249if le pct add gs x lim
250ret x
251end
252end
253end
254ret 0
255end
256
257note plast: end (exclusive) of the contiguous decode run. whole (the trim ends at or after the
258note track's presentation end): every sample, so the tail stays. Otherwise one past the last
259note decode sample from x0 on whose composition time is below mtb; the run is contiguous in
260note decode order, so every reference a kept sample needs (earlier in decode order) is kept.
261fn plast s ulong x0 ulong mtb ulong whole bool to ulong
262n = tg s KNS
263if le n x0
264quit 36 eempty
265end
266if whole
267ret n
268end
269gs = tg s KSB
270lim = plong mtb
271e = x0
272m = sub n x0
273for j ulong m bound 1048576
274if lt pct add gs add x0 j lim
275e = add add x0 j 1
276end
277end
278if eq e x0
279quit 36 eempty
280end
281ret e
282end
283
284note pcut: output track for slot s presented over movie [a, b) of the trim [start, ...).
285note Media time at a is rounded down and at b up: composition times are integers, so the sample
286note shown at movie time a is the same as at the exact (fractional) media time, and the output
287note edit keeps the exact movie-tick START (empty edit a - start, then b - a ticks of media).
288note The output decode timeline starts at the first kept sample (decode time r0 becomes 0), so
289note the edit's media time is mta - r0; negative (a composition offset before the first decoded
290note sample) is refused 33.
291fn pmt s ulong x ulong up bool to ulong
292d = sub x tg s KE0
293ret padd tg s KMT pmd d tg s KTS get pmts 0 up
294end
295
296note pend: end of the kept decode run of slot s for a trim ending at movie time b.
297fn pend s ulong x0 ulong b ulong to ulong
298we = padd tg s KE0 tg s KED
299mtb = pmt s b true
300ret plast s x0 mtb eq b we
301end
302
303note pmedia: the output edit's media time, mta less the first kept sample's decode time.
304fn pmedia s ulong x0 ulong mta ulong to long
305d0 = plong get sdts add tg s KSB x0
306m = sub plong mta d0
307if lt m 0
308quit 33 eunsup
309end
310ret m
311end
312
313fn pcut s ulong a ulong b ulong start ulong to void
314mta = pmt s a false
315x0 = pfirst s mta eq a tg s KE0
316x1 = pend s x0 b
317t = pnew s
318ptake t s x0 x1
319m = pmedia s x0 mta
320if gt a start
321pedit t sub a start -1
322end
323pedit t sub b a m
324pset t PTKD sub b start
325end
326
327fn ptrack s ulong start ulong finish ulong to void
328prange s
329ws = tg s KE0
330we = padd ws tg s KED
331a = start
332if gt ws a
333a = ws
334end
335b = finish
336if lt we b
337b = we
338end
339if ge a b
340quit 36 eempty
341end
342pcut s a b start
343end
344
345note ptrim: START and END in movie ticks of file f. Refused 35 unless START < END and START is
346note below the movie duration; END may pass the duration (each track ends at its own end).
347note Every track is kept (main refuses 32 for non-media handlers; prange checks KKEEP too).
348fn ptrim f ulong start ulong finish ulong to void
349pinit f
350if ge start finish
351quit 35 erange
352end
353if ge start fget f FMDUR
354quit 35 erange
355end
356nt = fget f FNTK
357if eq nt 0
358quit 22 emiss
359end
360if gt nt NTF
361quit 34 ebound
362end
363base = fget f FTK0
364for t ulong nt bound 8
365ptrack add base t start finish
366end
367pfin
368end
369
370note ---- join ----
371note pconv: movie ticks of file k to output movie ticks (file 0's mvhd timescale), exactly;
372note an inexact conversion is refused 43 (timescales differ).
373fn pconv k ulong v ulong to ulong
374mk = fget k FMTS
375m0 = get pmts 0
376if eq mk m0
377ret v
378end
379lo = pmd v m0 mk false
380if ne lo pmd v m0 mk true
381quit 43 ejts
382end
383ret lo
384end
385
386note plen: the presentation length of file k: its movie duration or its longest track
387note presentation (KE0 + KED), whichever is longer, in output movie ticks.
388fn plen k ulong to ulong
389v = fget k FMDUR
390s0 = fget k FTK0
391for t ulong fget k FNTK bound 8
392e = padd tg add s0 t KE0 tg add s0 t KED
393if gt e v
394v = e
395end
396end
397ret pconv k v
398end
399
400note pdiff: true when the two sample description boxes (stsd, 8.5.2) are not byte-equal.
401fn pdiff a ulong c ulong to bool
402n = tg a KSDS
403if ne n tg c KSDS
404ret true
405end
406if gt n SDCAP
407quit 34 ebound
408end
409o = ulong 0
410while lt o n bound 1048576
411m = sub n o
412if gt m 65536
413m = 65536
414end
415pldsa a o m
416pldsb c o m
417if pcmp m
418ret true
419end
420o = add o m
421end
422ret false
423end
424
425note pldsa / pldsb: m bytes of slot s's stsd from offset o into pca / pcb.
426fn pldsa s ulong o ulong m ulong to void
427p = plong padd tg s KSDP o
428r = pread get ffd tg s KFILE pca 0 m p
429if ne r plong m
430quit 60 eio
431end
432end
433
434fn pldsb s ulong o ulong m ulong to void
435p = plong padd tg s KSDP o
436r = pread get ffd tg s KFILE pcb 0 m p
437if ne r plong m
438quit 60 eio
439end
440end
441
442fn pcmp m ulong to bool
443for j ulong m bound 65536
444if ne get pca j get pcb j
445ret true
446end
447end
448ret false
449end
450
451note pjcheck: track s of a later input must match track s0 of input 0: handler 41, media
452note timescale 43, sample description bytes 42 (the copied stsd describes every part).
453fn pjcheck s0 ulong s ulong to void
454prange s
455if ne tg s KHDLR tg s0 KHDLR
456quit 41 ejhdlr
457end
458if ne tg s KTS tg s0 KTS
459quit 43 ejts
460end
461if pdiff s0 s
462quit 42 ejdesc
463end
464end
465
466note pjpart: append part k (track slot s) starting at output movie time p after the edits that
467note end at cur. Its own edit is kept: an empty edit fills any gap, then its media edit moves by
468note the media already written (PMDUR) less the part's first decode time. Returns the new end.
469fn pjpart tn ulong s ulong k ulong p ulong cur ulong to ulong
470st = padd p pconv k tg s KE0
471if lt st cur
472quit 23 etable
473end
474if gt st cur
475pedit tn sub st cur -1
476end
477ed = pconv k tg s KED
478d0 = ulong 0
479if gt tg s KNS 0
480d0 = get sdts tg s KSB
481end
482m = sub plong padd pget tn PMDUR tg s KMT plong d0
483if lt m 0
484quit 33 eunsup
485end
486if gt ed 0
487pedit tn ed m
488end
489ptake tn s 0 tg s KNS
490ret padd st ed
491end
492
493fn pjtrack count ulong t ulong to void
494s0 = add fget 0 FTK0 t
495prange s0
496tn = pnew s0
497cur = ulong 0
498p = ulong 0
499for k ulong count bound 16
500s = add fget k FTK0 t
501pjcheck s0 s
502cur = pjpart tn s k p cur
503p = padd p plen k
504end
505pset tn PTKD cur
506end
507
508note pjoin: concatenate already parsed inputs [0, count). Tracks pair by position; every input
509note must have the same track count (40). Output headers come from input 0.
510fn pjoin count ulong to void
511if lt count 1
512quit 1 eusage
513end
514if gt count NF
515quit 34 ebound
516end
517pinit 0
518nt = fget 0 FNTK
519if eq nt 0
520quit 22 emiss
521end
522if gt nt NTF
523quit 34 ebound
524end
525for k ulong count bound 16
526if ne fget k FNTK nt
527quit 40 ejcount
528end
529end
530for t ulong nt bound 8
531pjtrack count t
532end
533pfin
534end
535
536note ---- finish: ctts version, sync count, movie duration, mdat order ----
537note pscan: ctts (8.6.1.3) version 0 holds unsigned offsets, version 1 signed; both 32-bit.
538note Offsets that do not fit are refused 33. All offsets zero: no ctts.
539fn pscan t ulong to void
540q0 = pget t PSB
541n = pget t PNS
542lo = long 0
543hi = long 0
544nsyn = ulong 0
545for j ulong n bound 1048576
546g = get pidx add q0 j
547c = get scto g
548if lt c lo
549lo = c
550end
551if gt c hi
552hi = c
553end
554if ne get ssync g 0
555nsyn = add nsyn 1
556end
557end
558pset t PNSYN nsyn
559pset t PCTV 0
560if lt lo 0
561if lt lo -2147483648
562quit 33 eunsup
563end
564if gt hi 2147483647
565quit 33 eunsup
566end
567pset t PCTV 2
568ret
569end
570if gt hi 0
571if gt hi 4294967295
572quit 33 eunsup
573end
574pset t PCTV 1
575end
576end
577
578note pkey: decode time as seconds * 2^20 (saturating), for interleaving tracks in mdat.
579fn pkey d ulong ts ulong to ulong
580s = div d ts
581if gt s 1099511627775
582s = 1099511627775
583end
584ret add shl s 20 div shl rem d ts 20 ts
585end
586
587fn pnext to ulong
588nt = get pnt 0
589sel = nt
590sk = ulong 0
591for t ulong nt bound 8
592if lt get pcur t pget t PNS
593kv = pkey get pdts t pget t PTS
594if eq sel nt
595sel = t
596sk = kv
597end
598if lt kv sk
599sel = t
600sk = kv
601end
602end
603end
604if eq sel nt
605quit 23 etable
606end
607ret sel
608end
609
610note porder: mdat order interleaves tracks by decode time (one sample per chunk, 8.7.4); pofs
611note is each sample's offset from the first mdat payload byte.
612fn porder to void
613for t ulong 8
614put pcur t 0
615put pdts t 0
616end
617ofs = ulong 0
618for k ulong get pn 0 bound 1048576
619t = pnext
620j = add pget t PSB get pcur t
621g = get pidx j
622put pord k j
623put pofs j ofs
624ofs = padd ofs ulong get ssz g
625put pdts t padd get pdts t ulong get sdur g
626put pcur t add get pcur t 1
627end
628put ppay 0 ofs
629end
630
631fn pfin to void
632md = ulong 0
633for t ulong get pnt 0 bound 8
634pscan t
635if gt pget t PTKD md
636md = pget t PTKD
637end
638end
639put pmdur 0 md
640porder
641end