| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use crate::header::FrameContext; |
| 12 | use crate::tables::*; |
| 13 | |
| 14 | |
| 15 | |
| 16 | pub(crate) const COEF_COUNT_SAT: u32 = 24; |
| 17 | pub(crate) const COEF_MAX_UPDATE_FACTOR: u32 = 112; |
| 18 | pub(crate) const COEF_MAX_UPDATE_FACTOR_AFTER_KEY: u32 = 128; |
| 19 | |
| 20 | const MODE_MV_COUNT_SAT: u32 = 20; |
| 21 | const MODE_MV_MAX_UPDATE_FACTOR: u32 = 128; |
| 22 | |
| 23 | |
| 24 | #[derive(Clone, Default)] |
| 25 | pub(crate) struct Counts { |
| 26 | |
| 27 | |
| 28 | pub coef: [[[[[[u32; 4]; 6]; 6]; 2]; 2]; 4], |
| 29 | |
| 30 | pub eob_branch: [[[[[u32; 6]; 6]; 2]; 2]; 4], |
| 31 | pub partition: [[u32; 4]; 16], |
| 32 | pub y_mode: [[u32; 10]; 4], |
| 33 | pub uv_mode: [[u32; 10]; 10], |
| 34 | pub skip: [[u32; 2]; 3], |
| 35 | pub tx8: [[u32; 2]; 2], |
| 36 | pub tx16: [[u32; 3]; 2], |
| 37 | pub tx32: [[u32; 4]; 2], |
| 38 | |
| 39 | pub inter_mode: [[u32; 4]; 7], |
| 40 | pub intra_inter: [[u32; 2]; 4], |
| 41 | pub comp_inter: [[u32; 2]; 5], |
| 42 | pub comp_ref: [[u32; 2]; 5], |
| 43 | pub single_ref: [[[u32; 2]; 2]; 5], |
| 44 | pub switchable_interp: [[u32; 3]; 4], |
| 45 | pub mv_joints: [u32; 4], |
| 46 | pub mv: [MvCounts; 2], |
| 47 | } |
| 48 | |
| 49 | |
| 50 | #[derive(Clone, Copy, Default)] |
| 51 | pub(crate) struct MvCounts { |
| 52 | pub sign: [u32; 2], |
| 53 | pub classes: [u32; 11], |
| 54 | pub class0: [u32; 2], |
| 55 | pub bits: [[u32; 2]; 10], |
| 56 | pub class0_fp: [[u32; 4]; 2], |
| 57 | pub fp: [u32; 4], |
| 58 | pub class0_hp: [u32; 2], |
| 59 | pub hp: [u32; 2], |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | pub(crate) fn inc_mv(counts: &mut Counts, diff: (i32, i32)) { |
| 66 | let vert = diff.0 != 0; |
| 67 | let horz = diff.1 != 0; |
| 68 | let joint = (usize::from(vert) << 1) | usize::from(horz); |
| 69 | counts.mv_joints[joint] += 1; |
| 70 | if vert { |
| 71 | inc_mv_component(&mut counts.mv[0], diff.0); |
| 72 | } |
| 73 | if horz { |
| 74 | inc_mv_component(&mut counts.mv[1], diff.1); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fn inc_mv_component(c: &mut MvCounts, v: i32) { |
| 79 | let s = usize::from(v < 0); |
| 80 | c.sign[s] += 1; |
| 81 | let z = (if v < 0 { -v } else { v }) as u32 - 1; |
| 82 | |
| 83 | |
| 84 | let class = if z < 16 { |
| 85 | 0 |
| 86 | } else { |
| 87 | |
| 88 | let mut c = 1usize; |
| 89 | while c < 10 && z >= (2u32 << (c + 3)) { |
| 90 | c += 1; |
| 91 | } |
| 92 | c |
| 93 | }; |
| 94 | let base = if class == 0 { 0 } else { 2u32 << (class + 2) }; |
| 95 | let o = z - base; |
| 96 | let d = (o >> 3) as usize; |
| 97 | let f = ((o >> 1) & 3) as usize; |
| 98 | let e = (o & 1) as usize; |
| 99 | c.classes[class] += 1; |
| 100 | if class == 0 { |
| 101 | c.class0[d] += 1; |
| 102 | c.class0_fp[d][f] += 1; |
| 103 | c.class0_hp[e] += 1; |
| 104 | } else { |
| 105 | for i in 0..class { |
| 106 | c.bits[i][(d >> i) & 1] += 1; |
| 107 | } |
| 108 | c.fp[f] += 1; |
| 109 | c.hp[e] += 1; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | fn get_prob(num: u32, den: u32) -> u8 { |
| 115 | let p = (u64::from(num) * 256 + u64::from(den >> 1)) / u64::from(den); |
| 116 | p.clamp(1, 255) as u8 |
| 117 | } |
| 118 | |
| 119 | |
| 120 | fn weighted_prob(pre: u8, prob: u8, factor: u32) -> u8 { |
| 121 | let v = u32::from(pre) * (256 - factor) + u32::from(prob) * factor; |
| 122 | ((v + 128) >> 8) as u8 |
| 123 | } |
| 124 | |
| 125 | |
| 126 | fn merge_probs(pre: u8, ct: [u32; 2], count_sat: u32, max_update_factor: u32) -> u8 { |
| 127 | let den = ct[0] + ct[1]; |
| 128 | let prob = if den == 0 { 128 } else { get_prob(ct[0], den) }; |
| 129 | let count = den.min(count_sat); |
| 130 | let factor = max_update_factor * count / count_sat; |
| 131 | weighted_prob(pre, prob, factor) |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | fn mode_mv_merge_probs(pre: u8, ct: [u32; 2]) -> u8 { |
| 137 | let den = ct[0] + ct[1]; |
| 138 | if den == 0 { |
| 139 | return pre; |
| 140 | } |
| 141 | let count = den.min(MODE_MV_COUNT_SAT); |
| 142 | let factor = MODE_MV_MAX_UPDATE_FACTOR * count / MODE_MV_COUNT_SAT; |
| 143 | weighted_prob(pre, get_prob(ct[0], den), factor) |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | fn tree_merge(tree: &[i8], i: usize, pre: &[u8], counts: &[u32], out: &mut [u8]) -> u32 { |
| 149 | let side = |child: i8, out: &mut [u8]| -> u32 { |
| 150 | if child <= 0 { |
| 151 | counts[(-child) as usize] |
| 152 | } else { |
| 153 | tree_merge(tree, child as usize, pre, counts, out) |
| 154 | } |
| 155 | }; |
| 156 | let left = side(tree[i], out); |
| 157 | let right = side(tree[i + 1], out); |
| 158 | out[i >> 1] = mode_mv_merge_probs(pre[i >> 1], [left, right]); |
| 159 | left + right |
| 160 | } |
| 161 | |
| 162 | fn merge_tree(tree: &[i8], pre: &[u8], counts: &[u32], out: &mut [u8]) { |
| 163 | tree_merge(tree, 0, pre, counts, out); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | pub(crate) fn adapt_coef_probs( |
| 169 | cur: &mut FrameContext, |
| 170 | pre: &FrameContext, |
| 171 | counts: &Counts, |
| 172 | update_factor: u32, |
| 173 | ) { |
| 174 | for t in 0..4 { |
| 175 | for i in 0..2 { |
| 176 | for j in 0..2 { |
| 177 | for k in 0..6 { |
| 178 | |
| 179 | let nctx = if k == 0 { 3 } else { 6 }; |
| 180 | for l in 0..nctx { |
| 181 | let c = counts.coef[t][i][j][k][l]; |
| 182 | let n0 = c[0]; |
| 183 | let n1 = c[1]; |
| 184 | let n2 = c[2]; |
| 185 | let neob = c[3]; |
| 186 | let branch = counts.eob_branch[t][i][j][k][l]; |
| 187 | let ct = [ |
| 188 | [neob, branch.saturating_sub(neob)], |
| 189 | [n0, n1 + n2], |
| 190 | [n1, n2], |
| 191 | ]; |
| 192 | for m in 0..3 { |
| 193 | cur.coef[t][i][j][k][l][m] = merge_probs( |
| 194 | pre.coef[t][i][j][k][l][m], |
| 195 | ct[m], |
| 196 | COEF_COUNT_SAT, |
| 197 | update_factor, |
| 198 | ); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | |
| 208 | |
| 209 | pub(crate) fn adapt_mode_probs( |
| 210 | cur: &mut FrameContext, |
| 211 | pre: &FrameContext, |
| 212 | counts: &Counts, |
| 213 | tx_select: bool, |
| 214 | switchable: bool, |
| 215 | ) { |
| 216 | for i in 0..4 { |
| 217 | cur.intra_inter[i] = mode_mv_merge_probs(pre.intra_inter[i], counts.intra_inter[i]); |
| 218 | } |
| 219 | for i in 0..5 { |
| 220 | cur.comp_inter[i] = mode_mv_merge_probs(pre.comp_inter[i], counts.comp_inter[i]); |
| 221 | cur.comp_ref[i] = mode_mv_merge_probs(pre.comp_ref[i], counts.comp_ref[i]); |
| 222 | for j in 0..2 { |
| 223 | cur.single_ref[i][j] = |
| 224 | mode_mv_merge_probs(pre.single_ref[i][j], counts.single_ref[i][j]); |
| 225 | } |
| 226 | } |
| 227 | for i in 0..7 { |
| 228 | merge_tree( |
| 229 | &INTER_MODE_TREE, |
| 230 | &pre.inter_mode[i], |
| 231 | &counts.inter_mode[i], |
| 232 | &mut cur.inter_mode[i], |
| 233 | ); |
| 234 | } |
| 235 | for i in 0..4 { |
| 236 | merge_tree( |
| 237 | &INTRA_MODE_TREE, |
| 238 | &pre.y_mode[i], |
| 239 | &counts.y_mode[i], |
| 240 | &mut cur.y_mode[i], |
| 241 | ); |
| 242 | } |
| 243 | for i in 0..10 { |
| 244 | merge_tree( |
| 245 | &INTRA_MODE_TREE, |
| 246 | &pre.uv_mode[i], |
| 247 | &counts.uv_mode[i], |
| 248 | &mut cur.uv_mode[i], |
| 249 | ); |
| 250 | } |
| 251 | for i in 0..16 { |
| 252 | merge_tree( |
| 253 | &PARTITION_TREE, |
| 254 | &pre.partition[i], |
| 255 | &counts.partition[i], |
| 256 | &mut cur.partition[i], |
| 257 | ); |
| 258 | } |
| 259 | if switchable { |
| 260 | for i in 0..4 { |
| 261 | merge_tree( |
| 262 | &SWITCHABLE_INTERP_TREE, |
| 263 | &pre.switchable_interp[i], |
| 264 | &counts.switchable_interp[i], |
| 265 | &mut cur.switchable_interp[i], |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 | if tx_select { |
| 270 | for i in 0..2 { |
| 271 | let c = counts.tx8[i]; |
| 272 | cur.tx_p8x8[i][0] = mode_mv_merge_probs(pre.tx_p8x8[i][0], [c[0], c[1]]); |
| 273 | |
| 274 | let c = counts.tx16[i]; |
| 275 | cur.tx_p16x16[i][0] = mode_mv_merge_probs(pre.tx_p16x16[i][0], [c[0], c[1] + c[2]]); |
| 276 | cur.tx_p16x16[i][1] = mode_mv_merge_probs(pre.tx_p16x16[i][1], [c[1], c[2]]); |
| 277 | |
| 278 | let c = counts.tx32[i]; |
| 279 | cur.tx_p32x32[i][0] = |
| 280 | mode_mv_merge_probs(pre.tx_p32x32[i][0], [c[0], c[1] + c[2] + c[3]]); |
| 281 | cur.tx_p32x32[i][1] = mode_mv_merge_probs(pre.tx_p32x32[i][1], [c[1], c[2] + c[3]]); |
| 282 | cur.tx_p32x32[i][2] = mode_mv_merge_probs(pre.tx_p32x32[i][2], [c[2], c[3]]); |
| 283 | } |
| 284 | } |
| 285 | for i in 0..3 { |
| 286 | cur.skip[i] = mode_mv_merge_probs(pre.skip[i], counts.skip[i]); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | pub(crate) fn adapt_mv_probs( |
| 292 | cur: &mut FrameContext, |
| 293 | pre: &FrameContext, |
| 294 | counts: &Counts, |
| 295 | allow_hp: bool, |
| 296 | ) { |
| 297 | merge_tree( |
| 298 | &MV_JOINT_TREE, |
| 299 | &pre.nmv_joints, |
| 300 | &counts.mv_joints, |
| 301 | &mut cur.nmv_joints, |
| 302 | ); |
| 303 | for comp in 0..2 { |
| 304 | let c = &counts.mv[comp]; |
| 305 | cur.nmv_sign[comp] = mode_mv_merge_probs(pre.nmv_sign[comp], c.sign); |
| 306 | merge_tree( |
| 307 | &MV_CLASS_TREE, |
| 308 | &pre.nmv_classes[comp], |
| 309 | &c.classes, |
| 310 | &mut cur.nmv_classes[comp], |
| 311 | ); |
| 312 | cur.nmv_class0[comp][0] = mode_mv_merge_probs(pre.nmv_class0[comp][0], c.class0); |
| 313 | for j in 0..10 { |
| 314 | cur.nmv_bits[comp][j] = mode_mv_merge_probs(pre.nmv_bits[comp][j], c.bits[j]); |
| 315 | } |
| 316 | for j in 0..2 { |
| 317 | merge_tree( |
| 318 | &MV_FP_TREE, |
| 319 | &pre.nmv_class0_fp[comp][j], |
| 320 | &c.class0_fp[j], |
| 321 | &mut cur.nmv_class0_fp[comp][j], |
| 322 | ); |
| 323 | } |
| 324 | merge_tree(&MV_FP_TREE, &pre.nmv_fp[comp], &c.fp, &mut cur.nmv_fp[comp]); |
| 325 | if allow_hp { |
| 326 | cur.nmv_class0_hp[comp] = |
| 327 | mode_mv_merge_probs(pre.nmv_class0_hp[comp], c.class0_hp); |
| 328 | cur.nmv_hp[comp] = mode_mv_merge_probs(pre.nmv_hp[comp], c.hp); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | impl Counts { |
| 334 | |
| 335 | pub(crate) fn add(&mut self, other: &Counts) { |
| 336 | for (d, s) in self.coef.iter_mut().flatten().flatten().flatten().flatten().flatten().zip( |
| 337 | other.coef.iter().flatten().flatten().flatten().flatten().flatten(), |
| 338 | ) { |
| 339 | *d += s; |
| 340 | } |
| 341 | for (d, s) in self.eob_branch.iter_mut().flatten().flatten().flatten().flatten().zip( |
| 342 | other.eob_branch.iter().flatten().flatten().flatten().flatten(), |
| 343 | ) { |
| 344 | *d += s; |
| 345 | } |
| 346 | add_rows(&mut self.partition, &other.partition); |
| 347 | add_rows(&mut self.y_mode, &other.y_mode); |
| 348 | add_rows(&mut self.uv_mode, &other.uv_mode); |
| 349 | add_rows(&mut self.skip, &other.skip); |
| 350 | add_rows(&mut self.tx8, &other.tx8); |
| 351 | add_rows(&mut self.tx16, &other.tx16); |
| 352 | add_rows(&mut self.tx32, &other.tx32); |
| 353 | add_rows(&mut self.inter_mode, &other.inter_mode); |
| 354 | add_rows(&mut self.intra_inter, &other.intra_inter); |
| 355 | add_rows(&mut self.comp_inter, &other.comp_inter); |
| 356 | add_rows(&mut self.comp_ref, &other.comp_ref); |
| 357 | for (d, s) in self.single_ref.iter_mut().flatten().flatten().zip(other.single_ref.iter().flatten().flatten()) { |
| 358 | *d += s; |
| 359 | } |
| 360 | add_rows(&mut self.switchable_interp, &other.switchable_interp); |
| 361 | for (d, s) in self.mv_joints.iter_mut().zip(&other.mv_joints) { |
| 362 | *d += s; |
| 363 | } |
| 364 | for (d, s) in self.mv.iter_mut().zip(&other.mv) { |
| 365 | d.add(s); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
| 371 | fn add_rows<const M: usize, const N: usize>(dst: &mut [[u32; N]; M], src: &[[u32; N]; M]) { |
| 372 | for (d, s) in dst.iter_mut().flatten().zip(src.iter().flatten()) { |
| 373 | *d += s; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | impl MvCounts { |
| 378 | fn add(&mut self, o: &MvCounts) { |
| 379 | for (d, s) in self.sign.iter_mut().zip(&o.sign) { |
| 380 | *d += s; |
| 381 | } |
| 382 | for (d, s) in self.classes.iter_mut().zip(&o.classes) { |
| 383 | *d += s; |
| 384 | } |
| 385 | for (d, s) in self.class0.iter_mut().zip(&o.class0) { |
| 386 | *d += s; |
| 387 | } |
| 388 | for (d, s) in self.bits.iter_mut().flatten().zip(o.bits.iter().flatten()) { |
| 389 | *d += s; |
| 390 | } |
| 391 | for (d, s) in self.class0_fp.iter_mut().flatten().zip(o.class0_fp.iter().flatten()) { |
| 392 | *d += s; |
| 393 | } |
| 394 | for (d, s) in self.fp.iter_mut().zip(&o.fp) { |
| 395 | *d += s; |
| 396 | } |
| 397 | for (d, s) in self.class0_hp.iter_mut().zip(&o.class0_hp) { |
| 398 | *d += s; |
| 399 | } |
| 400 | for (d, s) in self.hp.iter_mut().zip(&o.hp) { |
| 401 | *d += s; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |