-
Notifications
You must be signed in to change notification settings - Fork 137
Parse H265 SEI for timestamp metadata #831
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
Open
chenosaurus
wants to merge
3
commits into
main
Choose a base branch
from
dc/h265_sei_timestamp2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package lksdk | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // parseH265SEIUserTimestamp parses H265 prefix SEI NAL units (type 39) carrying | ||
| // user_data_unregistered messages and returns a timestamp (microseconds) when detected. | ||
| // | ||
| // Expected payload format (after the 2-byte NAL header): | ||
| // | ||
| // payloadType = 5 (user_data_unregistered) | ||
| // payloadSize = 24 | ||
| // UUID = 16 bytes (3fa85f64-5717-4562-b3fc-2c963f66afa6) | ||
| // timestamp_us = 8 bytes, big-endian | ||
| // trailing = 0x80 (stop bits + padding) | ||
| func parseH265SEIUserTimestamp(nalData []byte) (int64, bool) { | ||
| if len(nalData) < 3 { | ||
| logger.Infow("H265 SEI user_data_unregistered: nal too short", "nal_len", len(nalData)) | ||
| return 0, false | ||
| } | ||
|
|
||
| // Skip 2-byte NAL header. | ||
| payload := nalData[2:] | ||
| i := 0 | ||
|
|
||
| // Parse payloadType (can be extended with 0xFF bytes). | ||
| payloadType := 0 | ||
| for i < len(payload) && payload[i] == 0xFF { | ||
| payloadType += 255 | ||
| i++ | ||
| } | ||
| if i >= len(payload) { | ||
| logger.Infow("H265 SEI user_data_unregistered: payloadType truncated", "payload_len", len(payload)) | ||
| return 0, false | ||
| } | ||
| payloadType += int(payload[i]) | ||
| i++ | ||
|
|
||
| // We only care about user_data_unregistered (type 5). | ||
| if payloadType != 5 { | ||
| return 0, false | ||
| } | ||
|
|
||
| // Parse payloadSize (can be extended with 0xFF bytes). | ||
| payloadSize := 0 | ||
| for i < len(payload) && payload[i] == 0xFF { | ||
| payloadSize += 255 | ||
| i++ | ||
| } | ||
| if i >= len(payload) { | ||
| logger.Infow("H265 SEI user_data_unregistered: payloadSize truncated", "payload_len", len(payload)) | ||
| return 0, false | ||
| } | ||
| payloadSize += int(payload[i]) | ||
| i++ | ||
|
|
||
| if payloadSize < 24 || len(payload) < i+payloadSize { | ||
| // Not enough data for UUID (16) + timestamp (8). | ||
| logger.Infow( | ||
| "H265 SEI user_data_unregistered: insufficient data for UUID + timestamp", | ||
| "payloadSize", payloadSize, | ||
| "payload_len", len(payload), | ||
| "offset", i, | ||
| ) | ||
| return 0, false | ||
| } | ||
|
|
||
| userData := payload[i : i+payloadSize] | ||
| uuidBytes := userData[:16] | ||
| tsBytes := userData[16:24] | ||
|
|
||
| // Validate the UUID matches the exact user timestamp UUID we expect. | ||
| if !bytes.Equal(uuidBytes, userTimestampSEIUUID[:]) { | ||
| return 0, false | ||
| } | ||
|
|
||
| timestampUS := binary.BigEndian.Uint64(tsBytes) | ||
|
|
||
| // Format UUID as 8-4-4-4-12 hex segments (for debug logs). | ||
| uuid := fmt.Sprintf("%x-%x-%x-%x-%x", | ||
| uuidBytes[0:4], | ||
| uuidBytes[4:6], | ||
| uuidBytes[6:8], | ||
| uuidBytes[8:10], | ||
| uuidBytes[10:16], | ||
| ) | ||
|
|
||
| logger.Debugw("H265 SEI user_data_unregistered parsed", "uuid", uuid, "timestamp_us", timestampUS) | ||
|
|
||
| return int64(timestampUS), true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package lksdk | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseH265SEIUserTimestamp_UUIDValidation(t *testing.T) { | ||
| const wantTS = int64(1234567890) | ||
|
|
||
| var tsBuf [8]byte | ||
| binary.BigEndian.PutUint64(tsBuf[:], uint64(wantTS)) | ||
|
|
||
| buildNAL := func(uuid [16]byte) []byte { | ||
| // 2-byte NAL header for prefix SEI (nal_unit_type = 39). | ||
| nal := []byte{0x4e, 0x01} | ||
|
|
||
| // payloadType = 5 (user_data_unregistered) | ||
| // payloadSize = 24 (16-byte UUID + 8-byte timestamp) | ||
| nal = append(nal, 0x05, 0x18) | ||
| nal = append(nal, uuid[:]...) | ||
| nal = append(nal, tsBuf[:]...) | ||
| return nal | ||
| } | ||
|
|
||
| t.Run("accepts matching UUID", func(t *testing.T) { | ||
| gotTS, ok := parseH265SEIUserTimestamp(buildNAL(userTimestampSEIUUID)) | ||
| if !ok { | ||
| t.Fatalf("expected ok=true") | ||
| } | ||
| if gotTS != wantTS { | ||
| t.Fatalf("timestamp mismatch: got %d want %d", gotTS, wantTS) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("rejects non-matching UUID", func(t *testing.T) { | ||
| badUUID := userTimestampSEIUUID | ||
| badUUID[0] ^= 0xff | ||
|
|
||
| _, ok := parseH265SEIUserTimestamp(buildNAL(badUUID)) | ||
| if ok { | ||
| t.Fatalf("expected ok=false") | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this continue needed or should it fall through and return empty data?