use crate::cabac::CabacEncoder;
use crate::contexts::{Contexts, ctx};
use crate::internal::bpg_bitstream::BitWriter;
use super::types::chroma_qp_from_luma;
#[derive(Clone)]
pub(super) struct AqState {
pub(super) active: bool,
pub(super) qg_log2: u8,
pub(super) qp_bd_offset: i32,
pub(super) slice_qp_y: i32,
pub(super) current_qpy: i32,
pub(super) last_qpy_prev_qg: i32,
pub(super) qpy_pred: i32,
pub(super) first_qp_group: bool,
pub(super) qg_x: i32,
pub(super) qg_y: i32,
pub(super) coded: bool,
pub(super) cu_qp_delta: i32,
pub(super) pred: i32,
pub(super) cu_target: i32,
pub(super) cu_x: u32,
pub(super) cu_y: u32,
pub(super) cu_log2_size: u8,
pub(super) target_qg: (i32, i32),
pub(super) target: i32,
}
impl AqState {
pub(super) fn reset_for_slice(&mut self) {
self.current_qpy = self.slice_qp_y;
self.last_qpy_prev_qg = self.slice_qp_y;
self.qpy_pred = self.slice_qp_y;
self.first_qp_group = true;
self.qg_x = -1;
self.qg_y = -1;
self.coded = false;
self.cu_qp_delta = 0;
}
}
impl<'a> super::Encoder<'a> {
pub(super) fn aq_qg_target(&mut self, x0: u32, y0: u32) -> i32 {
let mask = (1u32 << self.aq.qg_log2) - 1;
let (qx, qy) = ((x0 & !mask) as i32, (y0 & !mask) as i32);
if (qx, qy) != self.aq.target_qg {
let offset = if let Some(map) = &self.aq_offset_map {
map.offset_at(qx as u32, qy as u32)
} else {
use crate::AqMode::*;
use crate::preanalysis as pre;
let analysis = &self.analysis;
let fine = self.fine_aq.as_deref();
let (qx, qy) = (qx as u32, qy as u32);
let strength = self.aq_strength;
let clamp = self.aq_clamp;
let qg_log2 = self.aq.qg_log2;
match self.aq_mode {
Off => 0,
LegacyShrink => {
let imp = analysis.importance_at(qx, qy, qg_log2);
pre::aq_qp_offset(imp)
}
Perceptual => pre::aq_qp_offset_variance(
fine.map_or_else(
|| analysis.variance_at(qx, qy),
|f| f.variance_at(qx, qy),
),
fine.map_or_else(
|| analysis.frame_mean_log2var(),
|f| f.frame_mean_log2var(),
),
strength,
true,
clamp,
),
PsnrProbe => pre::aq_qp_offset_variance(
fine.map_or_else(
|| analysis.variance_at(qx, qy),
|f| f.variance_at(qx, qy),
),
fine.map_or_else(
|| analysis.frame_mean_log2var(),
|f| f.frame_mean_log2var(),
),
strength,
false,
clamp,
),
PerceptualChroma => pre::aq_qp_offset_chroma(
fine.map_or_else(
|| analysis.variance_at(qx, qy),
|f| f.variance_at(qx, qy),
),
fine.map_or_else(
|| analysis.chroma_activity_at(qx, qy),
|f| f.chroma_activity_at(qx, qy),
),
fine.map_or_else(
|| analysis.frame_mean_log2activity(),
|f| f.frame_mean_log2activity(),
),
strength,
clamp,
),
AutoVariance | AutoVarianceBiased => pre::aq_qp_offset_autovar(
fine.map_or_else(
|| analysis.autovar_qp_adj0_at(qx, qy),
|f| f.autovar_qp_adj0_at(qx, qy),
),
fine.map_or_else(|| analysis.autovar_avg_adj(), |f| f.autovar_avg_adj()),
fine.map_or_else(
|| analysis.autovar_avg_adj_final(),
|f| f.autovar_avg_adj_final(),
),
strength,
self.aq_mode == AutoVarianceBiased,
clamp,
),
PositiveProbe => pre::aq_qp_offset_positive(
fine.map_or_else(
|| analysis.variance_at(qx, qy),
|f| f.variance_at(qx, qy),
),
fine.map_or_else(
|| analysis.frame_min_log2var(),
|f| f.frame_min_log2var(),
),
fine.map_or_else(
|| analysis.frame_max_log2var(),
|f| f.frame_max_log2var(),
),
clamp,
),
TwoPassMeasured => 0,
}
};
self.aq.target = (self.aq.slice_qp_y + offset).clamp(0, 51);
self.aq.target_qg = (qx, qy);
}
self.aq.target
}
pub(super) fn aq_set_cu_qp(&mut self, x0: u32, y0: u32) {
let target = self.aq_qg_target(x0, y0);
let off = self.aq.qp_bd_offset;
self.cur_qp_y = target + off;
let qpi_c = (target + crate::chroma_qp_offset() as i32).clamp(-off, 57);
self.cur_qp_c = chroma_qp_from_luma(qpi_c, self.cat) + off;
}
pub(super) fn aq_get_qpy_at(&self, x: u32, y: u32) -> i32 {
let stride = self.frame.deblock_stride;
let idx = ((y / 4) * stride + (x / 4)) as usize;
self.frame
.qp_map
.get(idx)
.map(|&q| q as i32)
.unwrap_or(self.aq.slice_qp_y)
}
pub(super) fn aq_resolve_qpy(&self, pred: i32, delta: i32) -> i32 {
let off = self.aq.qp_bd_offset;
((pred + delta + 52 + 2 * off) % (52 + off)) - off
}
pub(super) fn aq_qg_reset(&mut self) {
self.aq.coded = false;
self.aq.cu_qp_delta = 0;
}
pub(super) fn aq_cu_begin(&mut self, cu_x: u32, cu_y: u32, cu_log2_size: u8) {
self.aq.cu_x = cu_x;
self.aq.cu_y = cu_y;
self.aq.cu_log2_size = cu_log2_size;
let qg_mask = (1u32 << self.aq.qg_log2) - 1;
let x_qg = (cu_x & !qg_mask) as i32;
let y_qg = (cu_y & !qg_mask) as i32;
if x_qg != self.aq.qg_x || y_qg != self.aq.qg_y {
self.aq.last_qpy_prev_qg = self.aq.current_qpy;
self.aq.qg_x = x_qg;
self.aq.qg_y = y_qg;
}
let pred0 = if self.aq.first_qp_group || (x_qg == 0 && y_qg == 0) {
self.aq.slice_qp_y
} else {
self.aq.qpy_pred
};
let ctb_size = 1u32 << super::types::CTB_LOG2;
let ctb_x = cu_x / ctb_size;
let ctb_y = cu_y / ctb_size;
let ctb_mask = ctb_size - 1;
let qp_y_a = if x_qg > 0 && (cu_x & ctb_mask) != 0 && ((x_qg as u32) & ctb_mask) != 0 {
let left_x = (x_qg as u32) - 1;
let left_ctb_x = left_x / ctb_size;
if left_ctb_x == ctb_x {
self.aq_get_qpy_at(left_x, y_qg as u32)
} else {
pred0
}
} else {
pred0
};
let qp_y_b = if y_qg > 0 && (cu_y & ctb_mask) != 0 && ((y_qg as u32) & ctb_mask) != 0 {
let above_y = (y_qg as u32) - 1;
if above_y / ctb_size == ctb_y {
self.aq_get_qpy_at(x_qg as u32, above_y)
} else {
pred0
}
} else {
pred0
};
let pred = (qp_y_a + qp_y_b + 1) >> 1;
self.aq.pred = pred;
self.aq.current_qpy = self.aq_resolve_qpy(pred, self.aq.cu_qp_delta);
self.aq.cu_target = self.aq_qg_target(cu_x, cu_y);
self.frame
.store_block_qp(cu_x, cu_y, 1u32 << cu_log2_size, self.aq.current_qpy as i8);
}
pub(super) fn aq_node_end(&mut self, x0: u32, y0: u32, log2_size: u8) {
let mask = (1u32 << self.aq.qg_log2) - 1;
let size = 1u32 << log2_size;
if ((x0 + size) & mask) == 0 && ((y0 + size) & mask) == 0 {
self.aq.qpy_pred = self.aq.current_qpy;
}
}
pub(super) fn aq_take_cu_qp_delta(&mut self) -> i32 {
let delta = self.aq.cu_target - self.aq.pred;
self.aq.coded = true;
if self.aq.first_qp_group || (self.aq.qg_x == 0 && self.aq.qg_y == 0) {
self.aq.first_qp_group = false;
}
self.aq.cu_qp_delta = delta;
self.aq.current_qpy = self.aq_resolve_qpy(self.aq.pred, delta);
self.frame.store_block_qp(
self.aq.cu_x,
self.aq.cu_y,
1u32 << self.aq.cu_log2_size,
self.aq.current_qpy as i8,
);
delta
}
}
pub(super) fn encode_cu_qp_delta(
enc: &mut CabacEncoder,
w: &mut BitWriter,
ctxs: &mut Contexts,
delta: i32,
) {
let abs = delta.unsigned_abs();
let prefix = abs.min(5);
enc.encode_bin(w, (prefix >= 1) as u8, ctxs.get(ctx::CU_QP_DELTA_ABS));
if prefix >= 1 {
for _ in 1..prefix {
enc.encode_bin(w, 1, ctxs.get(ctx::CU_QP_DELTA_ABS + 1));
}
if prefix < 5 {
enc.encode_bin(w, 0, ctxs.get(ctx::CU_QP_DELTA_ABS + 1));
}
}
if abs >= 5 {
encode_egk0_bypass(enc, w, abs - 5);
}
if abs >= 1 {
enc.encode_bin_ep(w, (delta < 0) as u8);
}
}
pub(super) fn encode_egk0_bypass(enc: &mut CabacEncoder, w: &mut BitWriter, value: u32) {
let mut m = 0u32;
while (1u32 << (m + 1)) - 1 <= value {
m += 1;
}
for _ in 0..m {
enc.encode_bin_ep(w, 1);
}
enc.encode_bin_ep(w, 0);
if m > 0 {
let suffix = value - ((1u32 << m) - 1);
enc.encode_bins_ep(w, suffix, m);
}
}