fix(video): preserve annex-b mediacodec output
Some checks failed
Mirror to GitHub / mirror (push) Failing after 31s
Build Release Binaries / build-amd64 (push) Failing after 3m35s

This commit is contained in:
Siavash Sameni
2026-05-25 20:20:22 +04:00
parent d57ebe3d2c
commit 06d28a9280
3 changed files with 101 additions and 3 deletions

View File

@@ -992,8 +992,13 @@ fn extract_vps_sps_pps(annex_b: &[u8]) -> HevcParameterSets {
/// (4-byte start codes `0x00 0x00 0x00 0x01`).
#[allow(dead_code)]
fn avcc_to_annexb(data: &[u8]) -> Vec<u8> {
if starts_with_annex_b_start_code(data) {
return data.to_vec();
}
let mut out = Vec::with_capacity(data.len() + data.len() / 4);
let mut offset = 0;
let mut saw_nal = false;
while offset + 4 <= data.len() {
let nal_len = u32::from_be_bytes([
data[offset],
@@ -1003,15 +1008,20 @@ fn avcc_to_annexb(data: &[u8]) -> Vec<u8> {
]) as usize;
offset += 4;
if offset + nal_len > data.len() {
break;
return if saw_nal { out } else { data.to_vec() };
}
out.extend_from_slice(&[0x00, 0x00, 0x00, 0x01]);
out.extend_from_slice(&data[offset..offset + nal_len]);
offset += nal_len;
saw_nal = true;
}
out
}
fn starts_with_annex_b_start_code(data: &[u8]) -> bool {
data.starts_with(&[0x00, 0x00, 0x01]) || data.starts_with(&[0x00, 0x00, 0x00, 0x01])
}
/// Parse an Annex-B access unit and return the first SPS and PPS found.
#[allow(dead_code)]
fn extract_sps_pps(annex_b: &[u8]) -> (Option<Vec<u8>>, Option<Vec<u8>>) {
@@ -1163,6 +1173,16 @@ mod tests {
assert_eq!(annex_b, expected);
}
#[test]
fn avcc_to_annexb_passes_through_annexb() {
let annex_b = vec![
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xC0, 0x1E,
0x00, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x21,
];
assert_eq!(avcc_to_annexb(&annex_b), annex_b);
}
#[test]
fn hevc_mediacodec_encoder_returns_not_initialized_on_non_android() {
let enc = MediaCodecHevcEncoder::new(1280, 720, 2_000_000);