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

1173 lines · 26653 bytes Raw Blame History
1note omega-mp4 (command ovideo), src/box.om: the box parser. Entry: bparse f (after openin arg f).
2note Helpers, constants and globals start with b / B. Sources: ISO/IEC 14496-12 (ISO base media file
3note format), cited "14496-12 n.n.n" (clause numbers of editions 5 and 6); the QuickTime File Format
4note uses the same box (atom) syntax for every box read here; ISO/IEC 23001-7 (common encryption) for
5note pssh and senc. bparse fills the file fields fl (fset), one track slot per trak (tset), the edit
6note entries eld/elm/elr and the sample arrays soff/ssz/sdts/scto/sdur/ssync, then advances ns.
7note Refusals (exit code, io.om text):
8note 20 ebox box header below its header length, past its parent or the file, a box version or a
9note fixed field the standard does not allow (timescale 0, track_ID 0, data reference 0)
10note 22 emiss a required box missing or repeated (ftyp, moov, mvhd, trak, tkhd, mdia, mdhd, hdlr,
11note minf, stbl, stsd, stts, stsc, stsz, stco/co64; also edts, elst, dinf, dref and the
12note minf media header when repeated)
13note 23 etable sample table counts disagree, a count exceeds its box, stss/stsc order, a sample
14note description index other than 1, a decode time or offset past 2^64 - 1, or a chunk
15note whose samples do not lie inside one mdat payload
16note 30 efrag fragmented: moof, mfra, styp, sidx or ssix at top level, mvex in moov
17note 31 eenc protected: sample entry encv, enca, enct, encs, encf, encm or resv, pssh in moov,
18note senc in stbl
19note 33 eunsup stz2, more than one sample description, a data reference without the self-contained
20note flag, an edit list entry with media_time below -1 or a media edit at a rate other
21note than 1 (any list of empty edits and rate-1 media edits is accepted; see bedit)
22note 34 ebound/edepth more than 8 traks, 128 track slots, 64 edit entries, 1048576 samples or
23note chunks or table entries, 65536 boxes visited, a 1 MiB sample description box
24note 60 eio read failure (io.om need)
25note Every count read from a box is checked against the bytes of that box before a loop uses it, every
26note index is checked against its bound before the array is indexed, and every sum (decode time,
27note sample offset) is checked against 2^64 - 1 before it is formed. Box walks follow box sizes from
28note the top of the file; nothing is found by searching bytes for a type.
29
30note Box types: the four ASCII bytes of the type read as one big-endian 32-bit number (14496-12 4.2).
31const BFTYP ulong 1718909296
32const BMOOV ulong 1836019574
33const BMDAT ulong 1835295092
34const BMOOF ulong 1836019558
35const BMFRA ulong 1835430497
36const BSTYP ulong 1937013104
37const BSIDX ulong 1936286840
38const BSSIX ulong 1936943480
39const BMVEX ulong 1836475768
40const BMVHD ulong 1836476516
41const BTRAK ulong 1953653099
42const BTKHD ulong 1953196132
43const BEDTS ulong 1701082227
44const BELST ulong 1701606260
45const BMDIA ulong 1835297121
46const BMDHD ulong 1835296868
47const BHDLR ulong 1751411826
48const BMINF ulong 1835626086
49const BSTBL ulong 1937007212
50const BSTSD ulong 1937011556
51const BSTTS ulong 1937011827
52const BCTTS ulong 1668576371
53const BSTSS ulong 1937011571
54const BSTSC ulong 1937011555
55const BSTSZ ulong 1937011578
56const BSTZ2 ulong 1937013298
57const BSTCO ulong 1937007471
58const BCO64 ulong 1668232756
59const BVMHD ulong 1986881636
60const BSMHD ulong 1936549988
61const BHMHD ulong 1752000612
62const BNMHD ulong 1852663908
63const BSTHD ulong 1937008740
64const BGMHD ulong 1735223396
65const BDINF ulong 1684631142
66const BDREF ulong 1685218662
67const BUUID ulong 1970628964
68const BPSSH ulong 1886614376
69const BSENC ulong 1936027235
70const BENCV ulong 1701733238
71const BENCA ulong 1701733217
72const BENCT ulong 1701733236
73const BENCS ulong 1701733235
74const BENCF ulong 1701733222
75const BENCM ulong 1701733229
76const BRESV ulong 1919251318
77const BVIDE ulong 1986618469
78const BSOUN ulong 1936684398
79
80note bhd: the last header read by bhdr: type, header length, box size, payload length.
81global bhd ulong 4
82note bmdp, bmde: payload start and end of each mdat box of the input being parsed, in file order
83note (so both ascend); bnmd their count (at most 65536: every mdat is a visited box).
84global bmdp ulong 65536
85global bmde ulong 65536
86global bnmd ulong 1
87note bdrc: entry count of the dref box of the track being parsed, 0 when its minf has none.
88global bdrc ulong 1
89note bch: the chunk offsets (stco/co64) of the track being parsed.
90global bch ulong 1048576
91note bdt: the running decode time of bstts. bsc: the stsc walk (0 the next track sample, 1 the
92note previous entry's first_chunk, 2 its samples_per_chunk).
93global bdt ulong 1
94global bsc ulong 3
95
96note ---- box headers ----
97note bhdr: the box at p inside a parent that ends at e (14496-12 4.2): size (32), type (32); size 1:
98note largesize (64) follows; size 0: the box runs to the end of the file, accepted only when its
99note parent ends there; type uuid: a 16-byte extended type follows. The size must cover the header
100note and stay inside the parent. Counts one visited box against NB.
101fn bhdr f ulong p ulong e ulong to void
102put nbox 0 add get nbox 0 1
103if gt get nbox 0 NB
104quit 34 edepth
105end
106if gt p e
107quit 20 ebox
108end
109if lt sub e p 8
110quit 20 ebox
111end
112s = rd32 f p
113t = rd32 f add p 4
114h = ulong 8
115if eq s 1
116if lt sub e p 16
117quit 20 ebox
118end
119s = rd64 f add p 8
120h = 16
121else
122if eq s 0
123if ne e fget f FSZ
124quit 20 ebox
125end
126s = sub e p
127end
128end
129if eq t BUUID
130h = add h 16
131end
132if lt s h
133quit 20 ebox
134end
135if gt s sub e p
136quit 20 ebox
137end
138put bhd 0 t
139put bhd 1 h
140put bhd 2 s
141put bhd 3 sub s h
142end
143
144note bneed: a payload of h bytes must hold at least n (the fixed fields of its box).
145fn bneed n ulong h ulong to void
146if lt h n
147quit 20 ebox
148end
149end
150
151note bonce: a box that may appear once was already seen (v is its recorded size, nonzero once seen).
152fn bonce v ulong to void
153if ne v 0
154quit 22 emiss
155end
156end
157
158note breq: the box recorded at size field k of track slot sl is required.
159fn breq sl ulong k ulong to void
160if eq tg sl k 0
161quit 22 emiss
162end
163end
164
165note bloc: record the box at a, size z, at location field k (size field k + 1) of slot sl, once.
166fn bloc sl ulong k ulong a ulong z ulong to void
167bonce tg sl add k 1
168tset sl k a
169tset sl add k 1 z
170end
171
172note bopen: re-read the whole box recorded at fields k, k + 1 of slot sl; its payload start (the
173note payload length is in bhd 3).
174fn bopen f ulong sl ulong k ulong to ulong
175p = tg sl k
176bhdr f p add p tg sl add k 1
177ret add p get bhd 1
178end
179
180note bcnt: a table with 8 bytes of version, flags and entry_count, then c entries of w bytes: c must
181note fit the payload (code 23) and the 1048576 entry bound (code 34).
182fn bcnt c ulong w ulong to void
183if lt get bhd 3 8
184quit 20 ebox
185end
186if gt c div sub get bhd 3 8 w
187quit 23 etable
188end
189if gt c NS
190quit 34 ebound
191end
192end
193
194note bsx32: a 32-bit two's complement field (read unsigned) as a signed value.
195fn bsx32 x ulong to long
196y = bitcast long x
197if ge x 2147483648
198y = sub y 4294967296
199end
200ret y
201end
202
203note bfragt: top-level boxes of fragmented or segmented files: moof (8.8.4), mfra (8.8.9), styp
204note (8.16.2), sidx (8.16.3), ssix (8.16.4).
205fn bfragt t ulong to ulong
206if or eq t BMOOF eq t BMFRA
207ret 1
208end
209if or eq t BSTYP eq t BSIDX
210ret 1
211end
212if eq t BSSIX
213ret 1
214end
215ret 0
216end
217
218note benct: protected sample entry types: encv, enca, enct, encs (14496-12 8.12: the original
219note format moves into sinf and the entry type signals protection), encf and encm (also refused as
220note protected, conservatively), resv (restricted scheme, 14496-12 8.15).
221fn benct t ulong to ulong
222if or eq t BENCV eq t BENCA
223ret 1
224end
225if or eq t BENCT eq t BENCS
226ret 1
227end
228if or eq t BENCF eq t BENCM
229ret 1
230end
231if eq t BRESV
232ret 1
233end
234ret 0
235end
236
237note bmhdt: media header boxes of minf: vmhd (12.1.2), smhd (12.2.2), hmhd (12.4.2), nmhd (8.4.5.2),
238note sthd (12.6.2) and the QuickTime base media header gmhd.
239fn bmhdt t ulong to ulong
240if or eq t BVMHD eq t BSMHD
241ret 1
242end
243if or eq t BHMHD eq t BNMHD
244ret 1
245end
246if or eq t BSTHD eq t BGMHD
247ret 1
248end
249ret 0
250end
251
252note ---- entry ----
253note bparse: walk the top level, then moov. The caller has run openin for file f.
254fn bparse f ulong to void
255if ge f NF
256quit 34 ebound
257end
258put nbox 0 0
259put bnmd 0 0
260btop f
261bmoov f
262end
263
264note btop: the top-level boxes in file order (14496-12 4.2, 6.2): ftyp (4.3) and moov (8.2.1) once
265note each and required; every mdat (8.1.1) payload extent recorded; fragment boxes refused 30; any
266note other box (free, skip, wide, udta, meta, uuid ...) is skipped by its size.
267fn btop f ulong to void
268z = fget f FSZ
269p = ulong 0
270while lt p z bound 65537
271bhdr f p z
272t = get bhd 0
273h = get bhd 1
274s = get bhd 2
275if eq t BFTYP
276bonce fget f FFTS
277fset f FFTP p
278fset f FFTS s
279end
280if eq t BMOOV
281bonce fget f FMOOVS
282fset f FMOOVP p
283fset f FMOOVS s
284end
285if eq t BMDAT
286bmdat f add p h add p s
287end
288if eq bfragt t 1
289fset f FFRAG 1
290quit 30 efrag
291end
292p = add p s
293end
294if eq fget f FFTS 0
295quit 22 emiss
296end
297if eq fget f FMOOVS 0
298quit 22 emiss
299end
300end
301
302note bmdat: one mdat payload [a, b); FMDATP is the first payload, FNMDAT the count.
303fn bmdat f ulong a ulong b ulong to void
304n = get bnmd 0
305if ge n NB
306quit 34 edepth
307end
308put bmdp n a
309put bmde n b
310if eq n 0
311fset f FMDATP a
312end
313put bnmd 0 add n 1
314fset f FNMDAT add n 1
315end
316
317note ---- moov (8.2.1) ----
318note bmoov: mvhd once (required), trak at least once (at most 8), mvex (8.8.1) refused 30, pssh
319note (23001-7 8.1) refused 31; other children (udta, meta, iods ...) skipped. Then mvhd, then each
320note trak into the next track slot; FTK0 is the first slot of this file, FNTK the count.
321fn bmoov f ulong to void
322a = fget f FMOOVP
323e = add a fget f FMOOVS
324ta = new ulong 8 0
325tz = new ulong 8 0
326bhdr f a e
327q = add a get bhd 1
328n = ulong 0
329while lt q e bound 65537
330bhdr f q e
331t = get bhd 0
332z = get bhd 2
333if eq t BMVHD
334bonce fget f FMVS
335fset f FMVP q
336fset f FMVS z
337end
338if eq t BTRAK
339if ge n NTF
340quit 34 ebound
341end
342put ta n q
343put tz n z
344n = add n 1
345end
346if eq t BMVEX
347fset f FFRAG 1
348quit 30 efrag
349end
350if eq t BPSSH
351quit 31 eenc
352end
353q = add q z
354end
355if eq fget f FMVS 0
356quit 22 emiss
357end
358if eq n 0
359quit 22 emiss
360end
361bmvhd f
362fset f FTK0 get ntk 0
363for i ulong n bound 8
364btrak f get ta i get tz i
365end
366fset f FNTK n
367end
368
369note bmvhd (8.2.2): FullBox; version 1: creation_time (64), modification_time (64), timescale (32),
370note duration (64); version 0: 32, 32, 32, 32; then rate, volume, reserved, matrix, pre_defined,
371note next_track_ID (80 bytes). Payload at least 112 (v1) or 100 (v0) bytes; timescale not 0.
372fn bmvhd f ulong to void
373a = fget f FMVP
374bhdr f a add a fget f FMVS
375q = add a get bhd 1
376bneed 4 get bhd 3
377v = rd8 f q
378ts = ulong 0
379d = ulong 0
380if eq v 1
381bneed 112 get bhd 3
382ts = rd32 f add q 20
383d = rd64 f add q 24
384else
385if ne v 0
386quit 20 ebox
387end
388bneed 100 get bhd 3
389ts = rd32 f add q 12
390d = rd32 f add q 16
391end
392if eq ts 0
393quit 20 ebox
394end
395fset f FMTS ts
396fset f FMDUR d
397fset f FMVV v
398end
399
400note ---- trak (8.3.1) ----
401note btrak: claim the next track slot (fields and edit entries zeroed), then tkhd once (required),
402note edts once (optional), mdia once (required); tref, udta, meta and others skipped.
403fn btrak f ulong a ulong s ulong to void
404sl = get ntk 0
405if ge sl NT
406quit 34 ebound
407end
408for k ulong 64
409tset sl k 0
410put eld add mul sl 64 k 0
411put elm add mul sl 64 k 0
412put elr add mul sl 64 k 0
413end
414put ntk 0 add sl 1
415tset sl KFILE f
416tset sl KTRKP a
417tset sl KTRKS s
418e = add a s
419bhdr f a e
420q = add a get bhd 1
421ea = ulong 0
422ez = ulong 0
423ma = ulong 0
424mz = ulong 0
425while lt q e bound 65537
426bhdr f q e
427t = get bhd 0
428z = get bhd 2
429if eq t BTKHD
430bonce tg sl KTKS
431tset sl KTKP q
432tset sl KTKS z
433end
434if eq t BEDTS
435bonce ez
436ea = q
437ez = z
438end
439if eq t BMDIA
440bonce mz
441ma = q
442mz = z
443end
444q = add q z
445end
446breq sl KTKS
447if eq mz 0
448quit 22 emiss
449end
450btkhd f sl
451if ne ez 0
452bedts f sl ea ez
453end
454bedit sl
455bmdia f sl ma mz
456end
457
458note btkhd (8.3.2): FullBox; version 1: creation_time (64), modification_time (64), track_ID (32),
459note reserved (32), duration (64); version 0: 32 each; then reserved, layer, alternate_group,
460note volume, reserved, matrix, width, height (60 bytes). Payload at least 96 (v1) or 84 (v0) bytes;
461note track_ID cannot be 0 (8.3.2.3). KTKD is the duration in movie timescale units.
462fn btkhd f ulong sl ulong to void
463q = bopen f sl KTKP
464bneed 4 get bhd 3
465v = rd8 f q
466id = ulong 0
467d = ulong 0
468if eq v 1
469bneed 96 get bhd 3
470id = rd32 f add q 20
471d = rd64 f add q 28
472else
473if ne v 0
474quit 20 ebox
475end
476bneed 84 get bhd 3
477id = rd32 f add q 12
478d = rd32 f add q 20
479end
480if eq id 0
481quit 20 ebox
482end
483tset sl KTID id
484tset sl KTKD d
485end
486
487note bedts (8.6.5): the edit box holds at most one edit list box.
488fn bedts f ulong sl ulong a ulong s ulong to void
489e = add a s
490bhdr f a e
491q = add a get bhd 1
492while lt q e bound 65537
493bhdr f q e
494t = get bhd 0
495z = get bhd 2
496if eq t BELST
497bonce tg sl KELS
498tset sl KELP q
499tset sl KELS z
500end
501q = add q z
502end
503if ne tg sl KELS 0
504belst f sl
505end
506end
507
508note belst (8.6.6): FullBox; entry_count (32); per entry, version 1: segment_duration (64, movie
509note timescale), media_time (signed 64, media timescale, -1 = empty edit); version 0: 32 and signed
510note 32; then media_rate_integer (16) and media_rate_fraction (16), kept as one fixed 16.16 value.
511note The count must fit the payload (code 20) and the 64-entry bound (code 34).
512fn belst f ulong sl ulong to void
513o = bopen f sl KELP
514bneed 8 get bhd 3
515v = rd8 f o
516w = ulong 12
517if eq v 1
518w = 20
519else
520if ne v 0
521quit 20 ebox
522end
523end
524c = rd32 f add o 4
525if gt c div sub get bhd 3 8 w
526quit 20 ebox
527end
528if gt c NE
529quit 34 ebound
530end
531tset sl KNE c
532tset sl KEDM 1
533b = mul sl 64
534o = add o 8
535for i ulong c bound 64
536d = ulong 0
537m = long 0
538if eq v 1
539d = rd64 f o
540m = bitcast long rd64 f add o 8
541o = add o 16
542else
543d = rd32 f o
544m = bsx32 rd32 f add o 4
545o = add o 8
546end
547put eld add b i d
548put elm add b i m
549put elr add b i rd32 f o
550o = add o 4
551end
552end
553
554note bedit: classify the edit list (8.6.6) into KEDM and the normalized fields KE0, KMT, KED.
555note Every entry must be an empty edit (media_time -1, rate not checked) or a media edit (media_time
556note >= 0) at rate 1.0 (fixed 16.16 65536); any other entry is refused 33. Then:
557note KEDM 0 no edit list: KE0 0, KMT 0, KED the tkhd duration.
558note KEDM 1 simple: [one empty edit] then exactly one media edit: KE0 the empty edit's
559note segment_duration (0 without one), KMT the media edit's media_time, KED its
560note segment_duration.
561note KEDM 2 complex: any other accepted list (no entries, only empty edits, several media edits,
562note an empty edit after a media edit): KE0, KMT and KED are 0; the entries stay in
563note eld/elm/elr. info and dump accept it; trim and join refuse it (plan.om, code 33).
564fn bedit sl ulong to void
565if eq tg sl KEDM 0
566tset sl KE0 0
567tset sl KMT 0
568tset sl KED tg sl KTKD
569ret
570end
571c = tg sl KNE
572b = mul sl 64
573for i ulong c bound 64
574m = get elm add b i
575if lt m -1
576quit 33 eunsup
577end
578if ge m 0
579if ne get elr add b i 65536
580quit 33 eunsup
581end
582end
583end
584tset sl KE0 0
585tset sl KMT 0
586tset sl KED 0
587j = ulong 0
588e0 = ulong 0
589if gt c 0
590if eq get elm b -1
591e0 = get eld b
592j = 1
593end
594end
595if ne sub c j 1
596tset sl KEDM 2
597ret
598end
599k = add b j
600if lt get elm k 0
601tset sl KEDM 2
602ret
603end
604tset sl KE0 e0
605tset sl KMT bitcast ulong get elm k
606tset sl KED get eld k
607end
608
609note ---- mdia (8.4.1) ----
610note bmdia: mdhd, hdlr and minf, each exactly once.
611fn bmdia f ulong sl ulong a ulong s ulong to void
612e = add a s
613bhdr f a e
614q = add a get bhd 1
615ma = ulong 0
616mz = ulong 0
617while lt q e bound 65537
618bhdr f q e
619t = get bhd 0
620z = get bhd 2
621if eq t BMDHD
622bonce tg sl KMDHS
623tset sl KMDHP q
624tset sl KMDHS z
625end
626if eq t BHDLR
627bonce tg sl KHDLS
628tset sl KHDLP q
629tset sl KHDLS z
630end
631if eq t BMINF
632bonce mz
633ma = q
634mz = z
635end
636q = add q z
637end
638breq sl KMDHS
639breq sl KHDLS
640if eq mz 0
641quit 22 emiss
642end
643bmdhd f sl
644bhdlr f sl
645bminf f sl ma mz
646end
647
648note bmdhd (8.4.2): FullBox; version 1: creation_time (64), modification_time (64), timescale (32),
649note duration (64); version 0: 32 each; then pad + language (16), pre_defined (16). Payload at least
650note 36 (v1) or 24 (v0) bytes; timescale not 0.
651fn bmdhd f ulong sl ulong to void
652q = bopen f sl KMDHP
653bneed 4 get bhd 3
654v = rd8 f q
655ts = ulong 0
656d = ulong 0
657if eq v 1
658bneed 36 get bhd 3
659ts = rd32 f add q 20
660d = rd64 f add q 24
661else
662if ne v 0
663quit 20 ebox
664end
665bneed 24 get bhd 3
666ts = rd32 f add q 12
667d = rd32 f add q 16
668end
669if eq ts 0
670quit 20 ebox
671end
672tset sl KTS ts
673tset sl KMDUR d
674end
675
676note bhdlr (8.4.3): FullBox; pre_defined (32); handler_type (32); reserved (3 x 32); name. The
677note QuickTime handler (component type, subtype, manufacturer, flags, mask, name) has its subtype at
678note the same offset. KKEEP is 1 for vide and soun.
679fn bhdlr f ulong sl ulong to void
680q = bopen f sl KHDLP
681bneed 24 get bhd 3
682h = rd32 f add q 8
683tset sl KHDLR h
684k = ulong 0
685if or eq h BVIDE eq h BSOUN
686k = 1
687end
688tset sl KKEEP k
689end
690
691note bminf (8.4.4): one media header (bmhdt; optional, recorded once as KMINP/KMINS/KMINH), dinf
692note (8.7.1, optional, once), stbl (8.5.1, required, once). A QuickTime data handler hdlr here is
693note skipped: the media handler is the one in mdia.
694fn bminf f ulong sl ulong a ulong s ulong to void
695e = add a s
696bhdr f a e
697q = add a get bhd 1
698put bdrc 0 0
699dn = ulong 0
700while lt q e bound 65537
701bhdr f q e
702t = get bhd 0
703z = get bhd 2
704if eq bmhdt t 1
705bonce tg sl KMINS
706tset sl KMINP q
707tset sl KMINS z
708tset sl KMINH t
709end
710if eq t BDINF
711bonce dn
712dn = 1
713bdinf f q z
714end
715if eq t BSTBL
716bonce tg sl KSTBS
717tset sl KSTBP q
718tset sl KSTBS z
719end
720q = add q z
721end
722breq sl KSTBS
723bstbl f sl
724end
725
726note bdinf (8.7.1): at most one dref.
727fn bdinf f ulong a ulong s ulong to void
728e = add a s
729bhdr f a e
730q = add a get bhd 1
731dn = ulong 0
732while lt q e bound 65537
733bhdr f q e
734t = get bhd 0
735z = get bhd 2
736if eq t BDREF
737bonce dn
738dn = 1
739bdref f q z
740end
741q = add q z
742end
743end
744
745note bdref (8.7.2): FullBox; entry_count (32), at least 1 and each entry a FullBox of at least 12
746note bytes (url , urn , QuickTime alis); flag 1 means the media data is in this file. An entry
747note without it is an external data reference: refused 33.
748fn bdref f ulong a ulong s ulong to void
749e = add a s
750bhdr f a e
751q = add a get bhd 1
752bneed 8 get bhd 3
753c = rd32 f add q 4
754if eq c 0
755quit 20 ebox
756end
757if gt c div sub get bhd 3 8 12
758quit 20 ebox
759end
760if gt c NB
761quit 34 edepth
762end
763o = add q 8
764for i ulong c bound 65536
765bhdr f o e
766if lt get bhd 3 4
767quit 20 ebox
768end
769g = rd32 f add o get bhd 1
770if eq and g 1 0
771quit 33 eunsup
772end
773o = add o get bhd 2
774end
775put bdrc 0 c
776end
777
778note ---- stbl (8.5.1) ----
779note bstbl: record stsd, stts, ctts, stss, stsc, stsz, stco or co64 (each once; stco and co64 share
780note one location, so both present is a repeat); stz2 (8.7.3.3) refused 33; senc (23001-7 7.2)
781note refused 31; other children (sdtp, sbgp, sgpd, subs, cslg, stps, saiz, saio) skipped. Then the
782note tables in an order where each pass reads one table front to back (the input window is 1 MiB).
783fn bstbl f ulong sl ulong to void
784q = bopen f sl KSTBP
785e = add tg sl KSTBP tg sl KSTBS
786while lt q e bound 65537
787bhdr f q e
788t = get bhd 0
789z = get bhd 2
790if eq t BSTSD
791bloc sl KSDP q z
792end
793if eq t BSTTS
794bloc sl KSTTP q z
795end
796if eq t BCTTS
797bloc sl KCTP q z
798end
799if eq t BSTSS
800bloc sl KSSP q z
801end
802if eq t BSTSC
803bloc sl KSCP q z
804end
805if eq t BSTSZ
806bloc sl KSZP q z
807end
808if eq t BSTCO
809bloc sl KCOP q z
810tset sl KCO 32
811end
812if eq t BCO64
813bloc sl KCOP q z
814tset sl KCO 64
815end
816if eq t BSTZ2
817quit 33 eunsup
818end
819if eq t BSENC
820tset sl KENC 1
821quit 31 eenc
822end
823q = add q z
824end
825breq sl KSDS
826breq sl KSTTS
827breq sl KSCS
828breq sl KSZS
829breq sl KCOS
830bstsd f sl
831bstsz f sl
832bstts f sl
833bctts f sl
834bstss f sl
835bstco f sl
836bstsc f sl
837put ns 0 add tg sl KSB tg sl KNS
838end
839
840note bstsd (8.5.2): FullBox; entry_count (32), exactly 1 (0: 22, more: 33); the entry is a box
841note (8.5.2.2 SampleEntry: 6 reserved bytes, data_reference_index (16), format fields). Protected
842note or restricted entry types refuse 31. data_reference_index is 1 .. the dref entry count. The
843note whole stsd box is at most SDCAP bytes. KCC is the entry type.
844fn bstsd f ulong sl ulong to void
845if gt tg sl KSDS SDCAP
846quit 34 ebound
847end
848q = bopen f sl KSDP
849e = add tg sl KSDP tg sl KSDS
850bneed 8 get bhd 3
851c = rd32 f add q 4
852if eq c 0
853quit 22 emiss
854end
855if gt c 1
856quit 33 eunsup
857end
858o = add q 8
859bhdr f o e
860t = get bhd 0
861h = get bhd 1
862if eq benct t 1
863tset sl KENC 1
864quit 31 eenc
865end
866bneed 8 get bhd 3
867r = rd16 f add add o h 6
868if eq r 0
869quit 20 ebox
870end
871d = get bdrc 0
872if ne d 0
873if gt r d
874quit 20 ebox
875end
876end
877tset sl KCC t
878end
879
880note bstsz (8.7.3.2): FullBox; sample_size (32); sample_count (32); when sample_size is 0,
881note sample_count entry_size values (32) follow. KNS is sample_count, KSB the first global sample.
882fn bstsz f ulong sl ulong to void
883o = bopen f sl KSZP
884bneed 12 get bhd 3
885k = rd32 f add o 4
886c = rd32 f add o 8
887if eq k 0
888if gt c div sub get bhd 3 12 4
889quit 23 etable
890end
891end
892b = get ns 0
893if gt c sub NS b
894quit 34 ebound
895end
896tset sl KNS c
897tset sl KSB b
898o = add o 12
899for i ulong c bound 1048576
900x = k
901if eq k 0
902x = rd32 f o
903o = add o 4
904end
905put ssz add b i uint x
906end
907end
908
909note bstts (8.6.1.2): FullBox; entry_count (32); entries sample_count (32), sample_delta (32). The
910note counts add up to the stsz sample count; DT(n+1) = DT(n) + delta(n), DT(0) = 0.
911fn bstts f ulong sl ulong to void
912o = bopen f sl KSTTP
913bneed 8 get bhd 3
914c = rd32 f add o 4
915bcnt c 8
916o = add o 8
917n = tg sl KNS
918b = tg sl KSB
919j = ulong 0
920put bdt 0 0
921for i ulong c bound 1048576
922a = rd32 f o
923if gt a sub n j
924quit 23 etable
925end
926bdts add b j a rd32 f add o 4
927j = add j a
928o = add o 8
929end
930if ne j n
931quit 23 etable
932end
933end
934
935note bdts: global samples s .. s + a - 1 get duration x and decode times from bdt; bdt advances.
936fn bdts s ulong a ulong x ulong to void
937t = get bdt 0
938for k ulong a bound 1048576
939put sdts add s k t
940put sdur add s k uint x
941if gt x sub 18446744073709551615 t
942quit 23 etable
943end
944t = add t x
945end
946put bdt 0 t
947end
948
949note bctts (8.6.1.3): FullBox (version 0 or 1); entry_count (32); entries sample_count (32),
950note sample_offset (version 0 unsigned 32, version 1 signed 32); CT(n) = DT(n) + offset(n). The
951note counts add up to the sample count. No ctts: every offset is 0 (KCTTS 0).
952fn bctts f ulong sl ulong to void
953n = tg sl KNS
954b = tg sl KSB
955if eq tg sl KCTS 0
956bcto b n 0
957ret
958end
959o = bopen f sl KCTP
960bneed 8 get bhd 3
961v = rd8 f o
962if gt v 1
963quit 20 ebox
964end
965tset sl KCTTS 1
966tset sl KCTV v
967c = rd32 f add o 4
968bcnt c 8
969o = add o 8
970j = ulong 0
971for i ulong c bound 1048576
972a = rd32 f o
973if gt a sub n j
974quit 23 etable
975end
976m = bitcast long rd32 f add o 4
977if eq v 1
978m = bsx32 rd32 f add o 4
979end
980bcto add b j a m
981j = add j a
982o = add o 8
983end
984if ne j n
985quit 23 etable
986end
987end
988
989note bcto: global samples s .. s + a - 1 get composition offset m.
990fn bcto s ulong a ulong m long to void
991for k ulong a bound 1048576
992put scto add s k m
993end
994end
995
996note bsyn: global samples s .. s + a - 1 get sync flag y.
997fn bsyn s ulong a ulong y uchar to void
998for k ulong a bound 1048576
999put ssync add s k y
1000end
1001end
1002
1003note bstss (8.6.2): FullBox; entry_count (32); sample_number (32, 1-based) values in strictly
1004note increasing order, each at most the sample count. No stss: every sample is a sync sample.
1005fn bstss f ulong sl ulong to void
1006n = tg sl KNS
1007b = tg sl KSB
1008if eq tg sl KSSS 0
1009bsyn b n 1
1010tset sl KNSYN n
1011ret
1012end
1013bsyn b n 0
1014tset sl KSTSS 1
1015o = bopen f sl KSSP
1016bneed 8 get bhd 3
1017c = rd32 f add o 4
1018bcnt c 4
1019if gt c n
1020quit 23 etable
1021end
1022o = add o 8
1023l = ulong 0
1024for k ulong c bound 1048576
1025x = rd32 f o
1026if le x l
1027quit 23 etable
1028end
1029if gt x n
1030quit 23 etable
1031end
1032put ssync add b sub x 1 1
1033l = x
1034o = add o 4
1035end
1036tset sl KNSYN c
1037end
1038
1039note bstco (8.7.5): FullBox; entry_count (32); chunk_offset values of 32 bits (stco) or 64 bits
1040note (co64), file offsets, read into bch. KNCH is the chunk count (at most NC).
1041fn bstco f ulong sl ulong to void
1042o = bopen f sl KCOP
1043bneed 8 get bhd 3
1044c = rd32 f add o 4
1045w = ulong 4
1046if eq tg sl KCO 64
1047w = 8
1048end
1049bcnt c w
1050if gt c NC
1051quit 34 ebound
1052end
1053o = add o 8
1054for i ulong c bound 1048576
1055x = ulong 0
1056if eq w 8
1057x = rd64 f o
1058else
1059x = rd32 f o
1060end
1061put bch i x
1062o = add o w
1063end
1064tset sl KNCH c
1065end
1066
1067note bstsc (8.7.4): FullBox; entry_count (32); entries first_chunk (32, 1-based),
1068note samples_per_chunk (32), sample_description_index (32). The first entry's first_chunk is 1, they
1069note increase strictly and stay within the chunk count; each entry covers its first_chunk up to the
1070note next entry's first_chunk - 1, the last one through the last chunk. The samples, in decode
1071note order, fill the chunks in order and must number exactly the sample count. The only sample
1072note description index allowed is 1 (bstsd accepts one entry).
1073fn bstsc f ulong sl ulong to void
1074o = bopen f sl KSCP
1075bneed 8 get bhd 3
1076c = rd32 f add o 4
1077bcnt c 12
1078o = add o 8
1079put bsc 0 0
1080put bsc 1 0
1081put bsc 2 0
1082for i ulong c bound 1048576
1083bsce f sl o i
1084o = add o 12
1085end
1086if gt c 0
1087brun sl sub get bsc 1 1 tg sl KNCH get bsc 2
1088end
1089if ne get bsc 0 tg sl KNS
1090quit 23 etable
1091end
1092end
1093
1094note bsce: stsc entry i at o. Its first_chunk is checked (1 for the first entry, above the previous
1095note one, at most the chunk count) before the previous entry's chunks are laid out.
1096fn bsce f ulong sl ulong o ulong i ulong to void
1097a = rd32 f o
1098if ne rd32 f add o 8 1
1099quit 23 etable
1100end
1101if gt a tg sl KNCH
1102quit 23 etable
1103end
1104l = get bsc 1
1105if eq i 0
1106if ne a 1
1107quit 23 etable
1108end
1109else
1110if le a l
1111quit 23 etable
1112end
1113brun sl sub l 1 sub a 1 get bsc 2
1114end
1115put bsc 1 a
1116put bsc 2 rd32 f add o 4
1117end
1118
1119note brun: chunks c0 .. c1 - 1 (0-based, c1 <= KNCH) of slot sl hold m samples each, from track
1120note sample bsc 0, which advances. Each chunk with samples must lie inside one mdat payload.
1121fn brun sl ulong c0 ulong c1 ulong m ulong to void
1122b = tg sl KSB
1123n = tg sl KNS
1124k = get bsc 0
1125for c ulong sub c1 c0 bound 1048576
1126if gt m sub n k
1127quit 23 etable
1128end
1129o = get bch add c0 c
1130if gt m 0
1131bmdin o bchunk add b k m o
1132end
1133k = add k m
1134end
1135put bsc 0 k
1136end
1137
1138note bchunk: global samples s .. s + m - 1 lie back to back from file offset o (8.7.4.1: a sample's
1139note offset is its chunk offset plus the sizes of the earlier samples of the chunk); the end offset.
1140fn bchunk s ulong m ulong o ulong to ulong
1141a = o
1142for i ulong m bound 1048576
1143x = ulong get ssz add s i
1144put soff add s i a
1145if gt x sub 18446744073709551615 a
1146quit 23 etable
1147end
1148a = add a x
1149end
1150ret a
1151end
1152
1153note bmdin: the bytes [a, z) of one chunk must lie inside one mdat payload (binary search over the
1154note ascending payload starts). The samples of a chunk are contiguous and the header of the next box
1155note separates two payloads, so this is the same as checking every sample of the chunk.
1156fn bmdin a ulong z ulong to void
1157lo = ulong 0
1158hi = get bnmd 0
1159while lt lo hi bound 64
1160m = div add lo hi 2
1161if le get bmdp m a
1162lo = add m 1
1163else
1164hi = m
1165end
1166end
1167if eq lo 0
1168quit 23 etable
1169end
1170if gt z get bmde sub lo 1
1171quit 23 etable
1172end
1173end