Skip to content

Commit

Permalink
feat: encrypt HEVC (hvc1) video
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbee committed Feb 7, 2025
1 parent f4d772a commit 4576d1c
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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

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)
}
})
}
}
Loading

0 comments on commit 4576d1c

Please sign in to comment.