| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use std::path::Path; |
| 14 | |
| 15 | use ec_core::BitWriter; |
| 16 | use ec_h264::{Decoder, Error, NalOutcome}; |
| 17 | |
| 18 | use super::common::{escape, find_stream}; |
| 19 | |
| 20 | |
| 21 | fn push_nal(out: &mut Vec<u8>, ref_idc: u8, kind: u8, rbsp: &[u8]) { |
| 22 | out.extend_from_slice(&[0, 0, 0, 1]); |
| 23 | out.push((ref_idc << 5) | kind); |
| 24 | out.extend_from_slice(&escape(rbsp)); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | fn sps( |
| 29 | profile: u8, |
| 30 | chroma_format_idc: u32, |
| 31 | bit_depth_minus8: u32, |
| 32 | separate: bool, |
| 33 | frame_mbs_only: bool, |
| 34 | ) -> Vec<u8> { |
| 35 | let mut w = BitWriter::new(); |
| 36 | w.write_bits(profile as u32, 8); |
| 37 | w.write_bits(0, 8); |
| 38 | w.write_bits(30, 8); |
| 39 | w.write_ue(0); |
| 40 | if matches!(profile, 100 | 110 | 122 | 244) { |
| 41 | w.write_ue(chroma_format_idc); |
| 42 | if chroma_format_idc == 3 { |
| 43 | w.write_bit(separate); |
| 44 | } |
| 45 | w.write_ue(bit_depth_minus8); |
| 46 | w.write_ue(bit_depth_minus8); |
| 47 | w.write_bit(false); |
| 48 | w.write_bit(false); |
| 49 | } |
| 50 | w.write_ue(0); |
| 51 | w.write_ue(2); |
| 52 | w.write_ue(1); |
| 53 | w.write_bit(false); |
| 54 | w.write_ue(10); |
| 55 | w.write_ue(if frame_mbs_only { 8 } else { 4 }); |
| 56 | w.write_bit(frame_mbs_only); |
| 57 | if !frame_mbs_only { |
| 58 | w.write_bit(true); |
| 59 | } |
| 60 | w.write_bit(true); |
| 61 | w.write_bit(false); |
| 62 | w.write_bit(false); |
| 63 | w.write_bit(true); |
| 64 | w.align_to_byte(); |
| 65 | w.as_bytes().to_vec() |
| 66 | } |
| 67 | |
| 68 | |
| 69 | fn pps(slice_groups: u32) -> Vec<u8> { |
| 70 | let mut w = BitWriter::new(); |
| 71 | w.write_ue(0); |
| 72 | w.write_ue(0); |
| 73 | w.write_bit(false); |
| 74 | w.write_bit(false); |
| 75 | w.write_ue(slice_groups - 1); |
| 76 | if slice_groups > 1 { |
| 77 | w.write_ue(1); |
| 78 | } |
| 79 | w.write_ue(0); |
| 80 | w.write_ue(0); |
| 81 | w.write_bit(false); |
| 82 | w.write_bits(0, 2); |
| 83 | w.write_se(0); |
| 84 | w.write_se(0); |
| 85 | w.write_se(0); |
| 86 | w.write_bit(false); |
| 87 | w.write_bit(false); |
| 88 | w.write_bit(false); |
| 89 | w.write_bit(true); |
| 90 | w.align_to_byte(); |
| 91 | w.as_bytes().to_vec() |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | fn pcm_slice(first_mb: u32, count: u32, frame_mbs_only: bool) -> Vec<u8> { |
| 97 | let mut w = BitWriter::new(); |
| 98 | w.write_ue(first_mb); |
| 99 | w.write_ue(7); |
| 100 | w.write_ue(0); |
| 101 | w.write_bits(0, 4); |
| 102 | if !frame_mbs_only { |
| 103 | w.write_bit(false); |
| 104 | } |
| 105 | w.write_ue(0); |
| 106 | w.write_bit(false); |
| 107 | w.write_bit(false); |
| 108 | w.write_se(0); |
| 109 | for _ in 0..count { |
| 110 | w.write_ue(25); |
| 111 | w.align_to_byte(); |
| 112 | w.write_bytes(&[0x80; 384]); |
| 113 | } |
| 114 | w.write_bit(true); |
| 115 | w.align_to_byte(); |
| 116 | w.as_bytes().to_vec() |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | fn header_only_slice(slice_type: u32) -> Vec<u8> { |
| 122 | let mut w = BitWriter::new(); |
| 123 | w.write_ue(0); |
| 124 | w.write_ue(slice_type); |
| 125 | w.write_ue(0); |
| 126 | w.write_bits(0, 4); |
| 127 | w.write_ue(0); |
| 128 | w.write_bit(false); |
| 129 | w.write_bit(false); |
| 130 | w.write_se(0); |
| 131 | w.write_se(0); |
| 132 | w.write_bit(true); |
| 133 | w.align_to_byte(); |
| 134 | w.as_bytes().to_vec() |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | fn first_error(stream: &[u8]) -> Option<Error> { |
| 140 | let mut dec = Decoder::new(); |
| 141 | for nal in ec_h264_syntax::AnnexBIter::new(stream) { |
| 142 | match dec.push_nal(nal) { |
| 143 | Ok(NalOutcome::PictureBoundary) => { |
| 144 | if let Err(e) = dec.end_picture() { |
| 145 | return Some(e); |
| 146 | } |
| 147 | if let Err(e) = dec.push_nal(nal) { |
| 148 | return Some(e); |
| 149 | } |
| 150 | } |
| 151 | Ok(_) => {} |
| 152 | Err(e) => return Some(e), |
| 153 | } |
| 154 | } |
| 155 | dec.flush().err() |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | fn assert_refused(stream: &[u8], feature: &str) { |
| 161 | match first_error(stream) { |
| 162 | Some(Error::Unsupported { what, why }) => { |
| 163 | assert!( |
| 164 | what.contains(feature), |
| 165 | "refused as `{what}` ({why}), expected a refusal naming `{feature}`" |
| 166 | ); |
| 167 | assert!(!why.is_empty(), "refusal of `{what}` gives no reason"); |
| 168 | } |
| 169 | Some(other) => panic!("expected Unsupported naming `{feature}`, got {other:?}"), |
| 170 | None => panic!("stream using {feature} decoded without a refusal"), |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | fn one_slice_stream( |
| 176 | profile: u8, |
| 177 | chroma: u32, |
| 178 | bd_minus8: u32, |
| 179 | separate: bool, |
| 180 | groups: u32, |
| 181 | slice: &[u8], |
| 182 | ) -> Vec<u8> { |
| 183 | let mut out = Vec::new(); |
| 184 | push_nal( |
| 185 | &mut out, |
| 186 | 3, |
| 187 | 7, |
| 188 | &sps(profile, chroma, bd_minus8, separate, true), |
| 189 | ); |
| 190 | push_nal(&mut out, 3, 8, &pps(groups)); |
| 191 | push_nal(&mut out, 3, 5, slice); |
| 192 | out |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn fmo_with_interlaced_coding_is_refused_by_name() { |
| 197 | let mut out = Vec::new(); |
| 198 | push_nal(&mut out, 3, 7, &sps(66, 1, 0, false, false)); |
| 199 | push_nal(&mut out, 3, 8, &pps(2)); |
| 200 | push_nal(&mut out, 3, 5, &pcm_slice(0, 1, false)); |
| 201 | assert_refused(&out, "FMO slice groups with interlaced coding"); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn arbitrary_slice_order_decodes() { |
| 206 | |
| 207 | |
| 208 | let mut out = Vec::new(); |
| 209 | push_nal(&mut out, 3, 7, &sps(66, 1, 0, false, true)); |
| 210 | push_nal(&mut out, 3, 8, &pps(1)); |
| 211 | push_nal(&mut out, 3, 5, &pcm_slice(50, 49, true)); |
| 212 | push_nal(&mut out, 3, 5, &pcm_slice(0, 50, true)); |
| 213 | assert!(first_error(&out).is_none(), "{:?}", first_error(&out)); |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn fmo_dispersed_map_decodes() { |
| 218 | |
| 219 | |
| 220 | let mut out = Vec::new(); |
| 221 | push_nal(&mut out, 3, 7, &sps(66, 1, 0, false, true)); |
| 222 | push_nal(&mut out, 3, 8, &pps(2)); |
| 223 | push_nal(&mut out, 3, 5, &pcm_slice(1, 49, true)); |
| 224 | push_nal(&mut out, 3, 5, &pcm_slice(0, 50, true)); |
| 225 | assert!(first_error(&out).is_none(), "{:?}", first_error(&out)); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn sp_slices_are_refused_by_name() { |
| 230 | let stream = one_slice_stream(88, 1, 0, false, 1, &header_only_slice(3)); |
| 231 | assert_refused(&stream, "SP and SI slices"); |
| 232 | } |
| 233 | |
| 234 | #[test] |
| 235 | fn si_slices_are_refused_by_name() { |
| 236 | let stream = one_slice_stream(88, 1, 0, false, 1, &header_only_slice(4)); |
| 237 | assert_refused(&stream, "SP and SI slices"); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn data_partitioning_is_refused_by_name() { |
| 242 | |
| 243 | |
| 244 | let mut out = Vec::new(); |
| 245 | push_nal(&mut out, 3, 7, &sps(88, 1, 0, false, true)); |
| 246 | push_nal(&mut out, 3, 8, &pps(1)); |
| 247 | push_nal(&mut out, 3, 2, &header_only_slice(7)); |
| 248 | assert_refused(&out, "slice data partitioning"); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn bit_depth_above_ten_is_refused_by_name() { |
| 253 | |
| 254 | |
| 255 | let stream = one_slice_stream(110, 1, 3, false, 1, &pcm_slice(0, 4, true)); |
| 256 | assert_refused(&stream, "bit depth above 10"); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn different_luma_and_chroma_bit_depth_is_refused_by_name() { |
| 261 | |
| 262 | |
| 263 | |
| 264 | let mut stream = Vec::new(); |
| 265 | let mut w = BitWriter::new(); |
| 266 | w.write_bits(244, 8); |
| 267 | w.write_bits(0, 8); |
| 268 | w.write_bits(30, 8); |
| 269 | w.write_ue(0); |
| 270 | w.write_ue(3); |
| 271 | w.write_bit(false); |
| 272 | w.write_ue(0); |
| 273 | w.write_ue(2); |
| 274 | w.write_bit(false); |
| 275 | w.write_bit(false); |
| 276 | w.write_ue(0); |
| 277 | w.write_ue(2); |
| 278 | w.write_ue(1); |
| 279 | w.write_bit(false); |
| 280 | w.write_ue(10); |
| 281 | w.write_ue(8); |
| 282 | w.write_bit(true); |
| 283 | w.write_bit(true); |
| 284 | w.write_bit(false); |
| 285 | w.write_bit(false); |
| 286 | w.write_bit(true); |
| 287 | w.align_to_byte(); |
| 288 | push_nal(&mut stream, 3, 7, w.as_bytes()); |
| 289 | push_nal(&mut stream, 3, 8, &pps(1)); |
| 290 | push_nal(&mut stream, 3, 5, &pcm_slice(0, 4, true)); |
| 291 | match first_error(&stream) { |
| 292 | Some(Error::Unsupported { .. }) | Some(Error::Corrupt { .. }) => {} |
| 293 | other => panic!("expected a refusal of mixed bit depths, got {other:?}"), |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | #[test] |
| 301 | fn jvt_refused_vectors_refuse_for_their_feature() { |
| 302 | let base = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vectors/h264-jvt"); |
| 303 | if !base.exists() { |
| 304 | assert!( |
| 305 | std::env::var_os("EC_REQUIRE_FIXTURES").is_none(), |
| 306 | "JVT vectors missing at {} (run scripts/fetch-vectors.sh)", |
| 307 | base.display() |
| 308 | ); |
| 309 | eprintln!("SKIP JVT vectors missing at {}", base.display()); |
| 310 | return; |
| 311 | } |
| 312 | let expected: &[(&str, &str)] = &[]; |
| 313 | let mut failures = Vec::new(); |
| 314 | for &(name, feature) in expected { |
| 315 | let Some(stream) = find_stream(&base.join(name)) else { |
| 316 | failures.push(format!("{name}: no bitstream on disk")); |
| 317 | continue; |
| 318 | }; |
| 319 | let bytes = std::fs::read(&stream).expect("read stream"); |
| 320 | match first_error(&bytes) { |
| 321 | Some(Error::Unsupported { what, .. }) if what.contains(feature) => {} |
| 322 | other => failures.push(format!( |
| 323 | "{name}: expected refusal naming `{feature}`, got {other:?}" |
| 324 | )), |
| 325 | } |
| 326 | } |
| 327 | assert!(failures.is_empty(), "{failures:#?}"); |
| 328 | } |