Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HEVC Encryption support #412

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support for SMPTE-2086 Mastering Display Metadata Box (SmDm)
- Support for Content Light Level Box (CoLL)
- Better test coverage for VisualSampleEntryBox
- IsVideoNaluType functions in both avc and hevc packages

### Fixed

- support short ESDS without SLConfig descriptor (issue #393)
- HEVC Slice Header CollocatedFromL0Flag should be true by default

## [0.47.0] - 2024-11-12

Expand Down
43 changes: 43 additions & 0 deletions avc/avc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,46 @@ func TestGetParameterSets(t *testing.T) {
}
}
}

func TestIsVideoNaluType(t *testing.T) {
testCases := []struct {
name string
naluType NaluType
want bool
}{
{
name: "video type - NALU_NON_IDR (1)",
naluType: NALU_NON_IDR,
want: true,
},
{
name: "video type - NALU_IDR (5)",
naluType: NALU_IDR,
want: true,
},
{
name: "non-video type - NALU_SEI (6)",
naluType: NALU_SEI,
want: false,
},
{
name: "non-video type - NALU_SPS (7)",
naluType: NALU_SPS,
want: false,
},
{
name: "non-video type - NALU_AUD (9)",
naluType: NALU_AUD,
want: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := IsVideoNaluType(tc.naluType)
if got != tc.want {
t.Errorf("IsVideoNaluType(%d) = %v; want %v", tc.naluType, got, tc.want)
}
})
}
}
6 changes: 3 additions & 3 deletions cmd/mp4ff-decrypt/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ mp4ff-decrypt [options] infile outfile
options:

-init string
Path to init file with encryption info (scheme, kid, pssh)
Path to init file with encryption info (scheme, kid, pssh)
-key string
Required: key (32 hex or 24 base64 chars)
Required: key (32 hex or 24 base64 chars)
-version
Get mp4ff version
Get mp4ff version
*/
package main
1 change: 1 addition & 0 deletions cmd/mp4ff-decrypt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func run(args []string) error {
var outFilePath = fs.Arg(1)

if opts.keyStr == "" {
fs.Usage()
return fmt.Errorf("no key specified")
}

Expand Down
16 changes: 9 additions & 7 deletions cmd/mp4ff-encrypt/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
mp4ff-encrypt encrypts a fragmented mp4 file using Common Encryption with cenc or cbcs scheme.
A combined fragmented file with init segment and media segment(s) will be encrypted.
For a pure media segment, an init segment with encryption information is needed.
For video, only AVC with avc1 and HEVC with hvc1 sample entries are currently supported.
For audio, all supported audio codecs should work.

Usage of mp4ff-encrypt:

Expand All @@ -10,18 +12,18 @@ mp4ff-encrypt [options] infile outfile
options:

-init string
Path to init file with encryption info (scheme, kid, pssh)
Path to init file with encryption info (scheme, kid, pssh)
-iv string
Required: iv (16 or 32 hex chars)
Required: iv (16 or 32 hex chars)
-key string
Required: key (32 hex or 24 base64 chars)
Required: key (32 hex or 24 base64 chars)
-kid string
key id (32 hex or 24 base64 chars). Required if initFilePath empty
key id (32 hex or 24 base64 chars). Required if initFilePath empty
-pssh string
file with one or more pssh box(es) in binary format. Will be added at end of moov box
file with one or more pssh box(es) in binary format. Will be added at end of moov box
-scheme string
cenc or cbcs. Required if initFilePath empty (default "cenc")
cenc or cbcs. Required if initFilePath empty (default "cenc")
-version
Get mp4ff version
Get mp4ff version
*/
package main
2 changes: 2 additions & 0 deletions cmd/mp4ff-encrypt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
var usg = `%s encrypts a fragmented mp4 file using Common Encryption with cenc or cbcs scheme.
A combined fragmented file with init segment and media segment(s) will be encrypted.
For a pure media segment, an init segment with encryption information is needed.
For video, only AVC with avc1 and HEVC with hvc1 sample entries are currently supported.
For audio, all supported audio codecs should work.

Usage of %s:
`
Expand Down
7 changes: 6 additions & 1 deletion hevc/hevc.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ func FindNaluTypesUpToFirstVideoNalu(sample []byte) []NaluType {
naluType := GetNaluType(sample[pos])
naluList = append(naluList, naluType)
pos += naluLength
if naluType <= highestVideoNaluType {
if IsVideoNaluType(naluType) {
break // Video has started
}
}
return naluList
}

// IsVideoNaluType returns true if NaluType is a video type (<= 31)
func IsVideoNaluType(naluType NaluType) bool {
return naluType <= highestVideoNaluType
}

// ContainsNaluType - is specific NaluType present in sample
func ContainsNaluType(sample []byte, specificNaluType NaluType) bool {
var pos uint32 = 0
Expand Down
48 changes: 48 additions & 0 deletions hevc/hevc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,51 @@ func TestNaluTypeStrings(t *testing.T) {
t.Errorf("got %d named instead of 22", named)
}
}

func TestIsVideoNaluType(t *testing.T) {
testCases := []struct {
name string
naluType NaluType
want bool
}{
{
name: "video type - NALU_TRAIL_N (0)",
naluType: NALU_TRAIL_N,
want: true,
},
{
name: "video type - NALU_TRAIL_R (1)",
naluType: NALU_TRAIL_R,
want: true,
},
{
name: "video type - NALU_IDR_W_RADL (19)",
naluType: NALU_IDR_W_RADL,
want: true,
},
{
name: "video type - highest (31)",
naluType: 31,
want: true,
},
{
name: "non-video type - VPS (32)",
naluType: NALU_VPS,
want: false,
},
{
name: "non-video type - SPS (33)",
naluType: NALU_SPS,
want: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := IsVideoNaluType(tc.naluType)
if got != tc.want {
t.Errorf("IsVideoNaluType(%d) = %v; want %v", tc.naluType, got, tc.want)
}
})
}
}
4 changes: 3 additions & 1 deletion hevc/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ type WeightingFactors struct {
}

func ParseSliceHeader(nalu []byte, spsMap map[uint32]*SPS, ppsMap map[uint32]*PPS) (*SliceHeader, error) {
sh := &SliceHeader{}
sh := &SliceHeader{
CollocatedFromL0Flag: true, // Default according to Section 7.4.7.1
}

buf := bytes.NewBuffer(nalu)
r := bits.NewEBSPReader(buf)
Expand Down
5 changes: 4 additions & 1 deletion hevc/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestParseSliceHeader(t *testing.T) {
LoopFilterAcrossSlicesEnabledFlag: true,
NumEntryPointOffsets: 1,
OffsetLenMinus1: 3,
CollocatedFromL0Flag: true,
EntryPointOffsetMinus1: []uint32{12},
Size: 6},
NALU_TRAIL_N: {
Expand All @@ -46,6 +47,7 @@ func TestParseSliceHeader(t *testing.T) {
LoopFilterAcrossSlicesEnabledFlag: true,
NumEntryPointOffsets: 1,
OffsetLenMinus1: 1,
CollocatedFromL0Flag: false,
EntryPointOffsetMinus1: []uint32{1},
Size: 10,
},
Expand Down Expand Up @@ -74,6 +76,7 @@ func TestParseSliceHeader(t *testing.T) {
},
},
},
CollocatedFromL0Flag: true,
FiveMinusMaxNumMergeCand: 2,
QpDelta: 7,
NumEntryPointOffsets: 1,
Expand Down Expand Up @@ -113,6 +116,6 @@ func TestParseSliceHeader(t *testing.T) {
}
}
if diff := deep.Equal(wantedHdr, gotHdr); diff != nil {
t.Errorf("Got Slice Header: %+v\n Diff is %v", gotHdr, diff)
t.Errorf("Got Slice Headers: %+v\n Diff is %v", gotHdr, diff)
}
}
Loading
Loading