Write the key-frame header OBU
Browse files at this commite203345d4f4ab9988068dc6aebd7c476c9624320Parent: 4cb83b2- feat
- write the key-frame header OBU
3 changed files +652 -2
- crates/ec-av1/src/frame.rs +649 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/sequence.rs +2 -2
crates/ec-av1/src/frame.rsadded+649 -0
| @@ -0,0 +1,649 @@ | ||
| 1 | +//! The frame header OBU (spec 5.9), for shown key frames. | |
| 2 | +//! | |
| 3 | +//! Every writer here is the bit-exact inverse of the matching reader in | |
| 4 | +//! [`ec_av1_syntax::frame`], and the tests prove it by parsing back what they | |
| 5 | +//! wrote. The subset is the one a stateless hardware encoder needs: a key frame | |
| 6 | +//! that is shown, in a sequence with no decoder model, no frame ids and no | |
| 7 | +//! still-picture shortcut. Everything outside it is refused by name rather than | |
| 8 | +//! written wrong — superres with a denominator other than 8, film grain | |
| 9 | +//! synthesis, non-uniform tile spacing, and every non-key or hidden frame. | |
| 10 | + | |
| 11 | +use ec_av1_syntax::obu::{ObuHeader, ObuType, tile_log2}; | |
| 12 | +use ec_av1_syntax::sequence::{SELECT_INTEGER_MV, SELECT_SCREEN_CONTENT_TOOLS, SequenceHeader}; | |
| 13 | +use ec_av1_syntax::{ | |
| 14 | + FrameHeader, FrameType, MAX_SEGMENTS, MAX_TILE_AREA, MAX_TILE_COLS, MAX_TILE_ROWS, | |
| 15 | + MAX_TILE_WIDTH, PRIMARY_REF_NONE, RestorationType, SEG_LVL_MAX, TOTAL_REFS_PER_FRAME, TxMode, | |
| 16 | +}; | |
| 17 | +use ec_core::{BitWriter, Error, Result}; | |
| 18 | + | |
| 19 | +use crate::bits::{write_byte_alignment, write_delta_q, write_su}; | |
| 20 | +use crate::obu::wrap_obu; | |
| 21 | + | |
| 22 | +/// `Segmentation_Feature_Bits` (spec 5.9.14). | |
| 23 | +const SEG_FEATURE_BITS: [u32; SEG_LVL_MAX] = [8, 6, 6, 6, 6, 3, 0, 0]; | |
| 24 | +/// `Segmentation_Feature_Signed` (spec 5.9.14). | |
| 25 | +const SEG_FEATURE_SIGNED: [bool; SEG_LVL_MAX] = [true, true, true, true, true, false, false, false]; | |
| 26 | +/// The loop filter reference deltas a frame with no primary reference starts | |
| 27 | +/// from (spec 6.8.2, `setup_past_independence`); only the ones that differ are | |
| 28 | +/// coded. | |
| 29 | +const DEFAULT_REF_DELTAS: [i8; TOTAL_REFS_PER_FRAME] = [1, 0, 0, 0, -1, 0, -1, -1]; | |
| 30 | + | |
| 31 | +/// `REMAP_LR_TYPE` inverted (spec 5.9.20): the coded index is not the enum's | |
| 32 | +/// discriminant, so the mapping is spelled out. | |
| 33 | +fn lr_type_code(t: RestorationType) -> u32 { | |
| 34 | + match t { | |
| 35 | + RestorationType::None => 0, | |
| 36 | + RestorationType::Switchable => 1, | |
| 37 | + RestorationType::Wiener => 2, | |
| 38 | + RestorationType::Sgrproj => 3, | |
| 39 | + } | |
| 40 | +} | |
| 41 | + | |
| 42 | +/// The increasing-flag idiom the tile columns and rows are coded with: a one | |
| 43 | +/// per step from `min` to `target`, and a terminating zero unless `target` is | |
| 44 | +/// already the maximum. | |
| 45 | +fn ramp(w: &mut BitWriter, target: u32, min: u32, max: u32) -> Result<()> { | |
| 46 | + if target < min || target > max { | |
| 47 | + return Err(Error::corrupt("AV1 tile_info: tile log2 out of range")); | |
| 48 | + } | |
| 49 | + for _ in min..target { | |
| 50 | + w.write_bit(true); | |
| 51 | + } | |
| 52 | + if target < max { | |
| 53 | + w.write_bit(false); | |
| 54 | + } | |
| 55 | + Ok(()) | |
| 56 | +} | |
| 57 | + | |
| 58 | +fn sb_cols(seq: &SequenceHeader, mi_cols: u32) -> u32 { | |
| 59 | + if seq.use_128x128_superblock { | |
| 60 | + mi_cols.div_ceil(32) | |
| 61 | + } else { | |
| 62 | + mi_cols.div_ceil(16) | |
| 63 | + } | |
| 64 | +} | |
| 65 | + | |
| 66 | +fn sb_rows(seq: &SequenceHeader, mi_rows: u32) -> u32 { | |
| 67 | + if seq.use_128x128_superblock { | |
| 68 | + mi_rows.div_ceil(32) | |
| 69 | + } else { | |
| 70 | + mi_rows.div_ceil(16) | |
| 71 | + } | |
| 72 | +} | |
| 73 | + | |
| 74 | +/// `tile_info()` (spec 5.9.15), uniform spacing only. | |
| 75 | +fn write_tile_info(w: &mut BitWriter, seq: &SequenceHeader, h: &FrameHeader) -> Result<()> { | |
| 76 | + if !h.tile_info.uniform_spacing { | |
| 77 | + return Err(Error::unsupported( | |
| 78 | + "AV1 tile_info", | |
| 79 | + "non-uniform tile spacing is not written", | |
| 80 | + )); | |
| 81 | + } | |
| 82 | + let (cols, rows) = (sb_cols(seq, h.mi_cols), sb_rows(seq, h.mi_rows)); | |
| 83 | + let sb_size = if seq.use_128x128_superblock { 7 } else { 6 }; | |
| 84 | + let max_tile_width_sb = MAX_TILE_WIDTH >> sb_size; | |
| 85 | + let max_tile_area_sb = MAX_TILE_AREA >> (2 * sb_size); | |
| 86 | + let min_log2_tile_cols = tile_log2(max_tile_width_sb, cols); | |
| 87 | + let max_log2_tile_cols = tile_log2(1, cols.min(MAX_TILE_COLS)); | |
| 88 | + let max_log2_tile_rows = tile_log2(1, rows.min(MAX_TILE_ROWS)); | |
| 89 | + let min_log2_tiles = min_log2_tile_cols.max(tile_log2(max_tile_area_sb, rows * cols)); | |
| 90 | + | |
| 91 | + w.write_bit(true); | |
| 92 | + ramp( | |
| 93 | + w, | |
| 94 | + h.tile_info.cols_log2, | |
| 95 | + min_log2_tile_cols, | |
| 96 | + max_log2_tile_cols, | |
| 97 | + )?; | |
| 98 | + let rows_start = min_log2_tiles.saturating_sub(h.tile_info.cols_log2); | |
| 99 | + ramp(w, h.tile_info.rows_log2, rows_start, max_log2_tile_rows)?; | |
| 100 | + if h.tile_info.cols_log2 > 0 || h.tile_info.rows_log2 > 0 { | |
| 101 | + w.write_bits( | |
| 102 | + h.tile_info.context_update_tile_id, | |
| 103 | + h.tile_info.rows_log2 + h.tile_info.cols_log2, | |
| 104 | + ); | |
| 105 | + w.write_bits(h.tile_info.tile_size_bytes - 1, 2); | |
| 106 | + } | |
| 107 | + Ok(()) | |
| 108 | +} | |
| 109 | + | |
| 110 | +/// `quantization_params()` (spec 5.9.12). | |
| 111 | +fn write_quantization_params(w: &mut BitWriter, seq: &SequenceHeader, h: &FrameHeader) { | |
| 112 | + let color = &seq.color_config; | |
| 113 | + let q = &h.quantization; | |
| 114 | + w.write_bits(u32::from(q.base_q_idx), 8); | |
| 115 | + write_delta_q(w, i32::from(q.delta_q_y_dc)); | |
| 116 | + if color.num_planes > 1 { | |
| 117 | + let diff_uv_delta = if color.separate_uv_delta_q { | |
| 118 | + let d = q.delta_q_v_dc != q.delta_q_u_dc || q.delta_q_v_ac != q.delta_q_u_ac; | |
| 119 | + w.write_bit(d); | |
| 120 | + d | |
| 121 | + } else { | |
| 122 | + false | |
| 123 | + }; | |
| 124 | + write_delta_q(w, i32::from(q.delta_q_u_dc)); | |
| 125 | + write_delta_q(w, i32::from(q.delta_q_u_ac)); | |
| 126 | + if diff_uv_delta { | |
| 127 | + write_delta_q(w, i32::from(q.delta_q_v_dc)); | |
| 128 | + write_delta_q(w, i32::from(q.delta_q_v_ac)); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + w.write_bit(q.using_qmatrix); | |
| 132 | + if q.using_qmatrix { | |
| 133 | + w.write_bits(u32::from(q.qm_y), 4); | |
| 134 | + w.write_bits(u32::from(q.qm_u), 4); | |
| 135 | + if color.separate_uv_delta_q { | |
| 136 | + w.write_bits(u32::from(q.qm_v), 4); | |
| 137 | + } | |
| 138 | + } | |
| 139 | +} | |
| 140 | + | |
| 141 | +/// `segmentation_params()` (spec 5.9.14). The frame this writer codes has no | |
| 142 | +/// primary reference, so the update flags are forced and not coded. | |
| 143 | +fn write_segmentation_params(w: &mut BitWriter, h: &FrameHeader) { | |
| 144 | + let seg = &h.segmentation; | |
| 145 | + w.write_bit(seg.enabled); | |
| 146 | + if !seg.enabled { | |
| 147 | + return; | |
| 148 | + } | |
| 149 | + if h.primary_ref_frame != PRIMARY_REF_NONE { | |
| 150 | + w.write_bit(seg.update_map); | |
| 151 | + if seg.update_map { | |
| 152 | + w.write_bit(seg.temporal_update); | |
| 153 | + } | |
| 154 | + w.write_bit(seg.update_data); | |
| 155 | + } | |
| 156 | + if seg.update_data || h.primary_ref_frame == PRIMARY_REF_NONE { | |
| 157 | + for segment in 0..MAX_SEGMENTS { | |
| 158 | + for feature in 0..SEG_LVL_MAX { | |
| 159 | + let enabled = seg.feature_enabled[segment][feature]; | |
| 160 | + w.write_bit(enabled); | |
| 161 | + if !enabled { | |
| 162 | + continue; | |
| 163 | + } | |
| 164 | + let bits = SEG_FEATURE_BITS[feature]; | |
| 165 | + let value = i32::from(seg.feature_data[segment][feature]); | |
| 166 | + if SEG_FEATURE_SIGNED[feature] { | |
| 167 | + write_su(w, 1 + bits, value); | |
| 168 | + } else if bits > 0 { | |
| 169 | + w.write_bits(value as u32, bits); | |
| 170 | + } | |
| 171 | + } | |
| 172 | + } | |
| 173 | + } | |
| 174 | +} | |
| 175 | + | |
| 176 | +/// `delta_q_params()` (spec 5.9.17). | |
| 177 | +fn write_delta_q_params(w: &mut BitWriter, h: &FrameHeader) { | |
| 178 | + if h.quantization.base_q_idx > 0 { | |
| 179 | + w.write_bit(h.delta.q_present); | |
| 180 | + } | |
| 181 | + if h.delta.q_present { | |
| 182 | + w.write_bits(u32::from(h.delta.q_res), 2); | |
| 183 | + } | |
| 184 | +} | |
| 185 | + | |
| 186 | +/// `delta_lf_params()` (spec 5.9.18). | |
| 187 | +fn write_delta_lf_params(w: &mut BitWriter, h: &FrameHeader) { | |
| 188 | + if !h.delta.q_present { | |
| 189 | + return; | |
| 190 | + } | |
| 191 | + if !h.allow_intrabc { | |
| 192 | + w.write_bit(h.delta.lf_present); | |
| 193 | + } | |
| 194 | + if h.delta.lf_present { | |
| 195 | + w.write_bits(u32::from(h.delta.lf_res), 2); | |
| 196 | + w.write_bit(h.delta.lf_multi); | |
| 197 | + } | |
| 198 | +} | |
| 199 | + | |
| 200 | +/// `loop_filter_params()` (spec 5.9.11). | |
| 201 | +fn write_loop_filter_params( | |
| 202 | + w: &mut BitWriter, | |
| 203 | + seq: &SequenceHeader, | |
| 204 | + h: &FrameHeader, | |
| 205 | + coded_lossless: bool, | |
| 206 | +) { | |
| 207 | + if coded_lossless || h.allow_intrabc { | |
| 208 | + return; | |
| 209 | + } | |
| 210 | + let lf = &h.loop_filter; | |
| 211 | + w.write_bits(u32::from(lf.level[0]), 6); | |
| 212 | + w.write_bits(u32::from(lf.level[1]), 6); | |
| 213 | + if seq.color_config.num_planes > 1 && (lf.level[0] > 0 || lf.level[1] > 0) { | |
| 214 | + w.write_bits(u32::from(lf.level[2]), 6); | |
| 215 | + w.write_bits(u32::from(lf.level[3]), 6); | |
| 216 | + } | |
| 217 | + w.write_bits(u32::from(lf.sharpness), 3); | |
| 218 | + w.write_bit(lf.delta_enabled); | |
| 219 | + if !lf.delta_enabled { | |
| 220 | + return; | |
| 221 | + } | |
| 222 | + w.write_bit(lf.delta_update); | |
| 223 | + if !lf.delta_update { | |
| 224 | + return; | |
| 225 | + } | |
| 226 | + for (i, &delta) in lf.ref_deltas.iter().enumerate() { | |
| 227 | + let update = delta != DEFAULT_REF_DELTAS[i]; | |
| 228 | + w.write_bit(update); | |
| 229 | + if update { | |
| 230 | + write_su(w, 7, i32::from(delta)); | |
| 231 | + } | |
| 232 | + } | |
| 233 | + for &delta in &lf.mode_deltas { | |
| 234 | + let update = delta != 0; | |
| 235 | + w.write_bit(update); | |
| 236 | + if update { | |
| 237 | + write_su(w, 7, i32::from(delta)); | |
| 238 | + } | |
| 239 | + } | |
| 240 | +} | |
| 241 | + | |
| 242 | +/// `cdef_params()` (spec 5.9.19). | |
| 243 | +fn write_cdef_params( | |
| 244 | + w: &mut BitWriter, | |
| 245 | + seq: &SequenceHeader, | |
| 246 | + h: &FrameHeader, | |
| 247 | + coded_lossless: bool, | |
| 248 | +) { | |
| 249 | + if coded_lossless || h.allow_intrabc || !seq.enable_cdef { | |
| 250 | + return; | |
| 251 | + } | |
| 252 | + let cdef = &h.cdef; | |
| 253 | + w.write_bits(u32::from(cdef.damping - 3), 2); | |
| 254 | + w.write_bits(u32::from(cdef.bits), 2); | |
| 255 | + // A secondary strength of 4 is coded as 3; the reader maps it back. | |
| 256 | + let coded_sec = |s: u8| u32::from(if s == 4 { 3 } else { s }); | |
| 257 | + for i in 0..(1usize << cdef.bits) { | |
| 258 | + w.write_bits(u32::from(cdef.y_pri_strength[i]), 4); | |
| 259 | + w.write_bits(coded_sec(cdef.y_sec_strength[i]), 2); | |
| 260 | + if seq.color_config.num_planes > 1 { | |
| 261 | + w.write_bits(u32::from(cdef.uv_pri_strength[i]), 4); | |
| 262 | + w.write_bits(coded_sec(cdef.uv_sec_strength[i]), 2); | |
| 263 | + } | |
| 264 | + } | |
| 265 | +} | |
| 266 | + | |
| 267 | +/// `lr_params()` (spec 5.9.20). | |
| 268 | +fn write_lr_params(w: &mut BitWriter, seq: &SequenceHeader, h: &FrameHeader, all_lossless: bool) { | |
| 269 | + if all_lossless || h.allow_intrabc || !seq.enable_restoration { | |
| 270 | + return; | |
| 271 | + } | |
| 272 | + let lr = &h.loop_restoration; | |
| 273 | + let (mut uses_lr, mut uses_chroma_lr) = (false, false); | |
| 274 | + for plane in 0..seq.color_config.num_planes as usize { | |
| 275 | + w.write_bits(lr_type_code(lr.frame_restoration_type[plane]), 2); | |
| 276 | + if lr.frame_restoration_type[plane] != RestorationType::None { | |
| 277 | + uses_lr = true; | |
| 278 | + uses_chroma_lr |= plane > 0; | |
| 279 | + } | |
| 280 | + } | |
| 281 | + if !uses_lr { | |
| 282 | + return; | |
| 283 | + } | |
| 284 | + if seq.use_128x128_superblock { | |
| 285 | + w.write_bits(u32::from(lr.lr_unit_shift) - 1, 1); | |
| 286 | + } else { | |
| 287 | + w.write_bit(lr.lr_unit_shift > 0); | |
| 288 | + if lr.lr_unit_shift > 0 { | |
| 289 | + w.write_bits(u32::from(lr.lr_unit_shift) - 1, 1); | |
| 290 | + } | |
| 291 | + } | |
| 292 | + if seq.color_config.subsampling_x == 1 && seq.color_config.subsampling_y == 1 && uses_chroma_lr | |
| 293 | + { | |
| 294 | + w.write_bits(u32::from(lr.lr_uv_shift), 1); | |
| 295 | + } | |
| 296 | +} | |
| 297 | + | |
| 298 | +/// `uncompressed_header()` (spec 5.9.2) for a shown key frame. | |
| 299 | +pub fn write_frame_header(w: &mut BitWriter, seq: &SequenceHeader, h: &FrameHeader) -> Result<()> { | |
| 300 | + if h.frame_type != FrameType::Key || !h.show_frame || h.show_existing_frame { | |
| 301 | + return Err(Error::unsupported( | |
| 302 | + "AV1 frame header", | |
| 303 | + "only a shown key frame is written", | |
| 304 | + )); | |
| 305 | + } | |
| 306 | + if seq.reduced_still_picture_header { | |
| 307 | + return Err(Error::unsupported( | |
| 308 | + "AV1 frame header", | |
| 309 | + "reduced_still_picture_header changes the whole header layout", | |
| 310 | + )); | |
| 311 | + } | |
| 312 | + if seq.decoder_model_info.is_some() { | |
| 313 | + return Err(Error::unsupported( | |
| 314 | + "AV1 frame header", | |
| 315 | + "decoder model timing is not written", | |
| 316 | + )); | |
| 317 | + } | |
| 318 | + if seq.frame_id_numbers_present_flag { | |
| 319 | + return Err(Error::unsupported( | |
| 320 | + "AV1 frame header", | |
| 321 | + "frame id numbers are not written", | |
| 322 | + )); | |
| 323 | + } | |
| 324 | + | |
| 325 | + w.write_bit(false); // show_existing_frame | |
| 326 | + w.write_bits(0, 2); // frame_type: KEY_FRAME | |
| 327 | + w.write_bit(true); // show_frame | |
| 328 | + // showable_frame and error_resilient_mode are forced for a shown key frame. | |
| 329 | + | |
| 330 | + w.write_bit(h.disable_cdf_update); | |
| 331 | + if seq.seq_force_screen_content_tools == SELECT_SCREEN_CONTENT_TOOLS { | |
| 332 | + w.write_bit(h.allow_screen_content_tools); | |
| 333 | + } else if h.allow_screen_content_tools != (seq.seq_force_screen_content_tools != 0) { | |
| 334 | + return Err(Error::corrupt( | |
| 335 | + "AV1 frame header: allow_screen_content_tools contradicts the sequence", | |
| 336 | + )); | |
| 337 | + } | |
| 338 | + if h.allow_screen_content_tools && seq.seq_force_integer_mv == SELECT_INTEGER_MV { | |
| 339 | + // The reader forces this back to true for an intra frame; the bit is | |
| 340 | + // still coded, and the value it carries is the one the reader discards. | |
| 341 | + w.write_bit(h.force_integer_mv); | |
| 342 | + } | |
| 343 | + w.write_bit(h.frame_size_override_flag); | |
| 344 | + w.write_bits(h.order_hint, seq.order_hint_bits); | |
| 345 | + // primary_ref_frame, buffer_removal_time, refresh_frame_flags and the | |
| 346 | + // ref_order_hint loop are all forced for a shown key frame. | |
| 347 | + | |
| 348 | + if h.frame_size_override_flag { | |
| 349 | + w.write_bits(h.frame_width - 1, seq.frame_width_bits); | |
| 350 | + w.write_bits(h.frame_height - 1, seq.frame_height_bits); | |
| 351 | + } | |
| 352 | + if seq.enable_superres { | |
| 353 | + w.write_bit(h.use_superres); | |
| 354 | + } | |
| 355 | + if h.use_superres || h.superres_denom != 8 { | |
| 356 | + return Err(Error::unsupported( | |
| 357 | + "AV1 superres", | |
| 358 | + "only a superres denominator of 8 (no scaling) is written", | |
| 359 | + )); | |
| 360 | + } | |
| 361 | + let render_differs = h.render_width != h.upscaled_width || h.render_height != h.frame_height; | |
| 362 | + w.write_bit(render_differs); | |
| 363 | + if render_differs { | |
| 364 | + w.write_bits(h.render_width - 1, 16); | |
| 365 | + w.write_bits(h.render_height - 1, 16); | |
| 366 | + } | |
| 367 | + if h.allow_screen_content_tools && h.upscaled_width == h.frame_width { | |
| 368 | + w.write_bit(h.allow_intrabc); | |
| 369 | + } | |
| 370 | + if !h.disable_cdf_update { | |
| 371 | + w.write_bit(h.disable_frame_end_update_cdf); | |
| 372 | + } | |
| 373 | + | |
| 374 | + write_tile_info(w, seq, h)?; | |
| 375 | + write_quantization_params(w, seq, h); | |
| 376 | + write_segmentation_params(w, h); | |
| 377 | + write_delta_q_params(w, h); | |
| 378 | + write_delta_lf_params(w, h); | |
| 379 | + | |
| 380 | + // `coded_lossless` gates the loop filter, CDEF, restoration and transform | |
| 381 | + // mode syntax; it is derived, never coded (spec 5.9.2). | |
| 382 | + let q = &h.quantization; | |
| 383 | + let coded_lossless = (0..MAX_SEGMENTS).all(|segment| { | |
| 384 | + h.segment_qindex(segment) == 0 | |
| 385 | + && q.delta_q_y_dc == 0 | |
| 386 | + && q.delta_q_u_ac == 0 | |
| 387 | + && q.delta_q_u_dc == 0 | |
| 388 | + && q.delta_q_v_ac == 0 | |
| 389 | + && q.delta_q_v_dc == 0 | |
| 390 | + }); | |
| 391 | + let all_lossless = coded_lossless && h.frame_width == h.upscaled_width; | |
| 392 | + | |
| 393 | + write_loop_filter_params(w, seq, h, coded_lossless); | |
| 394 | + write_cdef_params(w, seq, h, coded_lossless); | |
| 395 | + write_lr_params(w, seq, h, all_lossless); | |
| 396 | + if coded_lossless { | |
| 397 | + if h.tx_mode != TxMode::Only4x4 { | |
| 398 | + return Err(Error::corrupt( | |
| 399 | + "AV1 frame header: a lossless frame can only use TX_MODE_ONLY_4X4", | |
| 400 | + )); | |
| 401 | + } | |
| 402 | + } else { | |
| 403 | + w.write_bit(h.tx_mode == TxMode::Select); | |
| 404 | + } | |
| 405 | + // reference_select, skip mode and warped motion are all forced for intra. | |
| 406 | + w.write_bit(h.reduced_tx_set); | |
| 407 | + // Global motion is the identity for intra and is not coded. | |
| 408 | + if seq.film_grain_params_present { | |
| 409 | + w.write_bit(h.film_grain.apply_grain); | |
| 410 | + if h.film_grain.apply_grain { | |
| 411 | + return Err(Error::unsupported( | |
| 412 | + "AV1 film grain", | |
| 413 | + "film grain synthesis parameters are not written", | |
| 414 | + )); | |
| 415 | + } | |
| 416 | + } | |
| 417 | + Ok(()) | |
| 418 | +} | |
| 419 | + | |
| 420 | +/// A `OBU_FRAME`: the header above, byte-aligned, followed by the tile group | |
| 421 | +/// payload the caller supplies. | |
| 422 | +pub fn frame_obu(seq: &SequenceHeader, h: &FrameHeader, tile_data: &[u8]) -> Result<Vec<u8>> { | |
| 423 | + let mut w = BitWriter::new(); | |
| 424 | + write_frame_header(&mut w, seq, h)?; | |
| 425 | + write_byte_alignment(&mut w); | |
| 426 | + let mut payload = w.into_bytes(); | |
| 427 | + payload.extend_from_slice(tile_data); | |
| 428 | + Ok(wrap_obu( | |
| 429 | + &ObuHeader { | |
| 430 | + obu_type: ObuType::Frame, | |
| 431 | + extension_flag: false, | |
| 432 | + has_size_field: true, | |
| 433 | + temporal_id: 0, | |
| 434 | + spatial_id: 0, | |
| 435 | + }, | |
| 436 | + &payload, | |
| 437 | + )) | |
| 438 | +} | |
| 439 | + | |
| 440 | +#[cfg(test)] | |
| 441 | +mod tests { | |
| 442 | + use super::*; | |
| 443 | + use crate::sequence::sequence_header_obu; | |
| 444 | + use crate::sequence::tests::sample_1080p; | |
| 445 | + use ec_av1_syntax::{Av1Parser, ObuKind, QuantizationParams}; | |
| 446 | + | |
| 447 | + /// The key frame the sequence in [`sample_1080p`] would carry: 1920x1080, | |
| 448 | + /// one tile, quantised (so nothing is lossless), CDEF on. | |
| 449 | + fn sample_key_frame() -> FrameHeader { | |
| 450 | + FrameHeader { | |
| 451 | + frame_type: FrameType::Key, | |
| 452 | + frame_is_intra: true, | |
| 453 | + show_frame: true, | |
| 454 | + showable_frame: false, | |
| 455 | + error_resilient_mode: true, | |
| 456 | + force_integer_mv: true, | |
| 457 | + refresh_frame_flags: 0xFF, | |
| 458 | + primary_ref_frame: PRIMARY_REF_NONE, | |
| 459 | + order_hint: 0, | |
| 460 | + frame_width: 1920, | |
| 461 | + frame_height: 1080, | |
| 462 | + upscaled_width: 1920, | |
| 463 | + render_width: 1920, | |
| 464 | + render_height: 1080, | |
| 465 | + mi_cols: 480, | |
| 466 | + mi_rows: 270, | |
| 467 | + tile_info: ec_av1_syntax::TileInfo { | |
| 468 | + uniform_spacing: true, | |
| 469 | + cols: 1, | |
| 470 | + rows: 1, | |
| 471 | + cols_log2: 0, | |
| 472 | + rows_log2: 0, | |
| 473 | + mi_col_starts: vec![0, 480], | |
| 474 | + mi_row_starts: vec![0, 270], | |
| 475 | + context_update_tile_id: 0, | |
| 476 | + tile_size_bytes: 1, | |
| 477 | + }, | |
| 478 | + quantization: QuantizationParams { | |
| 479 | + base_q_idx: 100, | |
| 480 | + ..QuantizationParams::default() | |
| 481 | + }, | |
| 482 | + loop_filter: ec_av1_syntax::LoopFilterParams { | |
| 483 | + level: [12, 12, 0, 0], | |
| 484 | + sharpness: 0, | |
| 485 | + delta_enabled: true, | |
| 486 | + delta_update: false, | |
| 487 | + ref_deltas: DEFAULT_REF_DELTAS, | |
| 488 | + mode_deltas: [0; 2], | |
| 489 | + }, | |
| 490 | + cdef: ec_av1_syntax::CdefParams { | |
| 491 | + damping: 4, | |
| 492 | + bits: 0, | |
| 493 | + y_pri_strength: [3, 0, 0, 0, 0, 0, 0, 0], | |
| 494 | + y_sec_strength: [4, 0, 0, 0, 0, 0, 0, 0], | |
| 495 | + uv_pri_strength: [2, 0, 0, 0, 0, 0, 0, 0], | |
| 496 | + uv_sec_strength: [0; 8], | |
| 497 | + }, | |
| 498 | + tx_mode: TxMode::Select, | |
| 499 | + reduced_tx_set: false, | |
| 500 | + ..FrameHeader::default() | |
| 501 | + } | |
| 502 | + } | |
| 503 | + | |
| 504 | + /// Parse a sequence header OBU followed by a frame OBU, and hand back what | |
| 505 | + /// the frame OBU held. | |
| 506 | + fn roundtrip( | |
| 507 | + seq: &SequenceHeader, | |
| 508 | + h: &FrameHeader, | |
| 509 | + tile_data: &[u8], | |
| 510 | + ) -> (FrameHeader, Vec<ec_av1_syntax::Tile>) { | |
| 511 | + let mut stream = sequence_header_obu(seq).unwrap(); | |
| 512 | + let frame_start = stream.len(); | |
| 513 | + stream.extend_from_slice(&frame_obu(seq, h, tile_data).unwrap()); | |
| 514 | + | |
| 515 | + let mut parser = Av1Parser::new(); | |
| 516 | + let obus = parser.parse_temporal_unit(&stream).unwrap(); | |
| 517 | + assert_eq!(obus.len(), 2); | |
| 518 | + assert_eq!(obus[1].offset, frame_start); | |
| 519 | + assert_eq!(obus[1].offset + obus[1].total_size, stream.len()); | |
| 520 | + match &obus[1].kind { | |
| 521 | + ObuKind::Frame(parsed, tiles) => ((**parsed).clone(), tiles.clone()), | |
| 522 | + other => panic!("expected an OBU_FRAME, got {other:?}"), | |
| 523 | + } | |
| 524 | + } | |
| 525 | + | |
| 526 | + #[test] | |
| 527 | + fn key_frame_header_roundtrips() { | |
| 528 | + let seq = sample_1080p(); | |
| 529 | + let mut expected = sample_key_frame(); | |
| 530 | + let (parsed, tiles) = roundtrip(&seq, &expected, &[0x11, 0x22, 0x33]); | |
| 531 | + | |
| 532 | + // The header's own length is derived, not written; check it against the | |
| 533 | + // writer rather than copying it out of the parse. | |
| 534 | + let mut w = BitWriter::new(); | |
| 535 | + write_frame_header(&mut w, &seq, &expected).unwrap(); | |
| 536 | + assert_eq!(parsed.header_bits, w.bit_len()); | |
| 537 | + expected.header_bits = parsed.header_bits; | |
| 538 | + | |
| 539 | + assert_eq!(parsed, expected); | |
| 540 | + assert_eq!(tiles.len(), 1); | |
| 541 | + assert_eq!(tiles[0].size, 3); | |
| 542 | + } | |
| 543 | + | |
| 544 | + #[test] | |
| 545 | + fn four_tile_columns_roundtrip() { | |
| 546 | + let seq = sample_1080p(); | |
| 547 | + let mut expected = sample_key_frame(); | |
| 548 | + expected.tile_info = ec_av1_syntax::TileInfo { | |
| 549 | + uniform_spacing: true, | |
| 550 | + cols: 4, | |
| 551 | + rows: 1, | |
| 552 | + cols_log2: 2, | |
| 553 | + rows_log2: 0, | |
| 554 | + // 15 superblocks of 128 wide, 4 to a tile: 4, 4, 4, 3. | |
| 555 | + mi_col_starts: vec![0, 128, 256, 384, 480], | |
| 556 | + mi_row_starts: vec![0, 270], | |
| 557 | + context_update_tile_id: 3, | |
| 558 | + tile_size_bytes: 1, | |
| 559 | + }; | |
| 560 | + // tile_start_and_end_present_flag = 0, then three sized tiles of one | |
| 561 | + // byte each (the field codes size - 1) and one unsized last tile. | |
| 562 | + let tile_data = [0x00, 0x00, 0x11, 0x00, 0x22, 0x00, 0x33, 0x44]; | |
| 563 | + let (parsed, tiles) = roundtrip(&seq, &expected, &tile_data); | |
| 564 | + | |
| 565 | + let mut w = BitWriter::new(); | |
| 566 | + write_frame_header(&mut w, &seq, &expected).unwrap(); | |
| 567 | + expected.header_bits = w.bit_len(); | |
| 568 | + assert_eq!(parsed, expected); | |
| 569 | + assert_eq!(tiles.len(), 4); | |
| 570 | + assert!(tiles.iter().all(|t| t.size == 1)); | |
| 571 | + assert_eq!(tiles[3].column, 3); | |
| 572 | + } | |
| 573 | + | |
| 574 | + #[test] | |
| 575 | + fn segmentation_and_delta_q_roundtrip() { | |
| 576 | + let seq = sample_1080p(); | |
| 577 | + let mut expected = sample_key_frame(); | |
| 578 | + expected.segmentation.enabled = true; | |
| 579 | + expected.segmentation.update_map = true; | |
| 580 | + expected.segmentation.update_data = true; | |
| 581 | + expected.segmentation.feature_enabled[0][0] = true; | |
| 582 | + expected.segmentation.feature_data[0][0] = -40; | |
| 583 | + expected.segmentation.feature_enabled[2][5] = true; | |
| 584 | + expected.segmentation.feature_data[2][5] = 3; | |
| 585 | + // Both are derived from the feature table, not coded. | |
| 586 | + expected.segmentation.seg_id_pre_skip = true; | |
| 587 | + expected.segmentation.last_active_seg_id = 2; | |
| 588 | + expected.delta.q_present = true; | |
| 589 | + expected.delta.q_res = 2; | |
| 590 | + expected.delta.lf_present = true; | |
| 591 | + expected.delta.lf_res = 1; | |
| 592 | + expected.delta.lf_multi = true; | |
| 593 | + | |
| 594 | + let (parsed, _) = roundtrip(&seq, &expected, &[0x00]); | |
| 595 | + let mut w = BitWriter::new(); | |
| 596 | + write_frame_header(&mut w, &seq, &expected).unwrap(); | |
| 597 | + expected.header_bits = w.bit_len(); | |
| 598 | + assert_eq!(parsed, expected); | |
| 599 | + } | |
| 600 | + | |
| 601 | + #[test] | |
| 602 | + fn loop_restoration_roundtrips_with_its_own_type_codes() { | |
| 603 | + let mut seq = sample_1080p(); | |
| 604 | + seq.enable_restoration = true; | |
| 605 | + let mut expected = sample_key_frame(); | |
| 606 | + expected.loop_restoration.frame_restoration_type = [ | |
| 607 | + RestorationType::Sgrproj, | |
| 608 | + RestorationType::Wiener, | |
| 609 | + RestorationType::None, | |
| 610 | + ]; | |
| 611 | + expected.loop_restoration.lr_unit_shift = 1; | |
| 612 | + expected.loop_restoration.lr_uv_shift = 1; | |
| 613 | + | |
| 614 | + let (parsed, _) = roundtrip(&seq, &expected, &[0x00]); | |
| 615 | + assert_eq!( | |
| 616 | + parsed.loop_restoration.frame_restoration_type, | |
| 617 | + expected.loop_restoration.frame_restoration_type | |
| 618 | + ); | |
| 619 | + assert_eq!(parsed.loop_restoration.lr_unit_shift, 1); | |
| 620 | + assert_eq!(parsed.loop_restoration.lr_uv_shift, 1); | |
| 621 | + } | |
| 622 | + | |
| 623 | + #[test] | |
| 624 | + fn unsupported_frames_are_refused_by_name() { | |
| 625 | + let seq = sample_1080p(); | |
| 626 | + | |
| 627 | + let mut inter = sample_key_frame(); | |
| 628 | + inter.frame_type = FrameType::Inter; | |
| 629 | + assert!(frame_obu(&seq, &inter, &[0x00]).is_err()); | |
| 630 | + | |
| 631 | + let mut hidden = sample_key_frame(); | |
| 632 | + hidden.show_frame = false; | |
| 633 | + assert!(frame_obu(&seq, &hidden, &[0x00]).is_err()); | |
| 634 | + | |
| 635 | + let mut scaled = sample_key_frame(); | |
| 636 | + scaled.superres_denom = 9; | |
| 637 | + assert!(frame_obu(&seq, &scaled, &[0x00]).is_err()); | |
| 638 | + | |
| 639 | + let mut ragged = sample_key_frame(); | |
| 640 | + ragged.tile_info.uniform_spacing = false; | |
| 641 | + assert!(frame_obu(&seq, &ragged, &[0x00]).is_err()); | |
| 642 | + | |
| 643 | + let mut grainy = seq.clone(); | |
| 644 | + grainy.film_grain_params_present = true; | |
| 645 | + let mut grain_frame = sample_key_frame(); | |
| 646 | + grain_frame.film_grain.apply_grain = true; | |
| 647 | + assert!(frame_obu(&grainy, &grain_frame, &[0x00]).is_err()); | |
| 648 | + } | |
| 649 | +} |
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -13,5 +13,6 @@ | ||
| 13 | 13 | #![warn(missing_docs)] |
| 14 | 14 | |
| 15 | 15 | pub mod bits; |
| 16 | +pub mod frame; | |
| 16 | 17 | pub mod obu; |
| 17 | 18 | pub mod sequence; |
crates/ec-av1/src/sequence.rsmodified+2 -2
| @@ -234,14 +234,14 @@ pub fn sequence_header_obu(s: &SequenceHeader) -> Result<Vec<u8>> { | ||
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | #[cfg(test)] |
| 237 | -mod tests { | |
| 237 | +pub(crate) mod tests { | |
| 238 | 238 | use super::*; |
| 239 | 239 | use ec_av1_syntax::obu::{ObuHeader, read_leb128}; |
| 240 | 240 | use ec_av1_syntax::sequence::SequenceHeader; |
| 241 | 241 | use ec_core::{BitReader, BitWriter}; |
| 242 | 242 | |
| 243 | 243 | /// A realistic 1920x1080 8-bit 4:2:0 main-profile sequence header. |
| 244 | - fn sample_1080p() -> SequenceHeader { | |
| 244 | + pub(crate) fn sample_1080p() -> SequenceHeader { | |
| 245 | 245 | SequenceHeader { |
| 246 | 246 | seq_profile: 0, |
| 247 | 247 | still_picture: false, |