build-a-video-container-parser / lesson-23.md
Lesson 23 · The sample tables

Chunk offsets

The stco box lists where each chunk begins in the file, as absolute byte offsets. Today you parse that list - the anchor that ties the sample tables back to real positions in the media data.

The goal

Parse an stco box into its list of 32-bit chunk offsets.

Start here - the target
TO DO
Scenario: An stco decodes to a list of chunk offsets
Givenan stco with entry_count 2 and offsets 0x00 0x00 0x00 0x28 then 0x00 0x00 0x08 0x00
Whenthe stco is parsed
Thenthe chunk offsets are [40, 2048]
Andthe entry count read from the box is 2
Background

stco, the chunk-offset box, holds the absolute byte offset of each chunk from the very start of the file. This is the table that finally connects the abstract sample tables to concrete positions: stsc says which chunk a sample is in, and stco says where that chunk physically starts. Here the two chunks begin at byte 40 (0x28) and byte 2048 (0x800).

Store the offsets as uint64 even though they are read as 32-bit values. That is deliberate: the very next lesson introduces co64, the 64-bit variant for files larger than 4 GB, and both tables should feed the same sample-location code. Reading them into the same wide type now means that code never has to care which box supplied the offsets.

Make it work
// FullBox prefix(4) + entry_count(4) + entry_count * offset(4)
func parseStco(payload []byte) []uint64 {
n := readU32(payload[4:8])
// read n 32-bit offsets, widening each into a uint64
}
CheckpointDONE
You can parse 32-bit chunk offsets. Commit and stop here.