Registry Decoder entry, avcC extradata, mechanism-named refusals
Browse files at this commit19f3903e74263efac29a1bc6dc27a458ab44a747Parent: f985e44- feat
- registry Decoder entry, avcC extradata, mechanism-named refusals
6 changed files +331 -17
- Cargo.toml +1 -0
- crates/ec-h264/Cargo.toml +2 -2
- crates/ec-h264/src/cavlc.rs +7 -1
- crates/ec-h264/src/codec.rs +244 -0
- crates/ec-h264/src/decoder.rs +55 -14
- crates/ec-h264/src/lib.rs +22 -0
Cargo.tomlmodified+1 -0
| @@ -12,6 +12,7 @@ authors = ["Ahmet Kaan Gümüş"] | |||
| 12 | ec-av1-syntax = { path = "crates/ec-av1-syntax", version = "0.1.0" } | 12 | ec-av1-syntax = { path = "crates/ec-av1-syntax", version = "0.1.0" } |
| 13 | ec-core = { path = "crates/ec-core", version = "0.1.0" } | 13 | ec-core = { path = "crates/ec-core", version = "0.1.0" } |
| 14 | ec-dsp = { path = "crates/ec-dsp", version = "0.1.0" } | 14 | ec-dsp = { path = "crates/ec-dsp", version = "0.1.0" } |
| 15 | +ec-h264-syntax = { path = "crates/ec-h264-syntax", version = "0.1.0" } | ||
| 15 | ec-inflate = { path = "crates/ec-inflate", version = "0.1.0" } | 16 | ec-inflate = { path = "crates/ec-inflate", version = "0.1.0" } |
| 16 | ec-matroska = { path = "crates/ec-matroska", version = "0.1.0" } | 17 | ec-matroska = { path = "crates/ec-matroska", version = "0.1.0" } |
| 17 | ec-mp4 = { path = "crates/ec-mp4", version = "0.1.0" } | 18 | ec-mp4 = { path = "crates/ec-mp4", version = "0.1.0" } |
crates/ec-h264/Cargo.tomlmodified+2 -2
| @@ -8,9 +8,9 @@ authors.workspace = true | |||
| 8 | 8 | ||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | ec-core = { workspace = true } | 10 | ec-core = { workspace = true } |
| 11 | -ec-h264-syntax = { path = "../ec-h264-syntax", version = "0.1.0" } | 11 | +ec-h264-syntax = { workspace = true } |
| 12 | wide = { workspace = true } | 12 | wide = { workspace = true } |
| 13 | 13 | ||
| 14 | [dev-dependencies] | 14 | [dev-dependencies] |
| 15 | # The conformance harness walks Annex B itself to drive push_nal. | 15 | # The conformance harness walks Annex B itself to drive push_nal. |
| 16 | -ec-h264-syntax = { path = "../ec-h264-syntax", version = "0.1.0" } | 16 | +ec-h264-syntax = { workspace = true } |
crates/ec-h264/src/cavlc.rsmodified+7 -1
| @@ -43,7 +43,13 @@ fn read_coeff_token(r: &mut BitCursor<'_>, nc: i32) -> Result<(u8, u8)> { | |||
| 43 | 0..=1 => COEFF_TOKEN_NC0, | 43 | 0..=1 => COEFF_TOKEN_NC0, |
| 44 | 2..=3 => COEFF_TOKEN_NC2, | 44 | 2..=3 => COEFF_TOKEN_NC2, |
| 45 | 4..=7 => COEFF_TOKEN_NC4, | 45 | 4..=7 => COEFF_TOKEN_NC4, |
| 46 | - _ => return Err(Error::unsupported("CAVLC nC = -2", "4:2:2 chroma DC")), | 46 | + _ => { |
| 47 | + return Err(Error::unsupported( | ||
| 48 | + "CAVLC nC = -2", | ||
| 49 | + "nC -2 is the 4:2:2 chroma DC coeff_token table (9.2.1); only \ | ||
| 50 | + 4:2:0 chroma DC (nC -1) is decoded", | ||
| 51 | + )); | ||
| 52 | + } | ||
| 47 | }; | 53 | }; |
| 48 | let peek = r.peek16(); | 54 | let peek = r.peek16(); |
| 49 | for &(len, code, tc, t1) in table { | 55 | for &(len, code, tc, t1) in table { |
crates/ec-h264/src/codec.rsadded+244 -0
| @@ -0,0 +1,244 @@ | |||
| 1 | +//! The [`ec_core::registry::Decoder`] entry path: packets in, frames out. | ||
| 2 | +//! | ||
| 3 | +//! [`crate::Decoder`] is the NAL-level surface a bitstream tool wants. This is | ||
| 4 | +//! the surface a *player* wants, and it adds the two things a container brings | ||
| 5 | +//! with it: parameter sets carried out of band in an `avcC` record | ||
| 6 | +//! (ISO/IEC 14496-15 clause 5.3.3.1) and NAL units framed by a length prefix | ||
| 7 | +//! rather than by Annex B start codes. Which framing a packet uses is decided | ||
| 8 | +//! once, by whether `extradata` held an `avcC`. | ||
| 9 | + | ||
| 10 | +use std::collections::VecDeque; | ||
| 11 | + | ||
| 12 | +use ec_core::BitReader; | ||
| 13 | +use ec_core::error::{Error, Result}; | ||
| 14 | +use ec_core::frame::{Frame, PixelFormat}; | ||
| 15 | +use ec_core::packet::Packet; | ||
| 16 | +use ec_core::registry::{CodecId, CodecParameters, MediaParameters}; | ||
| 17 | +use ec_core::timebase::Timestamp; | ||
| 18 | +use ec_h264_syntax::AnnexBIter; | ||
| 19 | + | ||
| 20 | +use crate::decoder::{Decoder, NalOutcome}; | ||
| 21 | + | ||
| 22 | +/// H.264 decoder behind the codec registry's packet/frame contract. | ||
| 23 | +pub struct H264Decoder { | ||
| 24 | + params: CodecParameters, | ||
| 25 | + inner: Decoder, | ||
| 26 | + /// Bytes of the NAL length prefix when the stream is length framed; | ||
| 27 | + /// `None` means Annex B start codes. | ||
| 28 | + nal_length_size: Option<usize>, | ||
| 29 | + frames: VecDeque<Frame>, | ||
| 30 | + pending_pts: Option<Timestamp>, | ||
| 31 | + end_of_stream: bool, | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +impl H264Decoder { | ||
| 35 | + /// A decoder for `params`. | ||
| 36 | + /// | ||
| 37 | + /// When `params.extradata` holds an `avcC` record its parameter sets are | ||
| 38 | + /// read immediately — so [`H264Decoder::codec_parameters`] reports the | ||
| 39 | + /// picture size before the first packet — and packets are then expected to | ||
| 40 | + /// carry length-prefixed NAL units; otherwise packets are read as Annex B. | ||
| 41 | + pub fn new(params: CodecParameters) -> Result<H264Decoder> { | ||
| 42 | + if params.codec != CodecId::H264 { | ||
| 43 | + return Err(Error::unsupported( | ||
| 44 | + format!("codec {:?}", params.codec), | ||
| 45 | + "this decoder implements H.264 only", | ||
| 46 | + )); | ||
| 47 | + } | ||
| 48 | + let mut decoder = H264Decoder { | ||
| 49 | + params, | ||
| 50 | + inner: Decoder::new(), | ||
| 51 | + nal_length_size: None, | ||
| 52 | + frames: VecDeque::new(), | ||
| 53 | + pending_pts: None, | ||
| 54 | + end_of_stream: false, | ||
| 55 | + }; | ||
| 56 | + // 7 bytes is the shortest record that can hold the fixed header plus a | ||
| 57 | + // parameter-set count; version 1 is the only one 14496-15 defines. | ||
| 58 | + if let Some(extradata) = decoder.params.extradata.clone() | ||
| 59 | + && extradata.len() >= 7 | ||
| 60 | + && extradata[0] == 1 | ||
| 61 | + { | ||
| 62 | + decoder.parse_avcc(&extradata)?; | ||
| 63 | + } | ||
| 64 | + Ok(decoder) | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /// Read an `avcC` record: the NAL length size and the in-band parameter | ||
| 68 | + /// sets (ISO/IEC 14496-15 clause 5.3.3.1). | ||
| 69 | + fn parse_avcc(&mut self, extradata: &[u8]) -> Result<()> { | ||
| 70 | + let mut r = BitReader::new(extradata); | ||
| 71 | + // configurationVersion, AVCProfileIndication, profile_compatibility, | ||
| 72 | + // AVCLevelIndication: all restated by the SPS below. | ||
| 73 | + r.skip_bits(32)?; | ||
| 74 | + let length_size_minus_one = r.read_bits(8)? & 0x03; | ||
| 75 | + self.nal_length_size = Some(length_size_minus_one as usize + 1); | ||
| 76 | + let num_sps = r.read_bits(8)? & 0x1F; | ||
| 77 | + for _ in 0..num_sps { | ||
| 78 | + let len = r.read_bits(16)? as usize; | ||
| 79 | + let unit = r.read_bytes(len)?.to_vec(); | ||
| 80 | + self.push(&unit)?; | ||
| 81 | + } | ||
| 82 | + let num_pps = r.read_bits(8)?; | ||
| 83 | + for _ in 0..num_pps { | ||
| 84 | + let len = r.read_bits(16)? as usize; | ||
| 85 | + let unit = r.read_bytes(len)?.to_vec(); | ||
| 86 | + self.push(&unit)?; | ||
| 87 | + } | ||
| 88 | + Ok(()) | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /// Split a packet into NAL units by whichever framing this stream uses. | ||
| 92 | + fn decode_packet_payload(&mut self, data: &[u8]) -> Result<()> { | ||
| 93 | + let Some(size) = self.nal_length_size else { | ||
| 94 | + for unit in AnnexBIter::new(data) { | ||
| 95 | + let unit = unit.to_vec(); | ||
| 96 | + self.push(&unit)?; | ||
| 97 | + } | ||
| 98 | + return Ok(()); | ||
| 99 | + }; | ||
| 100 | + let mut offset = 0usize; | ||
| 101 | + while offset + size <= data.len() { | ||
| 102 | + let mut length = 0usize; | ||
| 103 | + for i in 0..size { | ||
| 104 | + length = (length << 8) | data[offset + i] as usize; | ||
| 105 | + } | ||
| 106 | + offset += size; | ||
| 107 | + let end = offset | ||
| 108 | + .checked_add(length) | ||
| 109 | + .filter(|&end| end <= data.len()) | ||
| 110 | + .ok_or_else(|| { | ||
| 111 | + Error::corrupt(format!( | ||
| 112 | + "H.264: a NAL unit of {length} bytes overruns the packet" | ||
| 113 | + )) | ||
| 114 | + })?; | ||
| 115 | + let unit = data[offset..end].to_vec(); | ||
| 116 | + self.push(&unit)?; | ||
| 117 | + offset = end; | ||
| 118 | + } | ||
| 119 | + Ok(()) | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /// Feed one NAL unit, completing the open picture when it starts a new one. | ||
| 123 | + fn push(&mut self, unit: &[u8]) -> Result<()> { | ||
| 124 | + match self.inner.push_nal(unit)? { | ||
| 125 | + NalOutcome::PictureBoundary => { | ||
| 126 | + // The NAL was not consumed: it opens the next picture. | ||
| 127 | + self.finish_picture()?; | ||
| 128 | + self.inner.push_nal(unit)?; | ||
| 129 | + } | ||
| 130 | + NalOutcome::ParameterSet => self.sync_parameters(), | ||
| 131 | + _ => {} | ||
| 132 | + } | ||
| 133 | + Ok(()) | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /// Emit the open picture, if there is one, as a frame. | ||
| 137 | + fn finish_picture(&mut self) -> Result<()> { | ||
| 138 | + if !self.inner.picture_open() { | ||
| 139 | + return Ok(()); | ||
| 140 | + } | ||
| 141 | + self.inner.end_picture()?; | ||
| 142 | + let mut frame = self.inner.frame()?; | ||
| 143 | + frame.pts = self.pending_pts.take(); | ||
| 144 | + self.frames.push_back(Frame::Video(frame)); | ||
| 145 | + Ok(()) | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + /// Publish the active SPS geometry on `params`, the way a demuxer that | ||
| 149 | + /// only had an `avcC` expects to read it back. | ||
| 150 | + fn sync_parameters(&mut self) { | ||
| 151 | + let Some((width, height)) = self.inner.picture_size() else { | ||
| 152 | + return; | ||
| 153 | + }; | ||
| 154 | + if let MediaParameters::Video(video) = &mut self.params.media { | ||
| 155 | + video.width = width; | ||
| 156 | + video.height = height; | ||
| 157 | + video.format = Some(PixelFormat::I420); | ||
| 158 | + } | ||
| 159 | + } | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +impl ec_core::registry::Decoder for H264Decoder { | ||
| 163 | + fn codec_parameters(&self) -> &CodecParameters { | ||
| 164 | + &self.params | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + fn send_packet(&mut self, packet: &Packet) -> Result<()> { | ||
| 168 | + self.end_of_stream = false; | ||
| 169 | + self.pending_pts = packet | ||
| 170 | + .pts | ||
| 171 | + .map(|ticks| Timestamp::new(ticks, packet.time_base)); | ||
| 172 | + self.decode_packet_payload(&packet.data)?; | ||
| 173 | + // One packet is one access unit, so whatever picture it started is | ||
| 174 | + // complete once the packet is consumed. | ||
| 175 | + self.finish_picture() | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + fn receive_frame(&mut self) -> Result<Frame> { | ||
| 179 | + match self.frames.pop_front() { | ||
| 180 | + Some(frame) => Ok(frame), | ||
| 181 | + None if self.end_of_stream => Err(Error::Eof), | ||
| 182 | + None => Err(Error::NeedMore), | ||
| 183 | + } | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + fn flush(&mut self) -> Result<()> { | ||
| 187 | + self.finish_picture()?; | ||
| 188 | + self.end_of_stream = true; | ||
| 189 | + Ok(()) | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + fn reset(&mut self) { | ||
| 193 | + // Parameter sets and the framing decision survive a seek; picture | ||
| 194 | + // state does not. | ||
| 195 | + self.inner.reset_pictures(); | ||
| 196 | + self.frames.clear(); | ||
| 197 | + self.pending_pts = None; | ||
| 198 | + self.end_of_stream = false; | ||
| 199 | + } | ||
| 200 | +} | ||
| 201 | + | ||
| 202 | +#[cfg(test)] | ||
| 203 | +mod tests { | ||
| 204 | + use super::*; | ||
| 205 | + use ec_core::TimeBase; | ||
| 206 | + use ec_core::registry::Decoder as _; | ||
| 207 | + | ||
| 208 | + #[test] | ||
| 209 | + fn only_h264_parameters_are_accepted() { | ||
| 210 | + assert!(H264Decoder::new(CodecParameters::new(CodecId::H265)).is_err()); | ||
| 211 | + let decoder = H264Decoder::new(CodecParameters::new(CodecId::H264)).unwrap(); | ||
| 212 | + assert_eq!(decoder.codec_parameters().codec, CodecId::H264); | ||
| 213 | + assert!(decoder.nal_length_size.is_none(), "Annex B by default"); | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + #[test] | ||
| 217 | + fn a_packet_without_parameter_sets_is_corrupt_not_a_panic() { | ||
| 218 | + let mut decoder = H264Decoder::new(CodecParameters::new(CodecId::H264)).unwrap(); | ||
| 219 | + // A slice NAL with no SPS or PPS in front of it. | ||
| 220 | + let stream = [0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x00]; | ||
| 221 | + let packet = Packet::new(0, TimeBase::new(1, 25), stream.to_vec()); | ||
| 222 | + let err = decoder.send_packet(&packet).unwrap_err(); | ||
| 223 | + assert!(matches!(err, Error::Corrupt { .. }), "{err}"); | ||
| 224 | + assert!(decoder.receive_frame().unwrap_err().is_need_more()); | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + #[test] | ||
| 228 | + fn flush_then_drain_reports_end_of_stream() { | ||
| 229 | + let mut decoder = H264Decoder::new(CodecParameters::new(CodecId::H264)).unwrap(); | ||
| 230 | + decoder.flush().unwrap(); | ||
| 231 | + assert!(decoder.receive_frame().unwrap_err().is_eof()); | ||
| 232 | + decoder.reset(); | ||
| 233 | + assert!(decoder.receive_frame().unwrap_err().is_need_more()); | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + /// A truncated `avcC` is a container bug, not a panic: the reader runs out | ||
| 237 | + /// of bytes and says so. | ||
| 238 | + #[test] | ||
| 239 | + fn a_truncated_avcc_is_an_error() { | ||
| 240 | + let mut params = CodecParameters::new(CodecId::H264); | ||
| 241 | + params.extradata = Some(ec_core::Buf::from_vec(vec![1, 66, 0, 30, 0xFF, 0xE1, 0x00])); | ||
| 242 | + assert!(H264Decoder::new(params).is_err()); | ||
| 243 | + } | ||
| 244 | +} | ||
crates/ec-h264/src/decoder.rsmodified+55 -14
| @@ -175,6 +175,9 @@ pub struct Decoder { | |||
| 175 | /// Scaling factors for intra Y/Cb/Cr 4x4 lists of the active PPS/SPS. | 175 | /// Scaling factors for intra Y/Cb/Cr 4x4 lists of the active PPS/SPS. |
| 176 | ls: [LevelScale4x4; 3], | 176 | ls: [LevelScale4x4; 3], |
| 177 | active_pps: Option<u32>, | 177 | active_pps: Option<u32>, |
| 178 | + /// `seq_parameter_set_id` of the most recently stored SPS, so a container | ||
| 179 | + /// entry path can publish the picture size before the first slice. | ||
| 180 | + last_sps: Option<u8>, | ||
| 178 | } | 181 | } |
| 179 | 182 | ||
| 180 | /// What [`Decoder::push_nal`] did with a NAL unit. | 183 | /// What [`Decoder::push_nal`] did with a NAL unit. |
| @@ -212,9 +215,18 @@ impl Decoder { | |||
| 212 | LevelScale4x4::new(&[16; 16]), | 215 | LevelScale4x4::new(&[16; 16]), |
| 213 | ], | 216 | ], |
| 214 | active_pps: None, | 217 | active_pps: None, |
| 218 | + last_sps: None, | ||
| 215 | } | 219 | } |
| 216 | } | 220 | } |
| 217 | 221 | ||
| 222 | + /// Cropped picture size of the most recently stored SPS, `None` until one | ||
| 223 | + /// arrives. Lets a container entry path fill in stream parameters from an | ||
| 224 | + /// `avcC` record without decoding a picture first. | ||
| 225 | + pub fn picture_size(&self) -> Option<(u32, u32)> { | ||
| 226 | + let sps = self.sps_map[self.last_sps? as usize].as_ref()?; | ||
| 227 | + Some((sps.width, sps.height)) | ||
| 228 | + } | ||
| 229 | + | ||
| 218 | /// Decode the first IDR picture of an Annex B stream and return it. | 230 | /// Decode the first IDR picture of an Annex B stream and return it. |
| 219 | pub fn decode_first_idr(&mut self, annexb: &[u8]) -> Result<VideoFrame> { | 231 | pub fn decode_first_idr(&mut self, annexb: &[u8]) -> Result<VideoFrame> { |
| 220 | let mut got_slice = false; | 232 | let mut got_slice = false; |
| @@ -245,8 +257,9 @@ impl Decoder { | |||
| 245 | let sps = Sps::parse(&rbsp); | 257 | let sps = Sps::parse(&rbsp); |
| 246 | self.rbsp = rbsp; | 258 | self.rbsp = rbsp; |
| 247 | let sps = sps?; | 259 | let sps = sps?; |
| 248 | - let id = sps.id as usize; | 260 | + let id = sps.id; |
| 249 | - self.sps_map[id] = Some(sps); | 261 | + self.sps_map[id as usize] = Some(sps); |
| 262 | + self.last_sps = Some(id); | ||
| 250 | Ok(NalOutcome::ParameterSet) | 263 | Ok(NalOutcome::ParameterSet) |
| 251 | } | 264 | } |
| 252 | NalUnitType::Pps => { | 265 | NalUnitType::Pps => { |
| @@ -266,9 +279,13 @@ impl Decoder { | |||
| 266 | self.rbsp = rbsp; | 279 | self.rbsp = rbsp; |
| 267 | r | 280 | r |
| 268 | } | 281 | } |
| 269 | - NalUnitType::SliceDataA | NalUnitType::SliceDataB | NalUnitType::SliceDataC => Err( | 282 | + NalUnitType::SliceDataA | NalUnitType::SliceDataB | NalUnitType::SliceDataC => { |
| 270 | - Error::unsupported("slice data partitioning", "NAL types 2..4 not implemented"), | 283 | + Err(Error::unsupported( |
| 271 | - ), | 284 | + "slice data partitioning", |
| 285 | + "nal_unit_type 2 to 4 carry a slice in three NAL units that \ | ||
| 286 | + clause 7.4.1 reassembles by slice_id; not implemented", | ||
| 287 | + )) | ||
| 288 | + } | ||
| 272 | _ => Ok(NalOutcome::Skipped), | 289 | _ => Ok(NalOutcome::Skipped), |
| 273 | } | 290 | } |
| 274 | } | 291 | } |
| @@ -323,6 +340,13 @@ impl Decoder { | |||
| 323 | self.has_picture && !self.pic.complete | 340 | self.has_picture && !self.pic.complete |
| 324 | } | 341 | } |
| 325 | 342 | ||
| 343 | + /// Drop picture state after a seek. Parameter sets survive, because the | ||
| 344 | + /// container does not resend them at every seek point. | ||
| 345 | + pub fn reset_pictures(&mut self) { | ||
| 346 | + self.has_picture = false; | ||
| 347 | + self.pic.complete = false; | ||
| 348 | + } | ||
| 349 | + | ||
| 326 | fn decode_slice_rbsp(&mut self, header: NalHeader, rbsp: &[u8]) -> Result<NalOutcome> { | 350 | fn decode_slice_rbsp(&mut self, header: NalHeader, rbsp: &[u8]) -> Result<NalOutcome> { |
| 327 | // Peek first_mb / slice_type / pps_id to select parameter sets. | 351 | // Peek first_mb / slice_type / pps_id to select parameter sets. |
| 328 | let mut peek = BitReader::new(rbsp); | 352 | let mut peek = BitReader::new(rbsp); |
| @@ -347,7 +371,8 @@ impl Decoder { | |||
| 347 | if sh.slice_type != SliceType::I { | 371 | if sh.slice_type != SliceType::I { |
| 348 | return Err(Error::unsupported( | 372 | return Err(Error::unsupported( |
| 349 | "non-I slice", | 373 | "non-I slice", |
| 350 | - "P/B/SP/SI slices arrive with inter prediction (S18c)", | 374 | + "P/B/SP/SI slices need inter prediction and a decoded picture \ |
| 375 | + buffer (clause 8.4); only intra slices are decoded", | ||
| 351 | )); | 376 | )); |
| 352 | } | 377 | } |
| 353 | 378 | ||
| @@ -409,30 +434,42 @@ impl Decoder { | |||
| 409 | } | 434 | } |
| 410 | 435 | ||
| 411 | /// Reject unsupported streams with named reasons before touching pixels. | 436 | /// Reject unsupported streams with named reasons before touching pixels. |
| 437 | +/// | ||
| 438 | +/// Every `why` here names the syntax element that fired and the machinery it | ||
| 439 | +/// would need: a refusal is a capability statement about this binary, and the | ||
| 440 | +/// conformance suite proves each one against a stream that really uses the | ||
| 441 | +/// feature. | ||
| 412 | fn check_supported(sps: &Sps, pps: &Pps, slice_type_code: u32) -> Result<()> { | 442 | fn check_supported(sps: &Sps, pps: &Pps, slice_type_code: u32) -> Result<()> { |
| 413 | if pps.entropy_coding_mode { | 443 | if pps.entropy_coding_mode { |
| 414 | return Err(Error::unsupported( | 444 | return Err(Error::unsupported( |
| 415 | "CABAC entropy coding", | 445 | "CABAC entropy coding", |
| 416 | - "only CAVLC is implemented (S18b)", | 446 | + "entropy_coding_mode_flag 1 needs the arithmetic decoder and the \ |
| 447 | + context models of clause 9.3; only CAVLC (9.2) is implemented", | ||
| 417 | )); | 448 | )); |
| 418 | } | 449 | } |
| 419 | if pps.num_slice_groups > 1 { | 450 | if pps.num_slice_groups > 1 { |
| 420 | return Err(Error::unsupported( | 451 | return Err(Error::unsupported( |
| 421 | "FMO slice groups", | 452 | "FMO slice groups", |
| 422 | - "num_slice_groups > 1 not implemented", | 453 | + "num_slice_groups_minus1 > 0 needs the macroblock-to-slice-group \ |
| 454 | + map of clause 8.2.2; only one slice group per picture is decoded", | ||
| 423 | )); | 455 | )); |
| 424 | } | 456 | } |
| 425 | if sps.chroma_format_idc != 1 { | 457 | if sps.chroma_format_idc != 1 { |
| 426 | return Err(Error::unsupported( | 458 | return Err(Error::unsupported( |
| 427 | "chroma format", | 459 | "chroma format", |
| 428 | - format!("chroma_format_idc {} (only 4:2:0)", sps.chroma_format_idc), | 460 | + format!( |
| 461 | + "chroma_format_idc {} needs its own chroma prediction, scan and \ | ||
| 462 | + DC transform; only 4:2:0 (ChromaArrayType 1) is decoded", | ||
| 463 | + sps.chroma_format_idc | ||
| 464 | + ), | ||
| 429 | )); | 465 | )); |
| 430 | } | 466 | } |
| 431 | if sps.bit_depth_luma != 8 || sps.bit_depth_chroma != 8 { | 467 | if sps.bit_depth_luma != 8 || sps.bit_depth_chroma != 8 { |
| 432 | return Err(Error::unsupported( | 468 | return Err(Error::unsupported( |
| 433 | "bit depth", | 469 | "bit depth", |
| 434 | format!( | 470 | format!( |
| 435 | - "{}-bit luma / {}-bit chroma (only 8-bit)", | 471 | + "{}-bit luma / {}-bit chroma needs 16-bit sample planes and the \ |
| 472 | + widened transform clip of clause 8.5; only 8-bit is decoded", | ||
| 436 | sps.bit_depth_luma, sps.bit_depth_chroma | 473 | sps.bit_depth_luma, sps.bit_depth_chroma |
| 437 | ), | 474 | ), |
| 438 | )); | 475 | )); |
| @@ -440,19 +477,22 @@ fn check_supported(sps: &Sps, pps: &Pps, slice_type_code: u32) -> Result<()> { | |||
| 440 | if !sps.frame_mbs_only { | 477 | if !sps.frame_mbs_only { |
| 441 | return Err(Error::unsupported( | 478 | return Err(Error::unsupported( |
| 442 | "interlaced coding", | 479 | "interlaced coding", |
| 443 | - "field pictures / MBAFF not implemented", | 480 | + "frame_mbs_only_flag 0 admits field pictures and MBAFF, whose \ |
| 481 | + neighbour derivation (6.4.9) and field deblocking are not implemented", | ||
| 444 | )); | 482 | )); |
| 445 | } | 483 | } |
| 446 | if sps.separate_colour_plane { | 484 | if sps.separate_colour_plane { |
| 447 | return Err(Error::unsupported( | 485 | return Err(Error::unsupported( |
| 448 | "separate colour planes", | 486 | "separate colour planes", |
| 449 | - "4:4:4 separate planes not implemented", | 487 | + "separate_colour_plane_flag 1 codes 4:4:4 as three monochrome \ |
| 488 | + planes with their own slice headers; not implemented", | ||
| 450 | )); | 489 | )); |
| 451 | } | 490 | } |
| 452 | if sps.transform_bypass { | 491 | if sps.transform_bypass { |
| 453 | return Err(Error::unsupported( | 492 | return Err(Error::unsupported( |
| 454 | "transform bypass", | 493 | "transform bypass", |
| 455 | - "qpprime_y_zero_transform_bypass not implemented", | 494 | + "qpprime_y_zero_transform_bypass_flag 1 makes QP'Y 0 lossless, \ |
| 495 | + bypassing the transform and scaling of 8.5 entirely; not implemented", | ||
| 456 | )); | 496 | )); |
| 457 | } | 497 | } |
| 458 | if slice_type_code > 9 { | 498 | if slice_type_code > 9 { |
| @@ -619,7 +659,8 @@ fn decode_macroblock( | |||
| 619 | if !is_i16 && ctx.transform_8x8_mode && r.read_bit()? { | 659 | if !is_i16 && ctx.transform_8x8_mode && r.read_bit()? { |
| 620 | return Err(Error::unsupported( | 660 | return Err(Error::unsupported( |
| 621 | "8x8 transform", | 661 | "8x8 transform", |
| 622 | - "transform_size_8x8_flag not implemented", | 662 | + "transform_size_8x8_flag 1 selects the 8x8 intra prediction of \ |
| 663 | + clause 8.3.2 and the 8x8 transform of 8.5.13; not implemented", | ||
| 623 | )); | 664 | )); |
| 624 | } | 665 | } |
| 625 | 666 | ||
crates/ec-h264/src/lib.rsmodified+22 -0
| @@ -7,16 +7,38 @@ | |||
| 7 | //! | 7 | //! |
| 8 | //! Implemented from Rec. ITU-T H.264 (V15, 08/2024) only; no third-party | 8 | //! Implemented from Rec. ITU-T H.264 (V15, 08/2024) only; no third-party |
| 9 | //! decoder source was consulted. | 9 | //! decoder source was consulted. |
| 10 | +//! | ||
| 11 | +//! Two entry surfaces over one decode core: [`Decoder`] takes NAL units, which | ||
| 12 | +//! is what a bitstream tool has, and [`H264Decoder`] implements | ||
| 13 | +//! [`ec_core::registry::Decoder`] over packets — including `avcC` extradata and | ||
| 14 | +//! length-prefixed NAL units — which is what a demuxer hands over. | ||
| 15 | +//! | ||
| 16 | +//! ```no_run | ||
| 17 | +//! use ec_core::registry::{CodecId, CodecParameters, Decoder}; | ||
| 18 | +//! use ec_core::{Packet, TimeBase}; | ||
| 19 | +//! use ec_h264::H264Decoder; | ||
| 20 | +//! | ||
| 21 | +//! # fn main() -> ec_core::Result<()> { | ||
| 22 | +//! let annex_b: Vec<u8> = std::fs::read("stream.264")?; | ||
| 23 | +//! let mut decoder = H264Decoder::new(CodecParameters::new(CodecId::H264))?; | ||
| 24 | +//! decoder.send_packet(&Packet::new(0, TimeBase::new(1, 25), annex_b))?; | ||
| 25 | +//! let frame = decoder.receive_frame()?; // the first decoded picture, I420 | ||
| 26 | +//! # let _ = frame; | ||
| 27 | +//! # Ok(()) | ||
| 28 | +//! # } | ||
| 29 | +//! ``` | ||
| 10 | 30 | ||
| 11 | #![forbid(unsafe_code)] | 31 | #![forbid(unsafe_code)] |
| 12 | 32 | ||
| 13 | mod bits; | 33 | mod bits; |
| 14 | mod cavlc; | 34 | mod cavlc; |
| 35 | +mod codec; | ||
| 15 | mod deblock; | 36 | mod deblock; |
| 16 | mod decoder; | 37 | mod decoder; |
| 17 | mod pred; | 38 | mod pred; |
| 18 | mod tables; | 39 | mod tables; |
| 19 | mod transform; | 40 | mod transform; |
| 20 | 41 | ||
| 42 | +pub use codec::H264Decoder; | ||
| 21 | pub use decoder::{Decoder, NalOutcome}; | 43 | pub use decoder::{Decoder, NalOutcome}; |
| 22 | pub use ec_core::error::{Error, Result}; | 44 | pub use ec_core::error::{Error, Result}; |