-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3
- Loading branch information
Showing
11 changed files
with
152 additions
and
129 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,89 @@ | ||
use super::{NAL_EOB_NUT, NAL_EOS_NUT, NAL_SEI_PREFIX, NAL_SEI_SUFFIX}; | ||
use anyhow::{bail, Result}; | ||
use bitvec_helpers::bitslice_reader::BitSliceReader; | ||
|
||
#[derive(Default, Debug, Clone)] | ||
pub struct SeiMessage { | ||
num_payload_type_ff_bytes: usize, | ||
last_payload_type_byte: u8, | ||
|
||
num_payload_size_ff_bytes: usize, | ||
last_payload_size_byte: u8, | ||
|
||
// Offset of the messame in the input slice | ||
pub msg_offset: usize, | ||
|
||
pub payload_type: u8, | ||
pub payload_offset: usize, | ||
pub payload_size: usize, | ||
} | ||
|
||
impl SeiMessage { | ||
/// Assumes the data does not contain any `emulation_prevention_three_byte`s | ||
pub fn parse_sei_rbsp(data: &[u8]) -> Result<Vec<SeiMessage>> { | ||
let mut reader = BitSliceReader::new(data); | ||
|
||
// forbidden_zero_bit | ||
reader.skip_n(1)?; | ||
|
||
let nal_type = reader.get_n::<u8>(6)?; | ||
|
||
if nal_type != NAL_SEI_PREFIX && nal_type != NAL_SEI_SUFFIX { | ||
bail!("NAL type {} is not SEI", nal_type); | ||
} | ||
|
||
if reader.available() < 9 && matches!(nal_type, NAL_EOS_NUT | NAL_EOB_NUT) { | ||
} else { | ||
reader.skip_n(6)?; // nuh_layer_id | ||
reader.skip_n(3)?; // temporal_id | ||
} | ||
|
||
let mut messages = Vec::new(); | ||
|
||
loop { | ||
messages.push(Self::parse_sei_message(&mut reader)?); | ||
|
||
if reader.available() <= 8 { | ||
break; | ||
} | ||
} | ||
|
||
Ok(messages) | ||
} | ||
|
||
fn parse_sei_message(reader: &mut BitSliceReader) -> Result<SeiMessage> { | ||
let mut msg = SeiMessage { | ||
msg_offset: reader.position() / 8, | ||
last_payload_type_byte: reader.get_n(8)?, | ||
..Default::default() | ||
}; | ||
|
||
while msg.last_payload_type_byte == 0xFF { | ||
msg.num_payload_type_ff_bytes += 1; | ||
msg.last_payload_type_byte = reader.get_n(8)?; | ||
|
||
msg.payload_type += 255; | ||
} | ||
|
||
msg.payload_type += msg.last_payload_type_byte; | ||
|
||
msg.last_payload_size_byte = reader.get_n(8)?; | ||
while msg.last_payload_size_byte == 0xFF { | ||
msg.num_payload_size_ff_bytes += 1; | ||
msg.last_payload_size_byte = reader.get_n(8)?; | ||
|
||
msg.payload_size += 255; | ||
} | ||
|
||
msg.payload_size += msg.last_payload_size_byte as usize; | ||
msg.payload_offset = reader.position() / 8; | ||
|
||
if msg.payload_size > reader.available() { | ||
bail!("Payload size is larger than NALU size"); | ||
} | ||
|
||
reader.skip_n(msg.payload_size * 8)?; | ||
|
||
Ok(msg) | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.