edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Frame and picture headers (RDD 36 section 5). |
| 2 | |
| 3 | use ec_core::error::{Error, Result}; |
| 4 | use ec_core::frame::Interlace; |
| 5 | |
| 6 | /// Largest picture side accepted (ProRes goes to 8K in practice). |
| 7 | pub const MAX_DIM: u32 = 16384; |
| 8 | /// Largest picture area accepted, in luma samples. |
| 9 | pub const MAX_AREA: u64 = 8192 * 4608; |
| 10 | |
| 11 | /// What the frame header says. |
| 12 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 13 | pub struct FrameInfo { |
| 14 | /// Visible width. |
| 15 | pub width: u32, |
| 16 | /// Visible height. |
| 17 | pub height: u32, |
| 18 | /// Header `version` field (0 or 1). |
| 19 | pub version: u16, |
| 20 | /// Four-byte encoder id (`Lavc`, `apl0`, ...). |
| 21 | pub creator: [u8; 4], |
| 22 | /// True for 4:4:4 (profiles 4444 / 4444 XQ), false for 4:2:2. |
| 23 | pub is_444: bool, |
| 24 | /// Interlace of the coded frame. |
| 25 | pub interlace: Interlace, |
| 26 | /// 0 = no alpha, 8 or 16 = coded alpha depth. |
| 27 | pub alpha_bits: u8, |
| 28 | /// Colour primaries / transfer / matrix code points as signalled. |
| 29 | pub primaries: u8, |
| 30 | /// Transfer characteristics code point. |
| 31 | pub transfer: u8, |
| 32 | /// Matrix coefficients code point. |
| 33 | pub matrix: u8, |
| 34 | /// The frame carried its own luma quantisation matrix. |
| 35 | pub custom_luma_qmat: bool, |
| 36 | /// The frame carried its own chroma quantisation matrix. |
| 37 | pub custom_chroma_qmat: bool, |
| 38 | /// `log2` of the nominal macroblocks per slice (0..=3) of the first picture. |
| 39 | pub log2_slice_mbs: u8, |
| 40 | } |
| 41 | |
| 42 | pub struct Header { |
| 43 | pub info: FrameInfo, |
| 44 | pub qmat_luma: [u8; 64], |
| 45 | pub qmat_chroma: [u8; 64], |
| 46 | /// Offset of the first picture header inside the frame data. |
| 47 | pub pictures_at: usize, |
| 48 | /// End of the frame (from the frame size field). |
| 49 | pub end: usize, |
| 50 | } |
| 51 | |
| 52 | pub fn be16(d: &[u8], at: usize) -> Option<u16> { |
| 53 | Some(u16::from_be_bytes([*d.get(at)?, *d.get(at + 1)?])) |
| 54 | } |
| 55 | pub fn be32(d: &[u8], at: usize) -> Option<u32> { |
| 56 | Some(u32::from_be_bytes([*d.get(at)?, *d.get(at + 1)?, *d.get(at + 2)?, *d.get(at + 3)?])) |
| 57 | } |
| 58 | |
| 59 | pub fn parse_frame(data: &[u8]) -> Result<Header> { |
| 60 | let size = be32(data, 0).ok_or_else(|| Error::corrupt("ProRes frame: shorter than the size field"))? as usize; |
| 61 | if data.get(4..8) != Some(b"icpf") { |
| 62 | return Err(Error::corrupt("ProRes frame: missing 'icpf' tag")); |
| 63 | } |
| 64 | if size < 28 || size > data.len() { |
| 65 | return Err(Error::corrupt(format!( |
| 66 | "ProRes frame: size field {size} against {} bytes of data", |
| 67 | data.len() |
| 68 | ))); |
| 69 | } |
| 70 | let d = &data[8..size]; |
| 71 | let hdr_size = be16(d, 0).ok_or_else(|| Error::corrupt("ProRes frame header: truncated"))? as usize; |
| 72 | if hdr_size < 20 || hdr_size > d.len() { |
| 73 | return Err(Error::corrupt(format!("ProRes frame header: size {hdr_size}"))); |
| 74 | } |
| 75 | let version = be16(d, 2).unwrap_or(0); |
| 76 | if version > 1 { |
| 77 | return Err(Error::unsupported( |
| 78 | format!("ProRes bitstream version {version}"), |
| 79 | "only versions 0 and 1 are defined", |
| 80 | )); |
| 81 | } |
| 82 | let width = u32::from(be16(d, 8).unwrap_or(0)); |
| 83 | let height = u32::from(be16(d, 10).unwrap_or(0)); |
| 84 | if width == 0 || height == 0 || width > MAX_DIM || height > MAX_DIM || u64::from(width) * u64::from(height) > MAX_AREA { |
| 85 | return Err(Error::corrupt(format!("ProRes frame header: picture size {width}x{height}"))); |
| 86 | } |
| 87 | let chroma_factor = d[12] >> 6; |
| 88 | let is_444 = match chroma_factor { |
| 89 | 2 => false, |
| 90 | 3 => true, |
| 91 | c => { |
| 92 | return Err(Error::unsupported( |
| 93 | format!("ProRes chroma format {c}"), |
| 94 | "only 4:2:2 (2) and 4:4:4 (3) are defined", |
| 95 | )); |
| 96 | } |
| 97 | }; |
| 98 | let interlace = match (d[12] >> 2) & 3 { |
| 99 | 0 => Interlace::Progressive, |
| 100 | 1 => Interlace::TopFieldFirst, |
| 101 | 2 => Interlace::BottomFieldFirst, |
| 102 | t => return Err(Error::corrupt(format!("ProRes frame header: frame_type {t}"))), |
| 103 | }; |
| 104 | let alpha_bits = match d[17] & 0xf { |
| 105 | 0 => 0, |
| 106 | 1 => 8, |
| 107 | 2 => 16, |
| 108 | a => { |
| 109 | return Err(Error::unsupported( |
| 110 | format!("ProRes alpha_channel_type {a}"), |
| 111 | "only none (0), 8-bit (1) and 16-bit (2) are defined", |
| 112 | )); |
| 113 | } |
| 114 | }; |
| 115 | if alpha_bits != 0 && !is_444 { |
| 116 | return Err(Error::corrupt("ProRes frame header: alpha channel in a 4:2:2 frame")); |
| 117 | } |
| 118 | let flags = d[19]; |
| 119 | let mut qmat_luma = [4u8; 64]; |
| 120 | let mut qmat_chroma = [4u8; 64]; |
| 121 | let mut p = 20; |
| 122 | let need = |p: usize| -> Result<()> { |
| 123 | if p + 64 > hdr_size { |
| 124 | Err(Error::corrupt("ProRes frame header: quantisation matrix past the header")) |
| 125 | } else { |
| 126 | Ok(()) |
| 127 | } |
| 128 | }; |
| 129 | let custom_luma_qmat = flags & 2 != 0; |
| 130 | if custom_luma_qmat { |
| 131 | need(p)?; |
| 132 | qmat_luma.copy_from_slice(&d[p..p + 64]); |
| 133 | p += 64; |
| 134 | } |
| 135 | let custom_chroma_qmat = flags & 1 != 0; |
| 136 | if custom_chroma_qmat { |
| 137 | need(p)?; |
| 138 | qmat_chroma.copy_from_slice(&d[p..p + 64]); |
| 139 | } else { |
| 140 | // RDD 36: an absent chroma matrix repeats the luma one. |
| 141 | qmat_chroma = qmat_luma; |
| 142 | } |
| 143 | Ok(Header { |
| 144 | info: FrameInfo { |
| 145 | width, |
| 146 | height, |
| 147 | version, |
| 148 | creator: [d[4], d[5], d[6], d[7]], |
| 149 | is_444, |
| 150 | interlace, |
| 151 | alpha_bits, |
| 152 | primaries: d[14], |
| 153 | transfer: d[15], |
| 154 | matrix: d[16], |
| 155 | custom_luma_qmat, |
| 156 | custom_chroma_qmat, |
| 157 | log2_slice_mbs: 0, |
| 158 | }, |
| 159 | qmat_luma, |
| 160 | qmat_chroma, |
| 161 | pictures_at: 8 + hdr_size, |
| 162 | end: size, |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | /// One picture header plus its slice table. |
| 167 | pub struct Picture { |
| 168 | pub slice_count: usize, |
| 169 | pub log2_slice_mbs: u32, |
| 170 | /// Offset (in the frame) of the slice size table. |
| 171 | pub table_at: usize, |
| 172 | /// Offset of the first slice. |
| 173 | pub data_at: usize, |
| 174 | /// End of this picture. |
| 175 | pub end: usize, |
| 176 | } |
| 177 | |
| 178 | pub fn parse_picture(frame: &[u8], at: usize, end: usize) -> Result<Picture> { |
| 179 | let p = frame.get(at..end).ok_or_else(|| Error::corrupt("ProRes picture: starts past the frame"))?; |
| 180 | if p.len() < 8 { |
| 181 | return Err(Error::corrupt("ProRes picture header: truncated")); |
| 182 | } |
| 183 | let hdr = usize::from(p[0] >> 3); |
| 184 | let pic_size = be32(p, 1).unwrap_or(0) as usize; |
| 185 | let slice_count = usize::from(be16(p, 5).unwrap_or(0)); |
| 186 | let log2 = u32::from(p[7] >> 4); |
| 187 | if hdr < 8 || hdr > p.len() { |
| 188 | return Err(Error::corrupt(format!("ProRes picture header: size {hdr}"))); |
| 189 | } |
| 190 | if log2 > 3 || p[7] & 0xf != 0 { |
| 191 | return Err(Error::unsupported( |
| 192 | format!("ProRes slice size byte {:#x}", p[7]), |
| 193 | "slices are 1, 2, 4 or 8 macroblocks wide and one macroblock tall", |
| 194 | )); |
| 195 | } |
| 196 | if pic_size < hdr + slice_count * 2 || pic_size > p.len() { |
| 197 | return Err(Error::corrupt(format!( |
| 198 | "ProRes picture header: picture size {pic_size} (header {hdr}, {slice_count} slices, {} bytes left)", |
| 199 | p.len() |
| 200 | ))); |
| 201 | } |
| 202 | Ok(Picture { |
| 203 | slice_count, |
| 204 | log2_slice_mbs: log2, |
| 205 | table_at: at + hdr, |
| 206 | data_at: at + hdr + slice_count * 2, |
| 207 | end: at + pic_size, |
| 208 | }) |
| 209 | } |