aboutsummaryrefslogtreecommitdiff
path: root/descriptor.go
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-02-13 11:24:24 +0100
committerOnur Gündüz <ogunduz@gmail.com>2026-02-13 11:28:45 +0100
commitf4b35bb7f4475db77fa6969eb42964824f616fb0 (patch)
tree54046849b6606d8160b563077234c3ff284a96a7 /descriptor.go
parent8502816a943bb33705a7775838c760ed72046ae8 (diff)
downloadmp4-f4b35bb7f4475db77fa6969eb42964824f616fb0.tar.gz
mp4-f4b35bb7f4475db77fa6969eb42964824f616fb0.zip
v2
Diffstat (limited to 'descriptor.go')
-rw-r--r--descriptor.go174
1 files changed, 87 insertions, 87 deletions
diff --git a/descriptor.go b/descriptor.go
index a2574bf..746bec1 100644
--- a/descriptor.go
+++ b/descriptor.go
@@ -1,110 +1,110 @@
package mp4
-// descriptor implements MPEG-4 descriptor parsing for esds boxes.
+import "strconv"
-var tagToName = map[byte]string{
- 0x03: "ESDescriptor",
- 0x04: "DecoderConfigDescriptor",
- 0x05: "DecoderSpecificInfo",
- 0x06: "SLConfigDescriptor",
-}
+// ReadEsdsCodec extracts the MIME codec string from esds box data.
+// It parses the MPEG-4 descriptor chain to find the OTI (Object Type Indication)
+// and audio configuration. Returns a string like "40.2" for AAC-LC.
+func ReadEsdsCodec(data []byte) string {
+ if len(data) < 2 {
+ return ""
+ }
-type descriptor struct {
- tag byte
- tagName string
- length int
- oti byte
- buffer []byte
- children map[string]*descriptor
-}
+ // Expect ESDescriptor (tag 0x03)
+ ptr, end := 0, len(data)
+ if data[ptr] != 0x03 {
+ return ""
+ }
+ ptr++
-func decodeDescriptor(buf []byte, start, end int) *descriptor {
- if start >= end {
- return nil
+ // Skip length bytes (variable-length encoding)
+ ptr = skipDescriptorLength(data, ptr, end)
+ if ptr < 0 || ptr+3 > end {
+ return ""
}
- tag := buf[start]
- ptr := start + 1
- length := 0
- for ptr < end {
- lenByte := buf[ptr]
- ptr++
- length = (length << 7) | int(lenByte&0x7f)
- if lenByte&0x80 == 0 {
- break
+
+ // ES_ID (2 bytes) + stream dependency flags (1 byte)
+ flags := data[ptr+2]
+ ptr += 3
+
+ // Skip optional fields based on flags
+ if flags&0x80 != 0 { // streamDependenceFlag
+ ptr += 2
+ }
+ if flags&0x40 != 0 { // URL_Flag
+ if ptr >= end {
+ return ""
}
+ urlLen := int(data[ptr])
+ ptr += 1 + urlLen
}
-
- tagName := tagToName[tag]
- d := &descriptor{
- tag: tag,
- tagName: tagName,
- length: (ptr - start) + length,
- children: make(map[string]*descriptor),
+ if flags&0x20 != 0 { // OCRstreamFlag
+ ptr += 2
}
- switch tagName {
- case "ESDescriptor":
- decodeESDescriptor(d, buf, ptr, end)
- case "DecoderConfigDescriptor":
- decodeDecoderConfigDescriptor(d, buf, ptr, end)
- case "DecoderSpecificInfo":
- dEnd := ptr + length
- if dEnd > end {
- dEnd = end
- }
- d.buffer = buf[ptr:dEnd]
- default:
- dEnd := min(ptr+length, end)
- d.buffer = buf[ptr:dEnd]
+ if ptr >= end {
+ return ""
}
- return d
-}
+ // Expect DecoderConfigDescriptor (tag 0x04)
+ if data[ptr] != 0x04 {
+ return ""
+ }
+ ptr++
+ ptr = skipDescriptorLength(data, ptr, end)
+ if ptr < 0 || ptr+13 > end {
+ return ""
+ }
-func decodeDescriptorArray(buf []byte, start, end int) map[string]*descriptor {
- m := make(map[string]*descriptor)
- ptr := start
- for ptr+2 <= end {
- desc := decodeDescriptor(buf, ptr, end)
- if desc == nil {
- break
- }
- ptr += desc.length
- name := desc.tagName
- if name == "" {
- continue
- }
- m[name] = desc
+ oti := data[ptr]
+ if oti == 0 {
+ return ""
}
- return m
-}
-func decodeESDescriptor(d *descriptor, buf []byte, start, end int) {
- if start+3 > end {
- return
+ // Format OTI as hex
+ otiStr := hexByte(oti)
+
+ // Skip to DecoderSpecificInfo: OTI(1)+streamType(1)+bufferSizeDB(3)+maxBitrate(4)+avgBitrate(4) = 13
+ ptr += 13
+
+ if ptr >= end || data[ptr] != 0x05 {
+ // No DecoderSpecificInfo, return just OTI
+ return otiStr
}
- flags := buf[start+2]
- ptr := start + 3
- if flags&0x80 != 0 {
- ptr += 2
+ ptr++
+ ptr = skipDescriptorLength(data, ptr, end)
+ if ptr < 0 || ptr >= end {
+ return otiStr
}
- if flags&0x40 != 0 {
- if ptr >= end {
- return
- }
- l := int(buf[ptr])
- ptr += l + 1
+
+ // Extract audio object type from first byte
+ audioConfig := (data[ptr] & 0xf8) >> 3
+ if audioConfig == 0 {
+ return otiStr
}
- if flags&0x20 != 0 {
- ptr += 2
+ return otiStr + "." + strconv.Itoa(int(audioConfig))
+}
+
+// hexByte formats a byte as a lowercase hex string without leading zeros beyond one digit.
+func hexByte(b byte) string {
+ if b < 16 {
+ return string(hexDigit(b))
}
- d.children = decodeDescriptorArray(buf, ptr, end)
+ var buf [2]byte
+ buf[0] = hexDigit(b >> 4)
+ buf[1] = hexDigit(b & 0x0f)
+ return string(buf[:])
}
-func decodeDecoderConfigDescriptor(d *descriptor, buf []byte, start, end int) {
- if start >= end {
- return
+// skipDescriptorLength skips the variable-length descriptor length field.
+// Returns the new position, or -1 on error.
+func skipDescriptorLength(data []byte, ptr, end int) int {
+ for ptr < end {
+ b := data[ptr]
+ ptr++
+ if b&0x80 == 0 {
+ return ptr
+ }
}
- d.oti = buf[start]
- d.children = decodeDescriptorArray(buf, start+13, end)
+ return -1
}