Skip to content

Commit

Permalink
Scale chroma distortion.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdaede committed Oct 11, 2019
1 parent 1c01f90 commit 26f2656
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
7 changes: 6 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ pub struct FrameInvariants<T: Pixel> {
pub ac_delta_q: [i8; 3],
pub lambda: f64,
pub me_lambda: f64,
pub dist_scale: [f64; 3],
pub me_range_scale: u8,
pub use_tx_domain_distortion: bool,
pub use_tx_domain_rate: bool,
Expand Down Expand Up @@ -684,6 +685,7 @@ impl<T: Pixel> FrameInvariants<T> {
dc_delta_q: [0; 3],
ac_delta_q: [0; 3],
lambda: 0.0,
dist_scale: [1.0; 3],
me_lambda: 0.0,
me_range_scale: 1,
use_tx_domain_distortion,
Expand Down Expand Up @@ -866,6 +868,7 @@ impl<T: Pixel> FrameInvariants<T> {
self.lambda =
qps.lambda * ((1 << (2 * (self.sequence.bit_depth - 8))) as f64);
self.me_lambda = self.lambda.sqrt();
self.dist_scale = qps.dist_scale;

let q = bexp64(qps.log_target_q as i64 + q57(QSCALE)) as f32;
/* These coefficients were trained on libaom. */
Expand Down Expand Up @@ -1232,7 +1235,9 @@ pub fn encode_tx_block<T: Pixel>(
let estimated_rate = estimate_rate(fi.base_q_idx, tx_size, tx_dist as u64);
w.add_bits_frac(estimated_rate as u32);
}
(has_coeff, tx_dist)
let bias =
compute_distortion_bias(fi, ts.to_frame_block_offset(tile_bo), bsize);
(has_coeff, (tx_dist as f64 * bias * fi.dist_scale[p]) as i64)
}

pub fn motion_compensate<T: Pixel>(
Expand Down
17 changes: 13 additions & 4 deletions src/rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ pub struct QuantizerParameters {
pub dc_qi: [u8; 3],
pub ac_qi: [u8; 3],
pub lambda: f64,
pub dist_scale: [f64; 3],
}

const Q57_SQUARE_EXP_SCALE: f64 =
Expand All @@ -573,8 +574,16 @@ impl QuantizerParameters {
let scale = q57(QSCALE + bit_depth as i32 - 8);
let quantizer = bexp64(log_target_q + scale);
let (offset_u, offset_v) = chroma_offset(log_target_q);
let quantizer_u = bexp64(log_target_q + offset_u + scale);
let quantizer_v = bexp64(log_target_q + offset_v + scale);
let log_target_q_u = log_target_q + offset_u;
let log_target_q_v = log_target_q + offset_v;
let quantizer_u = bexp64(log_target_q_u + scale);
let quantizer_v = bexp64(log_target_q_v + scale);
let lambda = (::std::f64::consts::LN_2 / 6.0)
* ((log_target_q as f64) * Q57_SQUARE_EXP_SCALE).exp();
let lambda_u = (::std::f64::consts::LN_2 / 6.0)
* ((log_target_q_u as f64) * Q57_SQUARE_EXP_SCALE).exp();
let lambda_v = (::std::f64::consts::LN_2 / 6.0)
* ((log_target_q_v as f64) * Q57_SQUARE_EXP_SCALE).exp();
QuantizerParameters {
log_base_q,
log_target_q,
Expand All @@ -589,8 +598,8 @@ impl QuantizerParameters {
select_ac_qi(quantizer_u, bit_depth).max(1),
select_ac_qi(quantizer_v, bit_depth).max(1),
],
lambda: (::std::f64::consts::LN_2 / 6.0)
* ((log_target_q as f64) * Q57_SQUARE_EXP_SCALE).exp(),
lambda,
dist_scale: [1.0, lambda / lambda_u, lambda / lambda_v],
}
}
}
Expand Down
41 changes: 21 additions & 20 deletions src/rdo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ fn compute_distortion<T: Pixel>(
),
};

distortion = (fi.dist_scale[0] * distortion as f64) as u64;

if !luma_only {
let PlaneConfig { xdec, ydec, .. } = ts.input.planes[1].cfg;

Expand All @@ -371,7 +373,7 @@ fn compute_distortion<T: Pixel>(
for p in 1..3 {
let input_region = ts.input_tile.planes[p].subregion(area);
let rec_region = ts.rec.planes[p].subregion(area);
distortion += sse_wxh(
distortion += (sse_wxh(
&input_region,
&rec_region,
w_uv,
Expand All @@ -383,7 +385,8 @@ fn compute_distortion<T: Pixel>(
bsize,
)
},
);
) as f64
* fi.dist_scale[p]) as u64;
}
};
}
Expand All @@ -401,7 +404,7 @@ fn compute_tx_distortion<T: Pixel>(
let input_region = ts.input_tile.planes[0].subregion(area);
let rec_region = ts.rec.planes[0].subregion(area);
let mut distortion = if skip {
sse_wxh(
(sse_wxh(
&input_region,
&rec_region,
bsize.width(),
Expand All @@ -413,12 +416,11 @@ fn compute_tx_distortion<T: Pixel>(
bsize,
)
},
)
) as f64
* fi.dist_scale[0]) as u64
} else {
assert!(tx_dist >= 0);
let bias =
compute_distortion_bias(fi, ts.to_frame_block_offset(tile_bo), bsize);
(tx_dist as f64 * bias) as u64
tx_dist as u64
};

if !luma_only && skip {
Expand All @@ -438,7 +440,7 @@ fn compute_tx_distortion<T: Pixel>(
for p in 1..3 {
let input_region = ts.input_tile.planes[p].subregion(area);
let rec_region = ts.rec.planes[p].subregion(area);
distortion += sse_wxh(
distortion += (sse_wxh(
&input_region,
&rec_region,
w_uv,
Expand All @@ -450,7 +452,8 @@ fn compute_tx_distortion<T: Pixel>(
bsize,
)
},
);
) as f64
* fi.dist_scale[p]) as u64;
}
}
}
Expand All @@ -475,7 +478,7 @@ fn compute_mean_importance<T: Pixel>(
total_importance / (bsize.width_mi() * bsize.height_mi()) as f32
}

fn compute_distortion_bias<T: Pixel>(
pub fn compute_distortion_bias<T: Pixel>(
fi: &FrameInvariants<T>, frame_bo: PlaneBlockOffset, bsize: BlockSize,
) -> f64 {
let mean_importance = compute_mean_importance(fi, frame_bo, bsize);
Expand Down Expand Up @@ -1650,24 +1653,22 @@ fn rdo_loop_plane_error<T: Pixel>(
let test_region =
test_plane.region(Area::BlockStartingAt { bo: test_bo.0 });

let value = if pli == 0 {
cdef_dist_wxh_8x8(&in_region, &test_region, fi.sequence.bit_depth)
} else {
// The closure returns 1. because we bias the distortion right
// below.
sse_wxh(&in_region, &test_region, 8 >> xdec, 8 >> ydec, |_, _| 1.)
};

let bias = compute_distortion_bias(
fi,
ts.to_frame_block_offset(bo),
BlockSize::BLOCK_8X8,
);
err += (value as f64 * bias) as u64;
err += if pli == 0 {
(cdef_dist_wxh_8x8(&in_region, &test_region, fi.sequence.bit_depth)
as f64
* bias) as u64
} else {
sse_wxh(&in_region, &test_region, 8 >> xdec, 8 >> ydec, |_, _| bias)
};
}
}
}
err
(err as f64 * fi.dist_scale[pli]) as u64
}

// Passed in a superblock offset representing the upper left corner of
Expand Down

0 comments on commit 26f2656

Please sign in to comment.