| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | use std::collections::BTreeMap; |
| 21 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 22 | use std::sync::{Mutex, OnceLock}; |
| 23 | |
| 24 | static ON: AtomicBool = AtomicBool::new(false); |
| 25 | |
| 26 | |
| 27 | #[inline] |
| 28 | pub fn on() -> bool { |
| 29 | static INIT: OnceLock<bool> = OnceLock::new(); |
| 30 | |
| 31 | *INIT.get_or_init(|| { |
| 32 | let armed = crate::envflags::var("EC_AV1_BITCENSUS").ok().as_deref() == Some("1"); |
| 33 | ON.store(armed, Ordering::Relaxed); |
| 34 | armed |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | |
| 39 | #[inline] |
| 40 | pub(crate) fn armed() -> bool { |
| 41 | ON.load(Ordering::Relaxed) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | #[derive(Default, Debug, Clone)] |
| 46 | pub struct Frame { |
| 47 | |
| 48 | pub idx: usize, |
| 49 | |
| 50 | pub kind: &'static str, |
| 51 | |
| 52 | pub qindex: u8, |
| 53 | |
| 54 | pub order_hint: u32, |
| 55 | |
| 56 | |
| 57 | pub ref_hints: [u32; 7], |
| 58 | |
| 59 | pub bytes: usize, |
| 60 | |
| 61 | pub delta_q: bool, |
| 62 | |
| 63 | pub segmentation: bool, |
| 64 | |
| 65 | pub lf: [u8; 2], |
| 66 | |
| 67 | pub cdef: [u8; 3], |
| 68 | |
| 69 | pub lr: [u8; 3], |
| 70 | |
| 71 | pub blocks: BTreeMap<(u16, u16), u64>, |
| 72 | |
| 73 | pub area: [u64; 3], |
| 74 | |
| 75 | |
| 76 | pub refs: [u64; 8], |
| 77 | |
| 78 | pub skip_area: u64, |
| 79 | |
| 80 | pub modes: [u64; 4], |
| 81 | |
| 82 | |
| 83 | pub mv: [u64; 6], |
| 84 | |
| 85 | pub tx: BTreeMap<(u16, u16), u64>, |
| 86 | |
| 87 | |
| 88 | |
| 89 | pub tx_type: BTreeMap<(u8, u8), u64>, |
| 90 | |
| 91 | pub families: BTreeMap<&'static str, (u64, f64)>, |
| 92 | } |
| 93 | |
| 94 | #[derive(Default)] |
| 95 | struct State { |
| 96 | frames: Vec<Frame>, |
| 97 | |
| 98 | |
| 99 | tables: Vec<(usize, usize, &'static str)>, |
| 100 | |
| 101 | last_block_area: u64, |
| 102 | } |
| 103 | |
| 104 | fn state() -> &'static Mutex<State> { |
| 105 | static S: OnceLock<Mutex<State>> = OnceLock::new(); |
| 106 | S.get_or_init(|| Mutex::new(State::default())) |
| 107 | } |
| 108 | |
| 109 | fn with<R>(f: impl FnOnce(&mut State) -> R) -> R { |
| 110 | f(&mut state().lock().unwrap_or_else(std::sync::PoisonError::into_inner)) |
| 111 | } |
| 112 | |
| 113 | fn cur(s: &mut State) -> &mut Frame { |
| 114 | if s.frames.is_empty() { |
| 115 | s.frames.push(Frame::default()); |
| 116 | } |
| 117 | s.frames.last_mut().expect("pushed above") |
| 118 | } |
| 119 | |
| 120 | |
| 121 | pub fn take() -> Vec<Frame> { |
| 122 | with(|s| std::mem::take(&mut s.frames)) |
| 123 | } |
| 124 | |
| 125 | |
| 126 | pub(crate) fn frame_start(header: &ec_av1_syntax::FrameHeader, bytes: usize, idx: usize) { |
| 127 | if !armed() { |
| 128 | return; |
| 129 | } |
| 130 | let kind = match (header.frame_is_intra, header.show_frame) { |
| 131 | (true, _) => "key", |
| 132 | (false, false) => "arf", |
| 133 | (false, true) => "leaf", |
| 134 | }; |
| 135 | with(|s| { |
| 136 | s.frames.push(Frame { |
| 137 | idx, |
| 138 | kind, |
| 139 | qindex: header.quantization.base_q_idx, |
| 140 | order_hint: header.order_hint, |
| 141 | ref_hints: header.order_hints, |
| 142 | bytes, |
| 143 | delta_q: header.delta.q_present, |
| 144 | segmentation: header.segmentation.enabled, |
| 145 | lf: [header.loop_filter.level[0], header.loop_filter.level[1]], |
| 146 | cdef: [ |
| 147 | header.cdef.bits, |
| 148 | header.cdef.y_pri_strength[0], |
| 149 | header.cdef.uv_pri_strength[0], |
| 150 | ], |
| 151 | lr: [ |
| 152 | header.loop_restoration.frame_restoration_type[0] as u8, |
| 153 | header.loop_restoration.frame_restoration_type[1] as u8, |
| 154 | header.loop_restoration.frame_restoration_type[2] as u8, |
| 155 | ], |
| 156 | ..Frame::default() |
| 157 | }); |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | pub(crate) fn register_cdfs(c: &crate::cdf_state::Cdfs) { |
| 165 | if !armed() { |
| 166 | return; |
| 167 | } |
| 168 | macro_rules! ranges { |
| 169 | ($($name:ident),* $(,)?) => { |
| 170 | vec![$({ |
| 171 | let start = std::ptr::addr_of!(c.$name) as usize; |
| 172 | (start, start + std::mem::size_of_val(&c.$name), stringify!($name)) |
| 173 | }),*] |
| 174 | }; |
| 175 | } |
| 176 | let mut tables: Vec<(usize, usize, &'static str)> = ranges![ |
| 177 | partition_w128, partition_w64, partition_w32, txfm_partition, skip, kf_y_mode, |
| 178 | uv_mode_no_cfl, uv_mode_cfl, cfl_sign, cfl_alpha, angle_delta, txb_skip_luma_16, |
| 179 | txb_skip_luma_8, txb_skip_luma_4, txb_skip_chroma_8, txb_skip_chroma_4, eob_pt_256_luma, eob_pt_64_luma, |
| 180 | eob_pt_64_chroma, eob_pt_16_chroma, eob_pt_16_luma, eob_pt_16_luma_class1, eob_pt_64_luma_class1, eob_pt_16_chroma_class1, |
| 181 | eob_pt_64_chroma_class1, eob_pt_128_chroma_class1, eob_pt_128_luma_class1, eob_pt_32_chroma_class1, eob_pt_256_luma_class1, eob_pt_256_chroma_class1, |
| 182 | eob_pt_512_luma_class1, eob_pt_512_chroma_class1, eob_pt_1024_luma_class1, eob_pt_1024_chroma_class1, eob_pt_32_luma, eob_pt_32_luma_class1, |
| 183 | eob_extra_luma_16, eob_extra_luma_8, eob_extra_luma_4, eob_extra_chroma_8, eob_extra_chroma_4, base_luma_16, |
| 184 | base_luma_8, base_luma_4, base_chroma_8, base_chroma_4, base_eob_luma_16, base_eob_luma_8, |
| 185 | base_eob_luma_4, base_eob_chroma_8, base_eob_chroma_4, br_luma_16, br_luma_8, br_luma_4, |
| 186 | br_chroma_8, br_chroma_4, partition_w16, partition_w8, txb_skip_luma_32, txb_skip_luma_64, |
| 187 | txb_skip_chroma_16, txb_skip_chroma_32, eob_pt_1024_luma, eob_pt_1024_chroma, eob_pt_256_chroma, eob_pt_512_luma, |
| 188 | eob_pt_128_chroma, eob_pt_512_chroma, eob_pt_128_luma, eob_pt_32_chroma, eob_extra_luma_32, eob_extra_luma_64, |
| 189 | eob_extra_chroma_16, eob_extra_chroma_32, base_luma_32, base_luma_64, base_chroma_16, base_chroma_32, |
| 190 | base_eob_luma_32, base_eob_luma_64, base_eob_chroma_16, base_eob_chroma_32, br_luma_32, br_chroma_16, |
| 191 | br_chroma_32, dc_sign_luma, intra_tx_type_16, intra_tx_type_8, intra_tx_type_8_set1, intra_tx_type_4, |
| 192 | intra_tx_type_4_set1, inter_tx_type_32, inter_tx_type_16, inter_tx_type_8, inter_tx_type_16_set2, inter_tx_type_8_set1, |
| 193 | inter_tx_type_4, inter_tx_type_4_set1, dc_sign_chroma, intra_inter, single_ref, comp_mode, |
| 194 | skip_mode, obmc, motion_mode, interintra, interintra_mode, wedge_interintra, |
| 195 | compound_type, wedge_idx, inter_compound_mode, comp_ref_type, uni_comp_ref, comp_ref, |
| 196 | comp_bwdref, comp_group_idx, compound_idx, y_mode, new_mv, zero_mv, |
| 197 | ref_mv, drl_mode, mv_joint, mv_comp, dv_joint, dv_comp, |
| 198 | filter_intra, filter_intra_mode, tx_size_cat0, tx_size_cat1, tx_size_cat2, tx_size_cat3, |
| 199 | switchable_interp, palette_y_mode, palette_y_size, palette_uv_mode, palette_uv_size, palette_y_color_index, |
| 200 | palette_uv_color_index, intrabc, restore_wiener, restore_sgrproj, restore_switchable, segment_id, |
| 201 | segment_pred, delta_q, delta_lf, delta_lf_multi |
| 202 | ]; |
| 203 | tables.sort_unstable(); |
| 204 | with(|s| s.tables = tables); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | pub(crate) fn charge(addr: usize, bits: f64) { |
| 209 | with(|s| { |
| 210 | let name = match s.tables.partition_point(|t| t.0 <= addr) { |
| 211 | 0 => "unregistered", |
| 212 | i if s.tables[i - 1].1 > addr => s.tables[i - 1].2, |
| 213 | _ => "unregistered", |
| 214 | }; |
| 215 | let e = cur(s).families.entry(name).or_default(); |
| 216 | e.0 += 1; |
| 217 | e.1 += bits; |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | pub(crate) fn charge_literal(bits: u32) { |
| 223 | with(|s| { |
| 224 | let e = cur(s).families.entry("literal").or_default(); |
| 225 | e.0 += u64::from(bits); |
| 226 | e.1 += f64::from(bits); |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | pub(crate) fn block(w_mi: usize, h_mi: usize, is_inter: bool, ref_frame: i8, skip: bool) { |
| 232 | if !armed() { |
| 233 | return; |
| 234 | } |
| 235 | let area = (w_mi * h_mi) as u64; |
| 236 | with(|s| { |
| 237 | s.last_block_area = area; |
| 238 | let f = cur(s); |
| 239 | *f.blocks.entry(((w_mi * 4) as u16, (h_mi * 4) as u16)).or_default() += 1; |
| 240 | f.area[usize::from(is_inter)] += area; |
| 241 | f.refs[(ref_frame.max(0) as usize).min(7)] += area; |
| 242 | if skip { |
| 243 | f.skip_area += area; |
| 244 | } |
| 245 | }); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | pub(crate) fn compound() { |
| 251 | if !armed() { |
| 252 | return; |
| 253 | } |
| 254 | with(|s| { |
| 255 | let area = s.last_block_area; |
| 256 | let f = cur(s); |
| 257 | f.area[1] = f.area[1].saturating_sub(area); |
| 258 | f.area[2] += area; |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | pub(crate) fn tx(w_px: u8, h_px: u8) { |
| 264 | if !armed() { |
| 265 | return; |
| 266 | } |
| 267 | with(|s| *cur(s).tx.entry((u16::from(w_px), u16::from(h_px))).or_default() += 1); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | pub(crate) fn mv(x: i32, y: i32) { |
| 272 | if !armed() { |
| 273 | return; |
| 274 | } |
| 275 | let m = x.abs().max(y.abs()); |
| 276 | let bucket = match m { |
| 277 | 0 => 0, |
| 278 | 1..=8 => 1, |
| 279 | 9..=32 => 2, |
| 280 | 33..=128 => 3, |
| 281 | 129..=512 => 4, |
| 282 | _ => 5, |
| 283 | }; |
| 284 | with(|s| cur(s).mv[bucket] += 1); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | pub(crate) fn mode(which: usize) { |
| 290 | if !armed() { |
| 291 | return; |
| 292 | } |
| 293 | with(|s| cur(s).modes[which] += 1); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | pub(crate) fn tx_type(len: usize, symbol: usize) { |
| 298 | if !armed() { |
| 299 | return; |
| 300 | } |
| 301 | with(|s| *cur(s).tx_type.entry((len as u8, symbol as u8)).or_default() += 1); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | pub(crate) fn block_intra_frame(w_mi: usize, h_mi: usize, skip: bool) { |
| 308 | if !armed() { |
| 309 | return; |
| 310 | } |
| 311 | if with(|s| cur(s).kind == "key") { |
| 312 | block(w_mi, h_mi, false, 0, skip); |
| 313 | } |
| 314 | } |