From 27e34124edd89301a00ade4500cdfee185639d2f Mon Sep 17 00:00:00 2001 From: Martin Riedl Date: Thu, 12 Dec 2024 19:23:33 +0100 Subject: [PATCH] feat: chunk offset box --- ChunkOffsetBox.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 ChunkOffsetBox.go diff --git a/ChunkOffsetBox.go b/ChunkOffsetBox.go new file mode 100644 index 0000000..bbeae5e --- /dev/null +++ b/ChunkOffsetBox.go @@ -0,0 +1,87 @@ +// Copyright 2024 Martin Riedl +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gomp4 + +import ( + "encoding/binary" + "fmt" +) + +// ChunkOffsetBox chunk offset box struct +// +// 8.7.5 Chunk Offset Box +// +// Box Type: ‘stco’, ‘co64’ +// Container: Sample Table Box (‘stbl’) +// Mandatory: Yes +// Quantity: Exactly one variant must be present +// +// The chunk offset table gives the index of each chunk into the containing file. There are two variants, +// permitting the use of 32‐bit or 64‐bit offsets. The latter is useful when managing very large +// presentations. At most one of these variants will occur in any single instance of a sample table. +// +// Offsets are file offsets, not the offset into any box within the file (e.g. Media Data Box). This permits +// referring to media data in files without any box structure. It does also mean that care must be taken +// when constructing a self‐contained ISO file with its metadata (Movie Box) at the front, as the size of the +// Movie Box will affect the chunk offsets to the media data. +type ChunkOffsetBox struct { + *FullBox + // is an integer that gives the number of entries in the following table + EntryCount uint32 + EntryList []ChunkOffsetEntry +} + +type ChunkOffsetEntry struct { + // is a 32 or 64 bit integer that gives the offset of the start of a chunk into its + // containing media file. + ChunkOffset uint32 +} + +// BoxTypeChunkOffset Chunk Offset Box +const BoxTypeChunkOffset = "stco" + +func init() { + BoxDefinitions = append(BoxDefinitions, BoxDefinition{ + Type: BoxTypeChunkOffset, + ParentTypes: []string{BoxTypeSampleTable}, + Parser: ParseChunkOffsetBox, + }) +} + +// ParseChunkOffsetBox creates a new chunk offset box struct based on bytes +func ParseChunkOffsetBox(parser *Parser, filePosition uint64, headerSize uint32, content []byte) (any, error) { + box := &ChunkOffsetBox{ + FullBox: newFullBox(&Box{filePosition, headerSize}, content[0:4]), + } + + // parse entry counter + box.EntryCount = binary.BigEndian.Uint32(content[4:8]) + + // parse offset entries + position := 8 + for i := 0; i < int(box.EntryCount); i++ { + newOffsetEntry := ChunkOffsetEntry{ + ChunkOffset: binary.BigEndian.Uint32(content[position+(i*4) : position+4+(i*4)]), + } + box.EntryList = append(box.EntryList, newOffsetEntry) + } + + // validate entries amount + if len(box.EntryList) != int(box.EntryCount) { + return box, fmt.Errorf("invalid amount of offset entries at %d; got %d but expected %d", filePosition, len(box.EntryList), box.EntryCount) + } + + return box, nil +} diff --git a/README.md b/README.md index e318072..4921ee6 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Implementation progress of ISO/IEC 14496-12:2015: | 8.7.2 Data Reference Box | dref, url, urn | 100% | | 8.7.3 Sample Size Boxes | stsz, stz2 | 50% | | 8.7.4 Sample To Chunk Box | stsc | 100% | -| 8.7.5 Chunk Offset Box | stco, co64 | - | +| 8.7.5 Chunk Offset Box | stco, co64 | 50% | | 8.7.6 Padding Bits Box | padb | - | | 8.7.7 Sub-Sample Information Box | subs | - | | 8.7.8 Sample Auxiliary Information Sizes Box | saiz | - |