Skip to content

Commit

Permalink
sgpd box entries parsing
Browse files Browse the repository at this point in the history
Previously entries were parsed as raw bytes, now KIDs/IVs are
explicitely parsed.
  • Loading branch information
ksa-real committed Jun 21, 2023
1 parent 6ec4def commit e869d8a
Show file tree
Hide file tree
Showing 6 changed files with 869 additions and 814 deletions.
47 changes: 34 additions & 13 deletions format/mp4/boxes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,27 +1367,48 @@ func decodeBox(ctx *decodeContext, d *decode.D, typ string) {
case "sgpd":
version := d.FieldU8("version")
d.FieldU24("flags")
d.FieldUTF8("grouping_type", 4)
var groupingType = d.FieldUTF8("grouping_type", 4)
var defaultLength uint64
if version == 1 {
defaultLength = d.FieldU32("default_length")
}
if version >= 2 {
} else if version >= 2 {
d.FieldU32("default_sample_description_index")
}
entryCount := d.FieldU32("entry_count")
d.FieldArray("entries", func(d *decode.D) {
for i := uint64(0); i < entryCount; i++ {
entryLen := defaultLength
if version == 1 {
if defaultLength == 0 {
entryLen = d.FieldU32("description_length")
} else if entryLen == 0 {
d.Fatalf("sgpd groups entry len <= 0 version 1")
}

d.FieldStructNArray("entries", "entry", int64(entryCount), func(d *decode.D) {
entryLen := defaultLength
if version == 1 {
if defaultLength == 0 {
entryLen = d.FieldU32("description_length")
} else if entryLen == 0 {
d.Fatalf("sgpd groups entry len <= 0")
d.Fatalf("sgpd groups entry len == 0")
}
} else if entryLen == 0 {
// TODO: this is likely a mistake: here version is != 1, so defaultLength is default (i.e. 0),
// TODO: so entryLen is also 0. So for version != 1 this fatal should always throw
d.Fatalf("sgpd groups entry len == 0")
}
// CENC Sample Encryption Info Entries
switch groupingType {
case "seig":
d.FieldU8("reserved")
d.FieldU4("crypto_bytes")
d.FieldU4("skip_bytes")
isEncrypted := d.FieldU8("is_encrypted")
perSampleIVSize := d.FieldU8("per_sample_iv_size")
d.FieldRawLen("kid", 8*16)
if isEncrypted != 0 {
// If perSampleIVSize > 0 then
if perSampleIVSize == 0 {
// This means whole fragment is encrypted with a constant IV
iVSize := d.FieldU8("constant_iv_size")
d.FieldRawLen("constant_iv", 8*int64(iVSize))
}
}
case "roll":
d.FieldU16("roll_distance")
default:
d.FieldRawLen("data", int64(entryLen)*8)
}
})
Expand Down
3 changes: 2 additions & 1 deletion format/mp4/testdata/aac.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ $ fq -d mp4 dv aac.mp4
0x510| 00 00 00 02 | .... | default_length: 2 0x515-0x518.7 (4)
0x510| 00 00 00 01 | .... | entry_count: 1 0x519-0x51c.7 (4)
| | | entries[0:1]: 0x51d-0x51e.7 (2)
0x510| ff ff | .. | [0]: raw bits data 0x51d-0x51e.7 (2)
| | | [0]{}: entry 0x51d-0x51e.7 (2)
0x510| ff ff | .. | roll_distance: 65535 0x51d-0x51e.7 (2)
| | | [6]{}: box 0x51f-0x53a.7 (28)
0x510| 00| .| size: 28 0x51f-0x522.7 (4)
0x520|00 00 1c |... |
Expand Down

0 comments on commit e869d8a

Please sign in to comment.