Skip to content

Commit 2c11865

Browse files
codec: accept hvcC SEI arrays without allocation
1 parent b92a7dd commit 2c11865

4 files changed

Lines changed: 109 additions & 43 deletions

File tree

‎crates/h264/src/decoder.rs‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -687,17 +687,3 @@ fn make_picture(o: &Output) -> Picture {
687687
draft: o.meta.draft,
688688
}
689689
}
690-
691-
#[cfg(test)]
692-
mod config_tests {
693-
use super::Decoder;
694-
695-
#[test]
696-
fn avcc_rejects_picture_slices_without_starting_a_picture() {
697-
let config = [1, 66, 0, 30, 0xff, 0xe1, 0, 2, 0x41, 0x80, 0];
698-
let mut decoder = Decoder::with_threads(1);
699-
assert!(decoder.configure_avcc(&config).is_err());
700-
assert!(decoder.pending.is_none());
701-
assert_eq!(decoder.stats().pictures, 0);
702-
}
703-
}

‎crates/h264/src/synth_tests.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ fn avcc_accepts_generated_sps_and_pps() {
9696
assert!(Decoder::from_avcc(&avcc).is_ok());
9797
}
9898

99+
#[test]
100+
fn avcc_rejects_a_valid_picture_slice_without_starting_a_picture() {
101+
let sequence = sps(66, 4, false, PocType::T0);
102+
let picture = pps(false, false);
103+
let slice = slice_nal(
104+
&Pic {
105+
idr: true,
106+
frame_num: 0,
107+
ref_idc: 3,
108+
long_term_ref: false,
109+
mods: vec![],
110+
mmcos: vec![],
111+
mbs: vec![Some(vec![128; 16 * 16]); NMB],
112+
b: false,
113+
poc_lsb: Some(0),
114+
poc_delta: None,
115+
wt: None,
116+
},
117+
false,
118+
);
119+
let mut avcc = vec![1, 66, 0, 30, 0xff, 0xe2];
120+
for nal in [&sequence[4..], &slice[4..]] {
121+
avcc.extend_from_slice(&(nal.len() as u16).to_be_bytes());
122+
avcc.extend_from_slice(nal);
123+
}
124+
avcc.push(1);
125+
avcc.extend_from_slice(&((picture.len() - 4) as u16).to_be_bytes());
126+
avcc.extend_from_slice(&picture[4..]);
127+
128+
let mut decoder = Decoder::with_threads(1);
129+
assert!(decoder.configure_avcc(&avcc).is_err());
130+
assert_eq!(decoder.stats().pictures, 0);
131+
}
132+
99133
#[derive(Default, Clone)]
100134
struct Pic {
101135
idr: bool,

‎crates/hevc/src/decoder.rs‎

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -161,30 +161,33 @@ impl Decoder {
161161
ensure!(hvcc.len() >= 23, "hvcC too short");
162162
ensure!(hvcc[0] == 1, "unsupported hvcC version {}", hvcc[0]);
163163
let num_arrays = hvcc[22] as usize;
164-
let mut pos = 23;
165-
let mut sets = Vec::new();
166-
for _ in 0..num_arrays {
167-
ensure!(pos + 3 <= hvcc.len(), "hvcC truncated");
168-
let array_type = hvcc[pos] & 0x3f;
169-
let n = u16::from_be_bytes([hvcc[pos + 1], hvcc[pos + 2]]) as usize;
170-
pos += 3;
171-
for _ in 0..n {
172-
ensure!(pos + 2 <= hvcc.len(), "hvcC truncated");
173-
let len = u16::from_be_bytes([hvcc[pos], hvcc[pos + 1]]) as usize;
174-
pos += 2;
175-
ensure!(pos + len <= hvcc.len(), "hvcC truncated");
176-
sets.push((array_type, &hvcc[pos..pos + len]));
177-
pos += len;
164+
for pass in 0..2 {
165+
let mut pos = 23;
166+
for _ in 0..num_arrays {
167+
ensure!(pos <= hvcc.len() && hvcc.len() - pos >= 3, "hvcC truncated");
168+
let array_type = hvcc[pos] & 0x3f;
169+
let n = u16::from_be_bytes([hvcc[pos + 1], hvcc[pos + 2]]) as usize;
170+
pos += 3;
171+
for _ in 0..n {
172+
ensure!(pos <= hvcc.len() && hvcc.len() - pos >= 2, "hvcC truncated");
173+
let len = u16::from_be_bytes([hvcc[pos], hvcc[pos + 1]]) as usize;
174+
pos += 2;
175+
ensure!(len <= hvcc.len() - pos, "hvcC truncated");
176+
let nal = &hvcc[pos..pos + len];
177+
if pass == 0 {
178+
let header = NalHeader::parse(nal)?;
179+
ensure!(
180+
[nal_type::VPS, nal_type::SPS, nal_type::PPS, nal_type::PREFIX_SEI, nal_type::SUFFIX_SEI].contains(&array_type),
181+
"hvcC array has unsupported NAL type {array_type}"
182+
);
183+
ensure!(header.nal_type == array_type, "hvcC array type {array_type} disagrees with NAL header type {}", header.nal_type);
184+
} else {
185+
self.handle_nal(nal, 0)?;
186+
}
187+
pos += len;
188+
}
178189
}
179190
}
180-
for (array_type, nal) in &sets {
181-
let header = NalHeader::parse(nal)?;
182-
ensure!([nal_type::VPS, nal_type::SPS, nal_type::PPS].contains(array_type), "hvcC array has non-parameter-set NAL type {array_type}");
183-
ensure!(header.nal_type == *array_type, "hvcC array type {array_type} disagrees with NAL header type {}", header.nal_type);
184-
}
185-
for (_, nal) in sets {
186-
self.handle_nal(nal, 0)?;
187-
}
188191
self.nal_length_size = Some((hvcc[21] & 3) as usize + 1);
189192
Ok(())
190193
}
@@ -669,14 +672,14 @@ mod config_tests {
669672
use super::Decoder;
670673

671674
#[test]
672-
fn hvcc_rejects_picture_slices_without_starting_a_picture() {
675+
fn hvcc_skips_prefix_and_suffix_sei_arrays() {
673676
let mut config = vec![0; 23];
674677
config[0] = 1;
675-
config[22] = 1;
676-
config.extend_from_slice(&[32, 0, 1, 0, 2, 0x02, 0x01]);
677-
let mut decoder = Decoder::with_threads(1);
678-
assert!(decoder.configure_hvcc(&config).is_err());
679-
assert!(decoder.pending.is_none());
680-
assert_eq!(decoder.stats().pictures, 0);
678+
config[22] = 2;
679+
config.extend_from_slice(&[39, 0, 1, 0, 2, 0x4e, 0x01]);
680+
config.extend_from_slice(&[40, 0, 1, 0, 2, 0x50, 0x01]);
681+
682+
let decoder = Decoder::from_hvcc(&config);
683+
assert!(decoder.is_ok());
681684
}
682685
}

‎crates/hevc/src/synth_tests.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,46 @@ fn hvcc_accepts_generated_vps_sps_and_pps() {
659659
}
660660
assert!(Decoder::from_hvcc(&hvcc).is_ok());
661661
}
662+
663+
#[test]
664+
fn hvcc_rejects_a_valid_picture_slice_without_starting_a_picture() {
665+
let sets = [vps(), sps(true), pps(0, &None, false)];
666+
let mut stream = Vec::new();
667+
let mut saved = None;
668+
encode_picture(
669+
&PicDesc {
670+
nal_type: 19,
671+
poc: 0,
672+
intra: true,
673+
cu: Cu::Pcm,
674+
pps_id: 0,
675+
st: vec![],
676+
lt: vec![],
677+
num_ref: 0,
678+
list_entry: None,
679+
segments: whole(WC * HC),
680+
},
681+
&None,
682+
&content(0),
683+
&mut stream,
684+
&mut saved,
685+
);
686+
687+
let mut hvcc = vec![0; 23];
688+
hvcc[0] = 1;
689+
hvcc[22] = 4;
690+
for (kind, set) in [32, 33, 34].into_iter().zip(sets) {
691+
let nal = &set[4..];
692+
hvcc.extend_from_slice(&[kind, 0, 1]);
693+
hvcc.extend_from_slice(&(nal.len() as u16).to_be_bytes());
694+
hvcc.extend_from_slice(nal);
695+
}
696+
let slice = &stream[4..];
697+
hvcc.extend_from_slice(&[19, 0, 1]);
698+
hvcc.extend_from_slice(&(slice.len() as u16).to_be_bytes());
699+
hvcc.extend_from_slice(slice);
700+
701+
let mut decoder = Decoder::with_threads(1);
702+
assert!(decoder.configure_hvcc(&hvcc).is_err());
703+
assert_eq!(decoder.stats().pictures, 0);
704+
}

0 commit comments

Comments
 (0)