forge
Explore
Log in

Don't you dare go Hollow

av1qm: using_qmatrix dequant lift -- libaom iwt_matrix_ref/get_dqv port, frame-level qm levels, column-major matrix indexing; refusal retired with pixel-exact witness (qm-on 420 units scaled, control 0; fail-pre-fix measured on 73f3ac64)

Browse files at this commit

94c775f65f8a7f1c56367624fa550d7d418ec1f9Ahmet Kaan GümüşParent: e32630c
crates/ec-av1/src/decode.rsmodified+88 -2
@@ -110,6 +110,15 @@ pub(crate) struct FrameCtx {
110 pub(crate) delta_q_res: std::cell::Cell<i32>,110 pub(crate) delta_q_res: std::cell::Cell<i32>,
111 pub(crate) current_q_idx: std::cell::Cell<i32>,111 pub(crate) current_q_idx: std::cell::Cell<i32>,
112 pub(crate) quant_deltas: std::cell::Cell<crate::quant::QuantDeltas>,112 pub(crate) quant_deltas: std::cell::Cell<crate::quant::QuantDeltas>,
113+ /// lane-av1-qmatrix: this frame's quantisation-matrix LEVEL per plane,
114+ /// straight from spec 5.9.12's `qm_y`/`qm_u`/`qm_v` (the parsed 4-bit
115+ /// value IS the table level -- libaom `quant_params->qmatrix_level_y/u/v`,
116+ /// decodeframe.c:1902-1917; the `aom_get_qmlevel` formula that maps
117+ /// base_q_idx onto a level is encoder-side only). 15 is libaom's flat
118+ /// sentinel (`NUM_QM_LEVELS - 1`, stored as NULL tables) and the default
119+ /// every `using_qmatrix == 0` frame keeps, so a matrix-free stream
120+ /// selects no matrix.
121+ pub(crate) qm_levels: std::cell::Cell<[u8; 3]>,
113 pub(crate) delta_lf_present: std::cell::Cell<bool>,122 pub(crate) delta_lf_present: std::cell::Cell<bool>,
114 pub(crate) delta_lf_res: std::cell::Cell<i32>,123 pub(crate) delta_lf_res: std::cell::Cell<i32>,
115 pub(crate) delta_lf_multi: std::cell::Cell<bool>,124 pub(crate) delta_lf_multi: std::cell::Cell<bool>,
@@ -221,6 +230,7 @@ impl FrameCtx {
221 v_dc: 0,230 v_dc: 0,
222 v_ac: 0,231 v_ac: 0,
223 }),232 }),
233+ qm_levels: std::cell::Cell::new([crate::qm::NUM_QM_LEVELS as u8 - 1; 3]),
224 delta_lf_present: std::cell::Cell::new(false),234 delta_lf_present: std::cell::Cell::new(false),
225 delta_lf_res: std::cell::Cell::new(1),235 delta_lf_res: std::cell::Cell::new(1),
226 delta_lf_multi: std::cell::Cell::new(false),236 delta_lf_multi: std::cell::Cell::new(false),
@@ -767,6 +777,54 @@ fn plane_q_delta(plane_idx: usize, fctx: &crate::decode::FrameCtx) -> (i32, i32)
767 }777 }
768 }778 }
769 779
780+/// lane-av1-qmatrix: records this FRAME's quantisation-matrix levels
781+/// ([`crate::decode::FrameCtx::qm_levels`]) from the parsed
782+/// `quantization_params`. Called from `stream.rs` at the frame header, where
783+/// the refusal this lane lifted used to sit.
784+pub(crate) fn set_qm_levels(levels: [u8; 3], fctx: &crate::decode::FrameCtx) {
785+ fctx.qm_levels.with(|c| c.set(levels));
786+}
787+
788+/// This block's inverse quantisation matrix -- libaom `av1_get_iqmatrix`
789+/// (`av1/common/quant_common.c:243`) plus `setup_segmentation_dequant`'s
790+/// `av1_use_qmatrix` gate, evaluated per dequantisation call site the same
791+/// way the site already evaluates its quantizer index. `None` dequantises
792+/// flat (the only behaviour before lane-av1-qmatrix):
793+/// - a level of 15, which is both libaom's flat sentinel and what every
794+/// `using_qmatrix == 0` frame's levels are parked at;
795+/// - a 1D/identity transform (`is_2d_transform` false: libaom's
796+/// `av1_get_iqmatrix` reads the NULL slot for those);
797+/// - a LOSSLESS block (libaom `av1_use_qmatrix`'s `!xd->lossless[seg]`: the
798+/// segment-adjusted quantizer index clamped to 0 with all five plane
799+/// deltas zero -- the same `block_q_idx` every other dequant site reads).
800+fn block_iqmatrix(
801+ fctx: &crate::decode::FrameCtx,
802+ plane_idx: usize,
803+ w: usize,
804+ h: usize,
805+ tx_type: TxType,
806+) -> Option<crate::qm::Iqm> {
807+ let levels = fctx.qm_levels.with(|c| c.get());
808+ let level = levels[plane_idx.min(2)];
809+ if usize::from(level) >= crate::qm::NUM_QM_LEVELS - 1 {
810+ return None;
811+ }
812+ if !tx_type.is_2d_transform() {
813+ return None;
814+ }
815+ let d = fctx.quant_deltas.with(|c| c.get());
816+ if block_q_idx(fctx) == 0
817+ && d.y_dc == 0
818+ && d.u_dc == 0
819+ && d.u_ac == 0
820+ && d.v_dc == 0
821+ && d.v_ac == 0
822+ {
823+ return None;
824+ }
825+ crate::qm::iwt_matrix(usize::from(level), plane_idx != 0, w, h)
826+}
827+
770 /// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom828 /// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom
771 /// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`]829 /// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`]
772 /// at every block-decode call site (spec order `skip -> cdef -> delta_q`).830 /// at every block-decode call site (spec order `skip -> cdef -> delta_q`).
@@ -2375,6 +2433,10 @@ pub(crate) struct TxParams {
2375 pub(crate) dc_delta: i32,2433 pub(crate) dc_delta: i32,
2376 pub(crate) ac_delta: i32,2434 pub(crate) ac_delta: i32,
2377 pub(crate) tx_type: TxType,2435 pub(crate) tx_type: TxType,
2436+ /// lane-av1-qmatrix: this unit's inverse quantisation matrix, selected
2437+ /// on the parse thread by [`block_iqmatrix`] and captured here so the
2438+ /// recon worker scales identically without touching frame state.
2439+ pub(crate) qm: Option<crate::qm::Iqm>,
2378 pub(crate) stride: usize,2440 pub(crate) stride: usize,
2379 /// lane-lossless: this transform unit belongs to a lossless frame, so the2441 /// lane-lossless: this transform unit belongs to a lossless frame, so the
2380 /// residual comes off the 4x4 Walsh-Hadamard2442 /// residual comes off the 4x4 Walsh-Hadamard
@@ -2398,6 +2460,7 @@ impl TxParams {
2398 dequant_and_inverse_typed_wh(2460 dequant_and_inverse_typed_wh(
2399 grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta,2461 grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta,
2400 self.tx_type,2462 self.tx_type,
2463+ self.qm,
2401 )2464 )
2402 };2465 };
2403 // An all-zero grid transforms to the empty marker, and a strided copy2466 // An all-zero grid transforms to the empty marker, and a strided copy
@@ -10010,6 +10073,7 @@ fn decode_rect_split(
10010 plane_q_delta(0, fctx).0,10073 plane_q_delta(0, fctx).0,
10011 plane_q_delta(0, fctx).1,10074 plane_q_delta(0, fctx).1,
10012 tu_tx_type,10075 tu_tx_type,
10076+ block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type),
10013 );10077 );
10014 push_intra_rect(0, 10078 push_intra_rect(0,
10015 tu_px,10079 tu_px,
@@ -10066,6 +10130,7 @@ fn decode_rect_split(
10066 plane_q_delta(0, fctx).0,10130 plane_q_delta(0, fctx).0,
10067 plane_q_delta(0, fctx).1,10131 plane_q_delta(0, fctx).1,
10068 tu_tx_type,10132 tu_tx_type,
10133+ block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type),
10069 );10134 );
10070 push_intra_rect(0, 10135 push_intra_rect(0,
10071 tu_px,10136 tu_px,
@@ -10248,6 +10313,7 @@ fn decode_rect_split(
10248 plane_q_delta(plane_idx, fctx).0,10313 plane_q_delta(plane_idx, fctx).0,
10249 plane_q_delta(plane_idx, fctx).1,10314 plane_q_delta(plane_idx, fctx).1,
10250 tx_type,10315 tx_type,
10316+ block_iqmatrix(fctx, plane_idx, chroma_w, chroma_h, tx_type),
10251 );10317 );
10252 let cfl = m10318 let cfl = m
10253 .alpha10319 .alpha
@@ -11756,6 +11822,7 @@ fn decode_block_rect(
11756 plane_q_delta(0, fctx).0,11822 plane_q_delta(0, fctx).0,
11757 plane_q_delta(0, fctx).1,11823 plane_q_delta(0, fctx).1,
11758 luma_tx_type,11824 luma_tx_type,
11825+ block_iqmatrix(fctx, 0, bw, bh, luma_tx_type),
11759 );11826 );
11760 push_intra_rect(0, 11827 push_intra_rect(0,
11761 px,11828 px,
@@ -11801,6 +11868,7 @@ fn decode_block_rect(
11801 plane_q_delta(1, fctx).0,11868 plane_q_delta(1, fctx).0,
11802 plane_q_delta(1, fctx).1,11869 plane_q_delta(1, fctx).1,
11803 u_tx_type,11870 u_tx_type,
11871+ block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type),
11804 );11872 );
11805 push_intra_rect(1, 11873 push_intra_rect(1,
11806 cpx,11874 cpx,
@@ -11845,6 +11913,7 @@ fn decode_block_rect(
11845 plane_q_delta(2, fctx).0,11913 plane_q_delta(2, fctx).0,
11846 plane_q_delta(2, fctx).1,11914 plane_q_delta(2, fctx).1,
11847 v_tx_type,11915 v_tx_type,
11916+ block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type),
11848 );11917 );
11849 push_intra_rect(2, 11918 push_intra_rect(2,
11850 cpx,11919 cpx,
@@ -12165,6 +12234,7 @@ fn decode_leaf_rect(
12165 let luma_residual = dequant_and_inverse_typed_wh(12234 let luma_residual = dequant_and_inverse_typed_wh(
12166 &luma_levels, bw, bh, crate::decode::bit_depth(fctx),12235 &luma_levels, bw, bh, crate::decode::bit_depth(fctx),
12167 block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type,12236 block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type,
12237+ block_iqmatrix(fctx, 0, bw, bh, luma_tx_type),
12168 );12238 );
12169 if let Some(b) = &palette_y_buf {12239 if let Some(b) = &palette_y_buf {
12170 set_palette_pred(b.clone(), fctx);12240 set_palette_pred(b.clone(), fctx);
@@ -12185,6 +12255,7 @@ fn decode_leaf_rect(
12185 let u_residual = dequant_and_inverse_typed_wh(12255 let u_residual = dequant_and_inverse_typed_wh(
12186 &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx),12256 &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx),
12187 block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type,12257 block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type,
12258+ block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type),
12188 );12259 );
12189 if let Some((ub, _)) = &palette_uv_bufs {12260 if let Some((ub, _)) = &palette_uv_bufs {
12190 set_palette_pred(ub.clone(), fctx);12261 set_palette_pred(ub.clone(), fctx);
@@ -12204,6 +12275,7 @@ fn decode_leaf_rect(
12204 let v_residual = dequant_and_inverse_typed_wh(12275 let v_residual = dequant_and_inverse_typed_wh(
12205 &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx),12276 &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx),
12206 block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type,12277 block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type,
12278+ block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type),
12207 );12279 );
12208 if let Some((_, vb)) = &palette_uv_bufs {12280 if let Some((_, vb)) = &palette_uv_bufs {
12209 set_palette_pred(vb.clone(), fctx);12281 set_palette_pred(vb.clone(), fctx);
@@ -12556,6 +12628,7 @@ fn decode_block_rect4(
12556 plane_q_delta(0, fctx).0,12628 plane_q_delta(0, fctx).0,
12557 plane_q_delta(0, fctx).1,12629 plane_q_delta(0, fctx).1,
12558 luma_tx_type,12630 luma_tx_type,
12631+ block_iqmatrix(fctx, 0, bw, bh, luma_tx_type),
12559 );12632 );
12560 if let Some(b) = &palette_y_buf {12633 if let Some(b) = &palette_y_buf {
12561 set_palette_pred(b.clone(), fctx);12634 set_palette_pred(b.clone(), fctx);
@@ -12587,6 +12660,7 @@ fn decode_block_rect4(
12587 plane_q_delta(plane, fctx).0,12660 plane_q_delta(plane, fctx).0,
12588 plane_q_delta(plane, fctx).1,12661 plane_q_delta(plane, fctx).1,
12589 tx_type,12662 tx_type,
12663+ block_iqmatrix(fctx, plane, chroma_w, chroma_h, tx_type),
12590 );12664 );
12591 if let Some((ub, vb)) = &palette_uv_bufs {12665 if let Some((ub, vb)) = &palette_uv_bufs {
12592 set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx);12666 set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx);
@@ -13538,6 +13612,7 @@ fn decode_rect4_16_strip(
13538 plane_q_delta(0, fctx).0,13612 plane_q_delta(0, fctx).0,
13539 plane_q_delta(0, fctx).1,13613 plane_q_delta(0, fctx).1,
13540 tu_tx_type,13614 tu_tx_type,
13615+ block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type),
13541 );13616 );
13542 push_intra_rect(0, 13617 push_intra_rect(0,
13543 tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual,13618 tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual,
@@ -13620,6 +13695,7 @@ fn decode_rect4_16_strip(
13620 plane_q_delta(0, fctx).0,13695 plane_q_delta(0, fctx).0,
13621 plane_q_delta(0, fctx).1,13696 plane_q_delta(0, fctx).1,
13622 tx_type,13697 tx_type,
13698+ block_iqmatrix(fctx, 0, bw, bh, tx_type),
13623 );13699 );
13624 if let Some(b) = &palette_y_buf {13700 if let Some(b) = &palette_y_buf {
13625 set_palette_pred(b.clone(), fctx);13701 set_palette_pred(b.clone(), fctx);
@@ -13836,6 +13912,7 @@ fn decode_rect4_16_strip(
13836 plane_q_delta(plane, fctx).0,13912 plane_q_delta(plane, fctx).0,
13837 plane_q_delta(plane, fctx).1,13913 plane_q_delta(plane, fctx).1,
13838 tx_type,13914 tx_type,
13915+ block_iqmatrix(fctx, plane, cw, ch, tx_type),
13839 );13916 );
13840 if let Some((ub, vb)) = &palette_uv_bufs {13917 if let Some((ub, vb)) = &palette_uv_bufs {
13841 set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx);13918 set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx);
@@ -14287,6 +14364,7 @@ fn decode_block_rect64(
14287 plane_q_delta(0, fctx).0,14364 plane_q_delta(0, fctx).0,
14288 plane_q_delta(0, fctx).1,14365 plane_q_delta(0, fctx).1,
14289 luma_tx_type,14366 luma_tx_type,
14367+ block_iqmatrix(fctx, 0, bw, bh, luma_tx_type),
14290 );14368 );
14291 push_intra_rect(0, 14369 push_intra_rect(0,
14292 px,14370 px,
@@ -14371,6 +14449,7 @@ fn decode_block_rect64(
14371 plane_q_delta(1, fctx).0,14449 plane_q_delta(1, fctx).0,
14372 plane_q_delta(1, fctx).1,14450 plane_q_delta(1, fctx).1,
14373 u_tx_type,14451 u_tx_type,
14452+ block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type),
14374 );14453 );
14375 // lane-t900 r34: the UV-palette prediction override, exactly as the14454 // lane-t900 r34: the UV-palette prediction override, exactly as the
14376 // `skip` arm above installs it. The [`PALETTE_PRED`] slot is a14455 // `skip` arm above installs it. The [`PALETTE_PRED`] slot is a
@@ -14436,6 +14515,7 @@ fn decode_block_rect64(
14436 plane_q_delta(2, fctx).0,14515 plane_q_delta(2, fctx).0,
14437 plane_q_delta(2, fctx).1,14516 plane_q_delta(2, fctx).1,
14438 v_tx_type,14517 v_tx_type,
14518+ block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type),
14439 );14519 );
14440 if let Some((_, vb)) = &palette_uv_bufs {14520 if let Some((_, vb)) = &palette_uv_bufs {
14441 set_palette_pred(vb.clone(), fctx);14521 set_palette_pred(vb.clone(), fctx);
@@ -15512,7 +15592,8 @@ fn read_plane(
15512 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);15592 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
15513 let tx = TxParams {15593 let tx = TxParams {
15514 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,15594 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
15515- ac_delta, tx_type, stride: 0, lossless: lossless(fctx),15595+ ac_delta, tx_type, qm: block_iqmatrix(fctx, plane_idx, side, side, tx_type), stride: 0,
15596+ lossless: lossless(fctx),
15516 };15597 };
15517 if crate::envflags::env_flag!("EC_AV1_TRACE") {15598 if crate::envflags::env_flag!("EC_AV1_TRACE") {
15518 let residual = tx.run(&levels);15599 let residual = tx.run(&levels);
@@ -18087,6 +18168,7 @@ fn decode_leaf_rect8(
18087 let residual = dequant_and_inverse_typed_wh(18168 let residual = dequant_and_inverse_typed_wh(
18088 &levels, tw, th, crate::decode::bit_depth(fctx), block_q_idx(fctx),18169 &levels, tw, th, crate::decode::bit_depth(fctx), block_q_idx(fctx),
18089 plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tx_type,18170 plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tx_type,
18171+ block_iqmatrix(fctx, 0, tw, th, tx_type),
18090 );18172 );
18091 if (tw, th) == (4, 4) {18173 if (tw, th) == (4, 4) {
18092 push_intra(0, tu_px, tu_py, 4, DC_PRED, 0, tu_reach, &residual, None, None, smooth_neighbor, fctx);18174 push_intra(0, tu_px, tu_py, 4, DC_PRED, 0, tu_reach, &residual, None, None, smooth_neighbor, fctx);
@@ -18323,6 +18405,7 @@ fn decode_leaf_rect8(
18323 plane_q_delta(0, fctx).0,18405 plane_q_delta(0, fctx).0,
18324 plane_q_delta(0, fctx).1,18406 plane_q_delta(0, fctx).1,
18325 tx_type,18407 tx_type,
18408+ block_iqmatrix(fctx, 0, bw, bh, tx_type),
18326 );18409 );
18327 push_intra_rect(0, 18410 push_intra_rect(0,
18328 px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra,18411 px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra,
@@ -20491,6 +20574,7 @@ fn read_inter_plane_rect(
20491 // (`TxParams::stride`) after the inverse transform.20574 // (`TxParams::stride`) after the inverse transform.
20492 let tx = TxParams {20575 let tx = TxParams {
20493 w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type,20576 w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type,
20577+ qm: block_iqmatrix(fctx, plane_idx, w, h, tx_type),
20494 stride, lossless: lossless(fctx),20578 stride, lossless: lossless(fctx),
20495 };20579 };
20496 push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx);20580 push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx);
@@ -26735,7 +26819,8 @@ fn read_inter_plane(
26735 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);26819 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
26736 let tx = TxParams {26820 let tx = TxParams {
26737 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,26821 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
26738- ac_delta, tx_type, stride: 0, lossless: lossless(fctx),26822+ ac_delta, tx_type, qm: block_iqmatrix(fctx, plane_idx, side, side, tx_type), stride: 0,
26823+ lossless: lossless(fctx),
26739 };26824 };
26740 push_mc_rect_tx(plane_idx, x, y, side, side, side, prediction, tx, &grid, fctx);26825 push_mc_rect_tx(plane_idx, x, y, side, side, side, prediction, tx, &grid, fctx);
26741 Ok((grid, tx_type))26826 Ok((grid, tx_type))
@@ -33390,6 +33475,7 @@ fn decode_intra_sub8_leaf(
33390 plane_q_delta(0, fctx).0,33475 plane_q_delta(0, fctx).0,
33391 plane_q_delta(0, fctx).1,33476 plane_q_delta(0, fctx).1,
33392 tx_type,33477 tx_type,
33478+ block_iqmatrix(fctx, 0, bw, bh, tx_type),
33393 );33479 );
33394 push_intra_rect(0, 33480 push_intra_rect(0,
33395 px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra,33481 px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra,
crates/ec-av1/src/encode.rsmodified+2 -1
@@ -2874,7 +2874,7 @@ impl Plane<'_> {
2874 // residual reconstructs to the prediction itself -- `clamp(p + 0)` is2874 // residual reconstructs to the prediction itself -- `clamp(p + 0)` is
2875 // `p` for a `u8`, so the bytes are the same.2875 // `p` for a `u8`, so the bytes are the same.
2876 let coded = dequant_and_inverse_typed_wh(2876 let coded = dequant_and_inverse_typed_wh(
2877- &levels, side, side, 8, i32::from(base_q_idx), 0, 0, tx_type,2877+ &levels, side, side, 8, i32::from(base_q_idx), 0, 0, tx_type, None,
2878 );2878 );
2879 #[cfg(test)]2879 #[cfg(test)]
2880 stage_since(2, t);2880 stage_since(2, t);
@@ -3032,6 +3032,7 @@ impl Plane<'_> {
3032 0,3032 0,
3033 0,3033 0,
3034 tx_type,3034 tx_type,
3035+ None,
3035 );3036 );
3036 #[cfg(test)]3037 #[cfg(test)]
3037 stage_since(2, t);3038 stage_since(2, t);
crates/ec-av1/src/lib.rsmodified+1 -0
@@ -46,6 +46,7 @@ pub mod mvstack;
46 pub mod obu;46 pub mod obu;
47 mod par;47 mod par;
48 pub mod probe;48 pub mod probe;
49+pub mod qm;
49 pub mod quant;50 pub mod quant;
50 mod refusal_inventory;51 mod refusal_inventory;
51 pub mod speed;52 pub mod speed;
crates/ec-av1/src/qm.rsadded+7046 -0

Diff too large; view file

@@ -0,0 +1,7046 @@
1+//! AV1 inverse quantisation matrices: spec 5.9.12's `using_qmatrix` +
2+//! `qm_y`/`qm_u`/`qm_v` and the per-position `dqv` scale of spec 7.12.3,
3+//! ported bug-for-bug from libaom (`av1/common/quant_common.c`'s
4+//! `iwt_matrix_ref` + `av1_qm_init` + `av1_get_iqmatrix`,
5+//! `av1/decoder/decodetxb.c`'s `get_dqv`). lane-av1-qmatrix.
6+//!
7+//! The shape of the port, and why each piece looks like it does:
8+//!
9+//! - The parsed 4-bit `qm_y`/`qm_u`/`qm_v` ARE the table level (libaom
10+//! `quant_params->qmatrix_level_y/u/v`, `decodeframe.c:1902-1917`). The
11+//! `aom_get_qmlevel(qindex, ...)` derivation that maps base_q_idx onto a
12+//! level is ENCODER-side only (how aomenc picks what to write); a decoder
13+//! never sees it.
14+//! - Level 15 is libaom's flat sentinel: `av1_qm_init` stores NULL pointers
15+//! for `NUM_QM_LEVELS - 1`, and 1D/identity transforms read that NULL slot
16+//! unconditionally (`av1_get_iqmatrix`'s `is_2d_transform` branch), so
17+//! `V_DCT`/`H_DCT`/`IDTX`-class units never scale their `dqv`. Both facts
18+//! land here as "return [`Iqm`] or `None`", and `None` dequantises exactly
19+//! as before this module existed.
20+//! - Per `av1_use_qmatrix` (`setup_segmentation_dequant`), a LOSSLESS
21+//! segment (quantizer index clamped to 0 with all five deltas zero) also
22+//! dequantises flat; the decode-side caller checks that per block through
23+//! the same quantizer index every dequant call site already uses.
24+//! - The tables are per plane KIND, not just per level: `iwt_matrix_ref`
25+//! is `[level][luma/chroma][...]` and chroma matrices are genuinely
26+//! different data (`av1_qm_init`'s `&iwt_matrix_ref[q][c >= 1][..]`).
27+//! - libaom stores one flat 3344-entry buffer per (level, kind); the
28+//! 14 own-storage shapes live at [`shape_range`]'s offsets in
29+//! `av1_qm_init`'s `TX_SIZES_ALL` walk order (the C file's own
30+//! `/* Size ... */` comments mark that same order), and the 64-axis
31+//! shapes REUSE a smaller shape's matrix (`av1_get_adjusted_tx_size`):
32+//! 64x64/64x32/32x64 point at the 32x32 matrix, 64x16 at 32x16, 16x64 at
33+//! 16x32. libaom indexes those reuses through its ACTIVE-AREA coeff
34+//! buffer (whose eob `av1_get_max_eob` caps at 1024), which is why
35+//! [`dqv_at`] re-strides our true-w raster onto `stride`.
36+
37+/// libaom `NUM_QM_LEVELS` (`quant_common.h`: `1 << QM_LEVEL_BITS`).
38+pub const NUM_QM_LEVELS: usize = 16;
39+/// libaom `AOM_QM_BITS` (`aom_dsp/aom_dsp_common.h`): the fixed-point
40+/// shift of the matrix multiply, `(qm * dqv + 16) >> 5`.
41+pub const AOM_QM_BITS: u32 = 5;
42+
43+/// One selected inverse matrix: libaom's `const qm_val_t *` plus the
44+/// stride that pointer runs at -- the ACTIVE-AREA HEIGHT, because libaom's
45+/// coefficient positions are COLUMN-major ([`dqv_at`]).
46+#[derive(Clone, Copy)]
47+pub struct Iqm {
48+ /// The matrix entries, in libaom's transposed position order.
49+ pub data: &'static [u8],
50+ /// The column stride of libaom's position space: the active-area
51+ /// height.
52+ pub stride: usize,
53+}
54+
55+/// `av1_get_adjusted_tx_size` (`av1/common/blockd.h:1361`): the matrix to
56+/// use for a transform whose true shape stores no matrix of its own.
57+fn adjusted(w: usize, h: usize) -> (usize, usize) {
58+ match (w, h) {
59+ (64, 64) | (64, 32) | (32, 64) => (32, 32),
60+ (64, 16) => (32, 16),
61+ (16, 64) => (16, 32),
62+ _ => (w, h),
63+ }
64+}
65+
66+/// `[av1_qm_init]`'s flat-buffer offsets for the 14 own-storage shapes, in
67+/// its `TX_SIZES_ALL` walk order. `(offset, length)` into one (level, kind)
68+/// slice; `None` for a shape that stores no matrix (the 64-axis clamps land
69+/// here only if `adjusted` ever misses one -- it does not).
70+fn shape_range(w: usize, h: usize) -> Option<(usize, usize)> {
71+ match (w, h) {
72+ (4, 4) => Some((0, 16)),
73+ (8, 8) => Some((16, 64)),
74+ (16, 16) => Some((80, 256)),
75+ (32, 32) => Some((336, 1024)),
76+ (4, 8) => Some((1360, 32)),
77+ (8, 4) => Some((1392, 32)),
78+ (8, 16) => Some((1424, 128)),
79+ (16, 8) => Some((1552, 128)),
80+ (16, 32) => Some((1680, 512)),
81+ (32, 16) => Some((2192, 512)),
82+ (4, 16) => Some((2704, 64)),
83+ (16, 4) => Some((2768, 64)),
84+ (8, 32) => Some((2832, 256)),
85+ (32, 8) => Some((3088, 256)),
86+ _ => None,
87+ }
88+}
89+
90+/// `av1_get_iqmatrix` narrowed to the decode path: the matrix for transform
91+/// `(w, h)` at `level` (0..=14) of plane kind (`chroma` = planes 1/2), or
92+/// `None` for libaom's NULL matrix. Level >= 15 is the flat sentinel, so a
93+/// qm-off frame (levels parked at 15) never selects a matrix.
94+pub fn iwt_matrix(level: usize, chroma: bool, w: usize, h: usize) -> Option<Iqm> {
95+ if level >= NUM_QM_LEVELS - 1 {
96+ return None;
97+ }
98+ let (sw, sh) = adjusted(w, h);
99+ let (off, len) = shape_range(sw, sh)?;
100+ Some(Iqm {
101+ data: &IWT[level][usize::from(chroma)][off..off + len],
102+ stride: sh,
103+ })
104+}
105+
106+/// `get_dqv` (`av1/decoder/decodetxb.c:54-60`): the DC/AC quantizer scaled
107+/// by the coefficient's own matrix entry, `((m * dqv) + 16) >> 5`. The
108+/// subtle half is the index: libaom's scan positions -- and so
109+/// `iqmatrix[coeff_idx]` and its whole txb coefficient buffer -- live in a
110+/// COLUMN-major position space (its `mcol_scan_8x4` is the identity
111+/// `0,1,2,...`: position `p` strides DOWN the columns of the `h`-tall
112+/// transposed block), so the coefficient of our row-major `w`-wide grid at
113+/// `(row, col)` reads matrix entry `col * stride + row`. Verified
114+/// coefficient-for-coefficient against the instrumented oracle on the
115+/// `--enable-qm=1` repro stream -- including 8x8 units, whose stored
116+/// matrices are NOT symmetric, so the transposition is observable there
117+/// and only this mapping matches.
118+#[inline]
119+pub fn dqv_at(qm: &Iqm, pos: usize, w: usize, dqv: i64) -> i64 {
120+ let (row, col) = (pos / w, pos % w);
121+ match qm.data.get(col * qm.stride + row) {
122+ Some(&m) => ((i64::from(m) * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS,
123+ // libaom's `av1_get_max_eob` caps eob at 1024 for the clamped 64
124+ // shapes, so a NONZERO level never reaches a position past the
125+ // reused matrix -- `dequant_wh_into`'s zero-level skip guards this
126+ // arm and it is unreachable on spec streams. Flat (dqv) if ever
127+ // reached.
128+ None => dqv,
129+ }
130+}
131+
132+// GENERATED from libaom's `iwt_matrix_ref[NUM_QM_LEVELS - 1][2][QM_TOTAL_SIZE]`
133+// (`av1/common/quant_common.c`, oracle at ~/.cache/aom-oracle) -- verbatim values.
134+// 15 levels x [luma, chroma] x 3344 flat entries; per (level, kind) the 14
135+// own-storage shapes follow `av1_qm_init`'s `TX_SIZES_ALL` walk order (the
136+// 64-variants reuse smaller shapes -- see `SHAPE_OFFSET`), which is also the
137+// order of the `/* Size ... */` comment markers in the C file itself.
138+// Level 15 is absent by construction: libaom stores NULL (flat) for it.
139+#[rustfmt::skip]
140+static IWT: [[[u8; 3344]; 2]; 15] = [
141+ // qm level 0
142+ [
143+ [// luma
144+ // Size 4x4
145+ 32, 43, 73, 97, 43, 67, 94, 110, 73, 94, 137, 150, 97, 110, 150, 200,
146+ // Size 8x8
147+ 32, 32, 38, 51, 68, 84, 95, 109, 32, 35, 40, 49, 63, 76, 89, 102,
148+ 38, 40, 54, 65, 78, 91, 98, 106, 51, 49, 65, 82, 97, 111, 113, 121,
149+ 68, 63, 78, 97, 117, 134, 138, 142, 84, 76, 91, 111, 134, 152, 159, 168,
150+ 95, 89, 98, 113, 138, 159, 183, 199, 109, 102, 106, 121, 142, 168, 199, 220,
151+ // Size 16x16
152+ 32, 31, 31, 34, 36, 44, 48, 59, 65, 80, 83, 91, 97, 104, 111, 119,
153+ 31, 32, 32, 33, 34, 41, 44, 54, 59, 72, 75, 83, 90, 97, 104, 112,
154+ 31, 32, 33, 35, 36, 42, 45, 54, 59, 71, 74, 81, 86, 93, 100, 107,
155+ 34, 33, 35, 39, 42, 47, 51, 58, 63, 74, 76, 81, 84, 90, 97, 105,
156+ 36, 34, 36, 42, 48, 54, 57, 64, 68, 79, 81, 88, 91, 96, 102, 105,
157+ 44, 41, 42, 47, 54, 63, 67, 75, 79, 90, 92, 95, 100, 102, 109, 112,
158+ 48, 44, 45, 51, 57, 67, 71, 80, 85, 96, 99, 107, 108, 111, 117, 120,
159+ 59, 54, 54, 58, 64, 75, 80, 92, 98, 110, 113, 115, 116, 122, 125, 130,
160+ 65, 59, 59, 63, 68, 79, 85, 98, 105, 118, 121, 127, 130, 134, 135, 140,
161+ 80, 72, 71, 74, 79, 90, 96, 110, 118, 134, 137, 140, 143, 144, 146, 152,
162+ 83, 75, 74, 76, 81, 92, 99, 113, 121, 137, 140, 151, 152, 155, 158, 165,
163+ 91, 83, 81, 81, 88, 95, 107, 115, 127, 140, 151, 159, 166, 169, 173, 179,
164+ 97, 90, 86, 84, 91, 100, 108, 116, 130, 143, 152, 166, 174, 182, 189, 193,
165+ 104, 97, 93, 90, 96, 102, 111, 122, 134, 144, 155, 169, 182, 191, 200, 210,
166+ 111, 104, 100, 97, 102, 109, 117, 125, 135, 146, 158, 173, 189, 200, 210, 220,
167+ 119, 112, 107, 105, 105, 112, 120, 130, 140, 152, 165, 179, 193, 210, 220, 231,
168+ // Size 32x32
169+ 32, 31, 31, 31, 31, 32, 34, 35, 36, 39, 44, 46, 48, 54, 59, 62,
170+ 65, 71, 80, 81, 83, 88, 91, 94, 97, 101, 104, 107, 111, 115, 119, 123,
171+ 31, 32, 32, 32, 32, 32, 34, 34, 35, 38, 42, 44, 46, 51, 56, 59,
172+ 62, 68, 76, 77, 78, 84, 86, 89, 92, 95, 99, 102, 105, 109, 113, 116,
173+ 31, 32, 32, 32, 32, 32, 33, 34, 34, 37, 41, 42, 44, 49, 54, 56,
174+ 59, 65, 72, 73, 75, 80, 83, 86, 90, 93, 97, 101, 104, 108, 112, 116,
175+ 31, 32, 32, 32, 33, 33, 34, 35, 35, 38, 41, 43, 45, 49, 54, 56,
176+ 59, 64, 72, 73, 74, 79, 82, 85, 88, 91, 94, 97, 101, 104, 107, 111,
177+ 31, 32, 32, 33, 33, 34, 35, 36, 36, 39, 42, 44, 45, 50, 54, 56,
178+ 59, 64, 71, 72, 74, 78, 81, 84, 86, 89, 93, 96, 100, 104, 107, 111,
179+ 32, 32, 32, 33, 34, 35, 37, 37, 38, 40, 42, 44, 46, 49, 53, 55,
180+ 58, 63, 69, 70, 72, 76, 79, 82, 85, 89, 93, 96, 99, 102, 106, 109,
181+ 34, 34, 33, 34, 35, 37, 39, 41, 42, 45, 47, 49, 51, 54, 58, 60,
182+ 63, 68, 74, 75, 76, 80, 81, 82, 84, 87, 90, 93, 97, 101, 105, 110,
183+ 35, 34, 34, 35, 36, 37, 41, 43, 45, 47, 50, 52, 53, 57, 61, 63,
184+ 65, 70, 76, 77, 79, 82, 84, 86, 89, 91, 92, 93, 96, 100, 103, 107,
185+ 36, 35, 34, 35, 36, 38, 42, 45, 48, 50, 54, 55, 57, 60, 64, 66,
186+ 68, 73, 79, 80, 81, 85, 88, 90, 91, 93, 96, 99, 102, 103, 105, 107,
187+ 39, 38, 37, 38, 39, 40, 45, 47, 50, 54, 58, 59, 61, 65, 69, 71,
188+ 73, 78, 84, 85, 86, 91, 92, 92, 95, 98, 100, 101, 103, 106, 110, 114,
189+ 44, 42, 41, 41, 42, 42, 47, 50, 54, 58, 63, 65, 67, 71, 75, 77,
190+ 79, 84, 90, 91, 92, 95, 95, 97, 100, 101, 102, 105, 109, 111, 112, 114,
191+ 46, 44, 42, 43, 44, 44, 49, 52, 55, 59, 65, 67, 69, 74, 78, 80,
192+ 82, 87, 93, 94, 95, 98, 100, 103, 102, 105, 108, 110, 111, 113, 117, 121,
193+ 48, 46, 44, 45, 45, 46, 51, 53, 57, 61, 67, 69, 71, 76, 80, 83,
194+ 85, 90, 96, 97, 99, 103, 107, 105, 108, 111, 111, 113, 117, 119, 120, 122,
195+ 54, 51, 49, 49, 50, 49, 54, 57, 60, 65, 71, 74, 76, 82, 87, 89,
196+ 92, 97, 104, 105, 106, 111, 110, 111, 114, 113, 116, 120, 120, 121, 125, 130,
197+ 59, 56, 54, 54, 54, 53, 58, 61, 64, 69, 75, 78, 80, 87, 92, 95,
198+ 98, 103, 110, 111, 113, 115, 115, 119, 116, 120, 122, 122, 125, 129, 130, 130,
199+ 62, 59, 56, 56, 56, 55, 60, 63, 66, 71, 77, 80, 83, 89, 95, 98,
200+ 101, 107, 114, 115, 117, 119, 123, 121, 125, 126, 125, 129, 131, 131, 135, 140,
201+ 65, 62, 59, 59, 59, 58, 63, 65, 68, 73, 79, 82, 85, 92, 98, 101,
202+ 105, 111, 118, 119, 121, 126, 127, 128, 130, 130, 134, 133, 135, 140, 140, 140,
203+ 71, 68, 65, 64, 64, 63, 68, 70, 73, 78, 84, 87, 90, 97, 103, 107,
204+ 111, 117, 125, 126, 128, 134, 132, 136, 133, 138, 137, 140, 143, 142, 145, 150,
205+ 80, 76, 72, 72, 71, 69, 74, 76, 79, 84, 90, 93, 96, 104, 110, 114,
206+ 118, 125, 134, 135, 137, 139, 140, 139, 143, 142, 144, 146, 146, 151, 152, 151,
207+ 81, 77, 73, 73, 72, 70, 75, 77, 80, 85, 91, 94, 97, 105, 111, 115,
208+ 119, 126, 135, 137, 138, 144, 147, 146, 148, 149, 151, 150, 156, 155, 157, 163,
209+ 83, 78, 75, 74, 74, 72, 76, 79, 81, 86, 92, 95, 99, 106, 113, 117,
210+ 121, 128, 137, 138, 140, 147, 151, 156, 152, 157, 155, 161, 158, 162, 165, 164,
211+ 88, 84, 80, 79, 78, 76, 80, 82, 85, 91, 95, 98, 103, 111, 115, 119,
212+ 126, 134, 139, 144, 147, 152, 154, 158, 163, 159, 165, 163, 168, 168, 169, 176,
213+ 91, 86, 83, 82, 81, 79, 81, 84, 88, 92, 95, 100, 107, 110, 115, 123,
214+ 127, 132, 140, 147, 151, 154, 159, 161, 166, 171, 169, 173, 173, 176, 179, 177,
215+ 94, 89, 86, 85, 84, 82, 82, 86, 90, 92, 97, 103, 105, 111, 119, 121,
216+ 128, 136, 139, 146, 156, 158, 161, 166, 168, 174, 179, 178, 180, 183, 183, 190,
217+ 97, 92, 90, 88, 86, 85, 84, 89, 91, 95, 100, 102, 108, 114, 116, 125,
218+ 130, 133, 143, 148, 152, 163, 166, 168, 174, 176, 182, 187, 189, 188, 193, 191,
219+ 101, 95, 93, 91, 89, 89, 87, 91, 93, 98, 101, 105, 111, 113, 120, 126,
220+ 130, 138, 142, 149, 157, 159, 171, 174, 176, 183, 184, 191, 195, 199, 197, 204,
221+ 104, 99, 97, 94, 93, 93, 90, 92, 96, 100, 102, 108, 111, 116, 122, 125,
222+ 134, 137, 144, 151, 155, 165, 169, 179, 182, 184, 191, 193, 200, 204, 210, 206,
223+ 107, 102, 101, 97, 96, 96, 93, 93, 99, 101, 105, 110, 113, 120, 122, 129,
224+ 133, 140, 146, 150, 161, 163, 173, 178, 187, 191, 193, 200, 202, 210, 214, 222,
225+ 111, 105, 104, 101, 100, 99, 97, 96, 102, 103, 109, 111, 117, 120, 125, 131,
226+ 135, 143, 146, 156, 158, 168, 173, 180, 189, 195, 200, 202, 210, 212, 220, 224,
227+ 115, 109, 108, 104, 104, 102, 101, 100, 103, 106, 111, 113, 119, 121, 129, 131,
228+ 140, 142, 151, 155, 162, 168, 176, 183, 188, 199, 204, 210, 212, 220, 222, 230,
229+ 119, 113, 112, 107, 107, 106, 105, 103, 105, 110, 112, 117, 120, 125, 130, 135,
230+ 140, 145, 152, 157, 165, 169, 179, 183, 193, 197, 210, 214, 220, 222, 231, 232,
231+ 123, 116, 116, 111, 111, 109, 110, 107, 107, 114, 114, 121, 122, 130, 130, 140,
232+ 140, 150, 151, 163, 164, 176, 177, 190, 191, 204, 206, 222, 224, 230, 232, 242,
233+ // Size 4x8
234+ 32, 33, 37, 49, 65, 80, 91, 104, 42, 42, 58, 71, 84, 97, 100, 112,
235+ 75, 69, 84, 103, 125, 142, 145, 146, 91, 86, 91, 110, 128, 152, 178, 190,
236+ // Size 8x4
237+ 32, 42, 75, 91, 33, 42, 69, 86, 37, 58, 84, 91, 49, 71, 103, 110,
238+ 65, 84, 125, 128, 80, 97, 142, 152, 91, 100, 145, 178, 104, 112, 146, 190,
239+ // Size 8x16
240+ 32, 31, 32, 34, 36, 44, 48, 58, 65, 79, 82, 91, 97, 103, 110, 118,
241+ 32, 33, 34, 37, 38, 43, 46, 54, 58, 70, 72, 80, 86, 93, 100, 107,
242+ 36, 34, 36, 42, 48, 53, 56, 63, 68, 79, 81, 88, 94, 98, 101, 105,
243+ 53, 49, 50, 54, 60, 71, 76, 87, 92, 104, 106, 106, 107, 114, 117, 118,
244+ 65, 59, 59, 63, 68, 79, 85, 98, 105, 118, 121, 130, 128, 131, 138, 136,
245+ 87, 78, 77, 79, 84, 95, 102, 116, 124, 141, 144, 148, 157, 150, 161, 157,
246+ 93, 86, 82, 80, 86, 94, 105, 112, 122, 135, 149, 162, 167, 174, 183, 182,
247+ 99, 93, 89, 88, 90, 97, 105, 115, 124, 135, 146, 159, 171, 186, 193, 203,
248+ // Size 16x8
249+ 32, 32, 36, 53, 65, 87, 93, 99, 31, 33, 34, 49, 59, 78, 86, 93,
250+ 32, 34, 36, 50, 59, 77, 82, 89, 34, 37, 42, 54, 63, 79, 80, 88,
251+ 36, 38, 48, 60, 68, 84, 86, 90, 44, 43, 53, 71, 79, 95, 94, 97,
252+ 48, 46, 56, 76, 85, 102, 105, 105, 58, 54, 63, 87, 98, 116, 112, 115,
253+ 65, 58, 68, 92, 105, 124, 122, 124, 79, 70, 79, 104, 118, 141, 135, 135,
254+ 82, 72, 81, 106, 121, 144, 149, 146, 91, 80, 88, 106, 130, 148, 162, 159,
255+ 97, 86, 94, 107, 128, 157, 167, 171, 103, 93, 98, 114, 131, 150, 174, 186,
256+ 110, 100, 101, 117, 138, 161, 183, 193, 118, 107, 105, 118, 136, 157, 182, 203,
257+ // Size 16x32
258+ 32, 31, 31, 31, 32, 32, 34, 35, 36, 39, 44, 46, 48, 53, 58, 61,
259+ 65, 71, 79, 81, 82, 88, 91, 94, 97, 100, 103, 107, 110, 114, 118, 122,
260+ 31, 32, 32, 32, 32, 33, 34, 34, 34, 37, 41, 43, 45, 49, 54, 57,
261+ 60, 65, 72, 74, 75, 80, 83, 85, 88, 91, 94, 97, 101, 104, 108, 111,
262+ 32, 32, 33, 33, 34, 35, 37, 37, 38, 40, 43, 44, 46, 50, 54, 56,
263+ 58, 63, 70, 71, 72, 77, 80, 83, 86, 89, 93, 96, 100, 104, 107, 111,
264+ 34, 34, 33, 34, 35, 37, 39, 41, 43, 45, 48, 49, 51, 54, 58, 60,
265+ 63, 68, 74, 75, 76, 80, 81, 82, 85, 87, 90, 93, 97, 100, 103, 107,
266+ 36, 35, 34, 35, 36, 38, 42, 45, 48, 50, 53, 55, 56, 60, 63, 66,
267+ 68, 73, 79, 80, 81, 85, 88, 91, 94, 97, 98, 100, 101, 103, 105, 107,
268+ 44, 42, 41, 41, 42, 42, 48, 50, 54, 58, 63, 65, 67, 71, 75, 77,
269+ 79, 84, 90, 91, 92, 97, 100, 100, 100, 100, 101, 104, 108, 112, 115, 119,
270+ 53, 51, 49, 49, 50, 49, 54, 57, 60, 65, 71, 73, 76, 82, 87, 89,
271+ 92, 97, 104, 105, 106, 108, 106, 105, 107, 111, 114, 117, 117, 117, 118, 119,
272+ 59, 56, 54, 54, 54, 53, 58, 61, 64, 69, 75, 78, 80, 87, 92, 95,
273+ 98, 103, 110, 112, 113, 115, 114, 118, 123, 121, 120, 119, 123, 127, 131, 136,
274+ 65, 62, 59, 59, 59, 58, 63, 65, 68, 73, 79, 82, 85, 92, 98, 101,
275+ 105, 111, 118, 119, 121, 126, 130, 131, 128, 127, 131, 136, 138, 137, 136, 136,
276+ 79, 75, 72, 71, 71, 69, 73, 76, 78, 84, 90, 93, 96, 103, 110, 114,
277+ 118, 125, 133, 135, 136, 142, 142, 137, 140, 145, 144, 142, 141, 146, 151, 156,
278+ 87, 82, 78, 78, 77, 75, 79, 82, 84, 89, 95, 98, 102, 109, 116, 120,
279+ 124, 132, 141, 142, 144, 149, 148, 153, 157, 152, 150, 155, 161, 159, 157, 156,
280+ 90, 85, 82, 81, 80, 78, 78, 83, 87, 89, 93, 100, 102, 107, 115, 118,
281+ 123, 132, 136, 140, 151, 153, 155, 160, 161, 164, 170, 168, 165, 167, 172, 178,
282+ 93, 88, 86, 84, 82, 82, 80, 84, 86, 91, 94, 98, 105, 107, 112, 119,
283+ 122, 130, 135, 140, 149, 153, 162, 165, 167, 173, 174, 177, 183, 185, 182, 179,
284+ 96, 91, 90, 87, 86, 86, 83, 84, 89, 91, 95, 100, 102, 110, 111, 118,
285+ 123, 128, 135, 138, 149, 152, 160, 167, 173, 178, 180, 187, 188, 190, 197, 203,
286+ 99, 94, 93, 90, 89, 89, 88, 87, 90, 93, 97, 99, 105, 107, 115, 116,
287+ 124, 127, 135, 139, 146, 152, 159, 166, 171, 182, 186, 191, 193, 201, 203, 204,
288+ 102, 97, 97, 93, 93, 92, 92, 90, 90, 96, 97, 103, 104, 111, 112, 120,
289+ 121, 130, 131, 142, 143, 154, 155, 168, 169, 181, 183, 198, 200, 206, 208, 217,
290+ // Size 32x16
291+ 32, 31, 32, 34, 36, 44, 53, 59, 65, 79, 87, 90, 93, 96, 99, 102,
292+ 31, 32, 32, 34, 35, 42, 51, 56, 62, 75, 82, 85, 88, 91, 94, 97,
293+ 31, 32, 33, 33, 34, 41, 49, 54, 59, 72, 78, 82, 86, 90, 93, 97,
294+ 31, 32, 33, 34, 35, 41, 49, 54, 59, 71, 78, 81, 84, 87, 90, 93,
295+ 32, 32, 34, 35, 36, 42, 50, 54, 59, 71, 77, 80, 82, 86, 89, 93,
296+ 32, 33, 35, 37, 38, 42, 49, 53, 58, 69, 75, 78, 82, 86, 89, 92,
297+ 34, 34, 37, 39, 42, 48, 54, 58, 63, 73, 79, 78, 80, 83, 88, 92,
298+ 35, 34, 37, 41, 45, 50, 57, 61, 65, 76, 82, 83, 84, 84, 87, 90,
299+ 36, 34, 38, 43, 48, 54, 60, 64, 68, 78, 84, 87, 86, 89, 90, 90,
300+ 39, 37, 40, 45, 50, 58, 65, 69, 73, 84, 89, 89, 91, 91, 93, 96,
301+ 44, 41, 43, 48, 53, 63, 71, 75, 79, 90, 95, 93, 94, 95, 97, 97,
302+ 46, 43, 44, 49, 55, 65, 73, 78, 82, 93, 98, 100, 98, 100, 99, 103,
303+ 48, 45, 46, 51, 56, 67, 76, 80, 85, 96, 102, 102, 105, 102, 105, 104,
304+ 53, 49, 50, 54, 60, 71, 82, 87, 92, 103, 109, 107, 107, 110, 107, 111,
305+ 58, 54, 54, 58, 63, 75, 87, 92, 98, 110, 116, 115, 112, 111, 115, 112,
306+ 61, 57, 56, 60, 66, 77, 89, 95, 101, 114, 120, 118, 119, 118, 116, 120,
307+ 65, 60, 58, 63, 68, 79, 92, 98, 105, 118, 124, 123, 122, 123, 124, 121,
308+ 71, 65, 63, 68, 73, 84, 97, 103, 111, 125, 132, 132, 130, 128, 127, 130,
309+ 79, 72, 70, 74, 79, 90, 104, 110, 118, 133, 141, 136, 135, 135, 135, 131,
310+ 81, 74, 71, 75, 80, 91, 105, 112, 119, 135, 142, 140, 140, 138, 139, 142,
311+ 82, 75, 72, 76, 81, 92, 106, 113, 121, 136, 144, 151, 149, 149, 146, 143,
312+ 88, 80, 77, 80, 85, 97, 108, 115, 126, 142, 149, 153, 153, 152, 152, 154,
313+ 91, 83, 80, 81, 88, 100, 106, 114, 130, 142, 148, 155, 162, 160, 159, 155,
314+ 94, 85, 83, 82, 91, 100, 105, 118, 131, 137, 153, 160, 165, 167, 166, 168,
315+ 97, 88, 86, 85, 94, 100, 107, 123, 128, 140, 157, 161, 167, 173, 171, 169,
316+ 100, 91, 89, 87, 97, 100, 111, 121, 127, 145, 152, 164, 173, 178, 182, 181,
317+ 103, 94, 93, 90, 98, 101, 114, 120, 131, 144, 150, 170, 174, 180, 186, 183,
318+ 107, 97, 96, 93, 100, 104, 117, 119, 136, 142, 155, 168, 177, 187, 191, 198,
319+ 110, 101, 100, 97, 101, 108, 117, 123, 138, 141, 161, 165, 183, 188, 193, 200,
320+ 114, 104, 104, 100, 103, 112, 117, 127, 137, 146, 159, 167, 185, 190, 201, 206,
321+ 118, 108, 107, 103, 105, 115, 118, 131, 136, 151, 157, 172, 182, 197, 203, 208,
322+ 122, 111, 111, 107, 107, 119, 119, 136, 136, 156, 156, 178, 179, 203, 204, 217,
323+ // Size 4x16
324+ 31, 32, 32, 34, 34, 41, 45, 54, 60, 72, 75, 83, 88, 94, 101, 108,
325+ 44, 41, 42, 48, 54, 63, 67, 75, 79, 90, 92, 100, 100, 101, 108, 115,
326+ 79, 72, 71, 73, 78, 90, 96, 110, 118, 133, 136, 142, 140, 144, 141, 151,
327+ 96, 90, 86, 83, 89, 95, 102, 111, 123, 135, 149, 160, 173, 180, 188, 197,
328+ // Size 16x4
329+ 31, 44, 79, 96, 32, 41, 72, 90, 32, 42, 71, 86, 34, 48, 73, 83,
330+ 34, 54, 78, 89, 41, 63, 90, 95, 45, 67, 96, 102, 54, 75, 110, 111,
331+ 60, 79, 118, 123, 72, 90, 133, 135, 75, 92, 136, 149, 83, 100, 142, 160,
332+ 88, 100, 140, 173, 94, 101, 144, 180, 101, 108, 141, 188, 108, 115, 151, 197,
333+ // Size 8x32
334+ 32, 31, 31, 31, 32, 32, 34, 35, 36, 39, 44, 46, 48, 53, 58, 61,
335+ 65, 71, 79, 81, 82, 88, 91, 94, 97, 100, 103, 107, 110, 114, 118, 122,
336+ 32, 32, 33, 33, 34, 35, 37, 37, 38, 40, 43, 44, 46, 50, 54, 56,
337+ 58, 63, 70, 71, 72, 77, 80, 83, 86, 89, 93, 96, 100, 104, 107, 111,
338+ 36, 35, 34, 35, 36, 38, 42, 45, 48, 50, 53, 55, 56, 60, 63, 66,
339+ 68, 73, 79, 80, 81, 85, 88, 91, 94, 97, 98, 100, 101, 103, 105, 107,
340+ 53, 51, 49, 49, 50, 49, 54, 57, 60, 65, 71, 73, 76, 82, 87, 89,
341+ 92, 97, 104, 105, 106, 108, 106, 105, 107, 111, 114, 117, 117, 117, 118, 119,
342+ 65, 62, 59, 59, 59, 58, 63, 65, 68, 73, 79, 82, 85, 92, 98, 101,
343+ 105, 111, 118, 119, 121, 126, 130, 131, 128, 127, 131, 136, 138, 137, 136, 136,
344+ 87, 82, 78, 78, 77, 75, 79, 82, 84, 89, 95, 98, 102, 109, 116, 120,
345+ 124, 132, 141, 142, 144, 149, 148, 153, 157, 152, 150, 155, 161, 159, 157, 156,
346+ 93, 88, 86, 84, 82, 82, 80, 84, 86, 91, 94, 98, 105, 107, 112, 119,
347+ 122, 130, 135, 140, 149, 153, 162, 165, 167, 173, 174, 177, 183, 185, 182, 179,
348+ 99, 94, 93, 90, 89, 89, 88, 87, 90, 93, 97, 99, 105, 107, 115, 116,
349+ 124, 127, 135, 139, 146, 152, 159, 166, 171, 182, 186, 191, 193, 201, 203, 204,
350+ // Size 32x8
351+ 32, 32, 36, 53, 65, 87, 93, 99, 31, 32, 35, 51, 62, 82, 88, 94,
352+ 31, 33, 34, 49, 59, 78, 86, 93, 31, 33, 35, 49, 59, 78, 84, 90,
353+ 32, 34, 36, 50, 59, 77, 82, 89, 32, 35, 38, 49, 58, 75, 82, 89,
354+ 34, 37, 42, 54, 63, 79, 80, 88, 35, 37, 45, 57, 65, 82, 84, 87,
355+ 36, 38, 48, 60, 68, 84, 86, 90, 39, 40, 50, 65, 73, 89, 91, 93,
356+ 44, 43, 53, 71, 79, 95, 94, 97, 46, 44, 55, 73, 82, 98, 98, 99,
357+ 48, 46, 56, 76, 85, 102, 105, 105, 53, 50, 60, 82, 92, 109, 107, 107,
358+ 58, 54, 63, 87, 98, 116, 112, 115, 61, 56, 66, 89, 101, 120, 119, 116,
359+ 65, 58, 68, 92, 105, 124, 122, 124, 71, 63, 73, 97, 111, 132, 130, 127,
360+ 79, 70, 79, 104, 118, 141, 135, 135, 81, 71, 80, 105, 119, 142, 140, 139,
361+ 82, 72, 81, 106, 121, 144, 149, 146, 88, 77, 85, 108, 126, 149, 153, 152,
362+ 91, 80, 88, 106, 130, 148, 162, 159, 94, 83, 91, 105, 131, 153, 165, 166,
363+ 97, 86, 94, 107, 128, 157, 167, 171, 100, 89, 97, 111, 127, 152, 173, 182,
364+ 103, 93, 98, 114, 131, 150, 174, 186, 107, 96, 100, 117, 136, 155, 177, 191,
365+ 110, 100, 101, 117, 138, 161, 183, 193, 114, 104, 103, 117, 137, 159, 185, 201,
366+ 118, 107, 105, 118, 136, 157, 182, 203, 122, 111, 107, 119, 136, 156, 179, 204,
367+ ],
368+ [// chroma
369+ // Size 4x4
370+ 35, 46, 57, 66, 46, 60, 69, 71, 57, 69, 90, 90, 66, 71, 90, 109,
371+ // Size 8x8
372+ 31, 38, 47, 50, 57, 63, 67, 71, 38, 47, 46, 47, 52, 57, 62, 67,
373+ 47, 46, 54, 57, 61, 66, 67, 68, 50, 47, 57, 66, 72, 77, 75, 75,
374+ 57, 52, 61, 72, 82, 88, 86, 84, 63, 57, 66, 77, 88, 96, 95, 95,
375+ 67, 62, 67, 75, 86, 95, 104, 107, 71, 67, 68, 75, 84, 95, 107, 113,
376+ // Size 16x16
377+ 32, 30, 33, 41, 49, 49, 50, 54, 57, 63, 65, 68, 70, 72, 74, 76,
378+ 30, 32, 35, 42, 46, 45, 46, 49, 52, 57, 58, 62, 64, 67, 70, 72,
379+ 33, 35, 39, 45, 47, 45, 46, 49, 51, 56, 57, 60, 62, 64, 66, 69,
380+ 41, 42, 45, 48, 50, 49, 50, 52, 53, 57, 58, 59, 60, 61, 64, 67,
381+ 49, 46, 47, 50, 53, 53, 54, 55, 56, 60, 61, 64, 64, 65, 66, 66,
382+ 49, 45, 45, 49, 53, 58, 60, 62, 63, 67, 68, 67, 69, 68, 70, 70,
383+ 50, 46, 46, 50, 54, 60, 61, 65, 67, 71, 71, 74, 73, 73, 74, 74,
384+ 54, 49, 49, 52, 55, 62, 65, 71, 73, 78, 79, 78, 77, 78, 78, 78,
385+ 57, 52, 51, 53, 56, 63, 67, 73, 76, 82, 83, 84, 84, 84, 82, 83,
386+ 63, 57, 56, 57, 60, 67, 71, 78, 82, 89, 90, 90, 89, 88, 87, 88,
387+ 65, 58, 57, 58, 61, 68, 71, 79, 83, 90, 91, 94, 93, 93, 92, 93,
388+ 68, 62, 60, 59, 64, 67, 74, 78, 84, 90, 94, 98, 99, 98, 98, 98,
389+ 70, 64, 62, 60, 64, 69, 73, 77, 84, 89, 93, 99, 102, 103, 104, 104,
390+ 72, 67, 64, 61, 65, 68, 73, 78, 84, 88, 93, 98, 103, 106, 108, 109,
391+ 74, 70, 66, 64, 66, 70, 74, 78, 82, 87, 92, 98, 104, 108, 111, 112,
392+ 76, 72, 69, 67, 66, 70, 74, 78, 83, 88, 93, 98, 104, 109, 112, 116,
393+ // Size 32x32
394+ 32, 31, 30, 32, 33, 36, 41, 45, 49, 48, 49, 50, 50, 52, 54, 56,
395+ 57, 60, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 78,
396+ 31, 31, 31, 33, 34, 38, 42, 45, 47, 47, 47, 47, 48, 50, 52, 53,
397+ 54, 57, 60, 61, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74,
398+ 30, 31, 32, 33, 35, 40, 42, 44, 46, 45, 45, 45, 46, 47, 49, 51,
399+ 52, 54, 57, 58, 58, 61, 62, 63, 64, 66, 67, 68, 70, 71, 72, 74,
400+ 32, 33, 33, 35, 37, 41, 43, 45, 47, 46, 45, 46, 46, 47, 49, 50,
401+ 51, 54, 57, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
402+ 33, 34, 35, 37, 39, 43, 45, 46, 47, 46, 45, 46, 46, 47, 49, 50,
403+ 51, 53, 56, 57, 57, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70,
404+ 36, 38, 40, 41, 43, 47, 47, 47, 48, 46, 45, 46, 46, 47, 48, 49,
405+ 50, 52, 54, 55, 55, 57, 58, 59, 61, 62, 64, 65, 66, 67, 68, 69,
406+ 41, 42, 42, 43, 45, 47, 48, 49, 50, 49, 49, 49, 50, 50, 52, 52,
407+ 53, 55, 57, 58, 58, 60, 59, 59, 60, 61, 61, 63, 64, 66, 67, 69,
408+ 45, 45, 44, 45, 46, 47, 49, 50, 51, 51, 51, 51, 52, 52, 53, 54,
409+ 55, 57, 59, 59, 60, 61, 61, 62, 63, 63, 63, 63, 63, 64, 65, 66,
410+ 49, 47, 46, 47, 47, 48, 50, 51, 53, 53, 53, 54, 54, 54, 55, 56,
411+ 56, 58, 60, 61, 61, 63, 64, 64, 64, 64, 65, 66, 66, 66, 66, 66,
412+ 48, 47, 45, 46, 46, 46, 49, 51, 53, 54, 55, 56, 56, 57, 58, 59,
413+ 60, 61, 63, 64, 64, 66, 66, 65, 66, 67, 67, 67, 67, 68, 69, 70,
414+ 49, 47, 45, 45, 45, 45, 49, 51, 53, 55, 58, 59, 60, 61, 62, 63,
415+ 63, 65, 67, 67, 68, 69, 67, 68, 69, 68, 68, 69, 70, 70, 70, 70,
416+ 50, 47, 45, 46, 46, 46, 49, 51, 54, 56, 59, 60, 60, 62, 64, 64,
417+ 65, 67, 69, 69, 70, 70, 71, 71, 70, 70, 71, 71, 71, 71, 72, 74,
418+ 50, 48, 46, 46, 46, 46, 50, 52, 54, 56, 60, 60, 61, 63, 65, 66,
419+ 67, 68, 71, 71, 71, 73, 74, 72, 73, 74, 73, 73, 74, 74, 74, 74,
420+ 52, 50, 47, 47, 47, 47, 50, 52, 54, 57, 61, 62, 63, 66, 68, 69,
421+ 70, 72, 75, 75, 75, 77, 75, 75, 76, 75, 75, 76, 75, 75, 76, 77,
422+ 54, 52, 49, 49, 49, 48, 52, 53, 55, 58, 62, 64, 65, 68, 71, 72,
423+ 73, 75, 78, 78, 79, 79, 78, 79, 77, 78, 78, 77, 78, 79, 78, 78,
424+ 56, 53, 51, 50, 50, 49, 52, 54, 56, 59, 63, 64, 66, 69, 72, 73,
425+ 75, 77, 80, 80, 81, 81, 82, 80, 81, 81, 79, 81, 80, 79, 81, 82,
426+ 57, 54, 52, 51, 51, 50, 53, 55, 56, 60, 63, 65, 67, 70, 73, 75,
427+ 76, 79, 82, 82, 83, 85, 84, 83, 84, 83, 84, 82, 82, 84, 83, 82,
428+ 60, 57, 54, 54, 53, 52, 55, 57, 58, 61, 65, 67, 68, 72, 75, 77,
429+ 79, 82, 85, 85, 86, 88, 86, 87, 85, 86, 85, 85, 86, 84, 85, 86,
430+ 63, 60, 57, 57, 56, 54, 57, 59, 60, 63, 67, 69, 71, 75, 78, 80,
431+ 82, 85, 89, 89, 90, 90, 90, 89, 89, 88, 88, 88, 87, 88, 88, 87,
432+ 64, 61, 58, 57, 57, 55, 58, 59, 61, 64, 67, 69, 71, 75, 78, 80,
433+ 82, 85, 89, 90, 91, 92, 93, 92, 92, 91, 91, 90, 91, 90, 90, 92,
434+ 65, 61, 58, 58, 57, 55, 58, 60, 61, 64, 68, 70, 71, 75, 79, 81,
435+ 83, 86, 90, 91, 91, 94, 94, 96, 93, 94, 93, 94, 92, 93, 93, 92,
436+ 67, 63, 61, 60, 59, 57, 60, 61, 63, 66, 69, 70, 73, 77, 79, 81,
437+ 85, 88, 90, 92, 94, 96, 96, 97, 98, 95, 97, 95, 96, 95, 95, 96,
438+ 68, 64, 62, 61, 60, 58, 59, 61, 64, 66, 67, 71, 74, 75, 78, 82,
439+ 84, 86, 90, 93, 94, 96, 98, 98, 99, 100, 98, 99, 98, 98, 98, 97,
440+ 69, 65, 63, 62, 61, 59, 59, 62, 64, 65, 68, 71, 72, 75, 79, 80,
441+ 83, 87, 89, 92, 96, 97, 98, 100, 100, 101, 102, 101, 101, 101, 100, 102,
442+ 70, 66, 64, 63, 62, 61, 60, 63, 64, 66, 69, 70, 73, 76, 77, 81,
443+ 84, 85, 89, 92, 93, 98, 99, 100, 102, 102, 103, 104, 104, 103, 104, 102,
444+ 71, 67, 66, 64, 63, 62, 61, 63, 64, 67, 68, 70, 74, 75, 78, 81,
445+ 83, 86, 88, 91, 94, 95, 100, 101, 102, 104, 104, 105, 106, 107, 105, 107,
446+ 72, 68, 67, 65, 64, 64, 61, 63, 65, 67, 68, 71, 73, 75, 78, 79,
447+ 84, 85, 88, 91, 93, 97, 98, 102, 103, 104, 106, 106, 108, 108, 109, 107,
448+ 73, 69, 68, 66, 65, 65, 63, 63, 66, 67, 69, 71, 73, 76, 77, 81,
449+ 82, 85, 88, 90, 94, 95, 99, 101, 104, 105, 106, 109, 108, 110, 111, 112,
450+ 74, 70, 70, 67, 66, 66, 64, 63, 66, 67, 70, 71, 74, 75, 78, 80,
451+ 82, 86, 87, 91, 92, 96, 98, 101, 104, 106, 108, 108, 111, 111, 112, 113,
452+ 75, 71, 71, 68, 68, 67, 66, 64, 66, 68, 70, 71, 74, 75, 79, 79,
453+ 84, 84, 88, 90, 93, 95, 98, 101, 103, 107, 108, 110, 111, 113, 113, 115,
454+ 76, 72, 72, 69, 69, 68, 67, 65, 66, 69, 70, 72, 74, 76, 78, 81,
455+ 83, 85, 88, 90, 93, 95, 98, 100, 104, 105, 109, 111, 112, 113, 116, 115,
456+ 78, 74, 74, 70, 70, 69, 69, 66, 66, 70, 70, 74, 74, 77, 78, 82,
457+ 82, 86, 87, 92, 92, 96, 97, 102, 102, 107, 107, 112, 113, 115, 115, 118,
458+ // Size 4x8
459+ 31, 40, 46, 48, 54, 61, 64, 68, 47, 45, 56, 61, 65, 69, 68, 71,
460+ 60, 54, 64, 75, 85, 92, 90, 87, 66, 61, 64, 73, 82, 92, 102, 105,
461+ // Size 8x4
462+ 31, 47, 60, 66, 40, 45, 54, 61, 46, 56, 64, 64, 48, 61, 75, 73,
463+ 54, 65, 85, 82, 61, 69, 92, 92, 64, 68, 90, 102, 68, 71, 87, 105,
464+ // Size 8x16
465+ 32, 30, 33, 42, 49, 49, 50, 54, 57, 63, 64, 68, 70, 72, 74, 76,
466+ 37, 40, 43, 47, 48, 46, 46, 49, 50, 55, 56, 59, 62, 64, 67, 69,
467+ 48, 46, 47, 50, 53, 53, 54, 55, 56, 60, 61, 64, 66, 66, 66, 67,
468+ 52, 48, 47, 50, 54, 61, 64, 68, 70, 75, 75, 74, 73, 75, 74, 73,
469+ 57, 52, 51, 53, 57, 64, 67, 73, 76, 82, 83, 86, 83, 83, 84, 82,
470+ 66, 60, 59, 60, 62, 69, 73, 80, 84, 92, 93, 94, 96, 92, 94, 91,
471+ 68, 63, 60, 59, 62, 66, 72, 76, 80, 87, 93, 98, 99, 101, 103, 101,
472+ 71, 66, 63, 62, 62, 66, 70, 75, 79, 84, 89, 94, 98, 104, 106, 109,
473+ // Size 16x8
474+ 32, 37, 48, 52, 57, 66, 68, 71, 30, 40, 46, 48, 52, 60, 63, 66,
475+ 33, 43, 47, 47, 51, 59, 60, 63, 42, 47, 50, 50, 53, 60, 59, 62,
476+ 49, 48, 53, 54, 57, 62, 62, 62, 49, 46, 53, 61, 64, 69, 66, 66,
477+ 50, 46, 54, 64, 67, 73, 72, 70, 54, 49, 55, 68, 73, 80, 76, 75,
478+ 57, 50, 56, 70, 76, 84, 80, 79, 63, 55, 60, 75, 82, 92, 87, 84,
479+ 64, 56, 61, 75, 83, 93, 93, 89, 68, 59, 64, 74, 86, 94, 98, 94,
480+ 70, 62, 66, 73, 83, 96, 99, 98, 72, 64, 66, 75, 83, 92, 101, 104,
481+ 74, 67, 66, 74, 84, 94, 103, 106, 76, 69, 67, 73, 82, 91, 101, 109,
482+ // Size 16x32
483+ 32, 31, 30, 32, 33, 37, 42, 45, 49, 48, 49, 49, 50, 52, 54, 55,
484+ 57, 60, 63, 64, 64, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
485+ 31, 31, 32, 34, 36, 40, 43, 44, 46, 46, 45, 46, 46, 48, 50, 51,
486+ 52, 54, 57, 58, 59, 61, 62, 62, 63, 64, 65, 66, 67, 68, 69, 70,
487+ 37, 38, 40, 41, 43, 47, 47, 47, 48, 47, 46, 46, 46, 47, 49, 49,
488+ 50, 52, 55, 55, 56, 58, 59, 60, 62, 63, 64, 65, 67, 68, 69, 70,
489+ 42, 42, 42, 44, 45, 47, 48, 49, 50, 50, 49, 49, 50, 50, 52, 52,
490+ 53, 55, 58, 58, 58, 60, 60, 60, 60, 61, 62, 63, 64, 65, 66, 67,
491+ 48, 47, 46, 46, 47, 47, 50, 51, 53, 53, 53, 53, 54, 54, 55, 56,
492+ 56, 58, 60, 61, 61, 63, 64, 65, 66, 67, 66, 66, 66, 66, 67, 67,
493+ 49, 47, 45, 45, 46, 45, 49, 51, 53, 56, 58, 59, 59, 61, 62, 63,
494+ 64, 65, 67, 68, 68, 69, 71, 70, 69, 68, 68, 69, 70, 71, 72, 73,
495+ 52, 50, 48, 48, 47, 47, 50, 52, 54, 57, 61, 62, 64, 66, 68, 69,
496+ 70, 72, 75, 75, 75, 76, 74, 72, 73, 74, 75, 75, 74, 74, 73, 73,
497+ 54, 52, 50, 49, 49, 48, 52, 54, 55, 59, 62, 64, 65, 68, 71, 72,
498+ 73, 75, 78, 78, 79, 79, 78, 79, 81, 79, 78, 76, 77, 78, 80, 81,
499+ 57, 54, 52, 51, 51, 50, 53, 55, 57, 60, 64, 65, 67, 71, 73, 75,
500+ 76, 79, 82, 82, 83, 85, 86, 85, 83, 82, 83, 84, 84, 83, 82, 81,
501+ 63, 60, 57, 57, 56, 54, 57, 59, 60, 64, 67, 69, 71, 75, 78, 80,
502+ 82, 85, 89, 89, 90, 92, 91, 88, 89, 90, 89, 87, 86, 87, 88, 90,
503+ 66, 63, 60, 59, 59, 57, 60, 61, 62, 66, 69, 71, 73, 77, 80, 82,
504+ 84, 88, 92, 92, 93, 95, 94, 95, 96, 93, 92, 93, 94, 93, 91, 90,
505+ 67, 64, 62, 61, 60, 58, 58, 61, 63, 65, 67, 70, 72, 74, 78, 80,
506+ 82, 86, 88, 90, 95, 96, 96, 98, 97, 98, 100, 98, 96, 96, 97, 99,
507+ 68, 65, 63, 62, 60, 60, 59, 61, 62, 65, 66, 68, 72, 73, 76, 79,
508+ 80, 84, 87, 89, 93, 94, 98, 99, 99, 102, 101, 102, 103, 103, 101, 99,
509+ 69, 66, 65, 63, 62, 61, 60, 60, 63, 64, 66, 68, 70, 73, 74, 78,
510+ 80, 82, 85, 87, 91, 92, 96, 98, 101, 102, 103, 105, 105, 105, 107, 108,
511+ 71, 67, 66, 64, 63, 62, 62, 61, 62, 64, 66, 67, 70, 71, 75, 76,
512+ 79, 81, 84, 86, 89, 91, 94, 97, 98, 102, 104, 106, 106, 109, 109, 108,
513+ 72, 68, 68, 65, 65, 63, 63, 61, 62, 65, 65, 68, 69, 72, 73, 77,
514+ 77, 81, 81, 86, 87, 91, 91, 96, 97, 101, 102, 107, 107, 109, 110, 113,
515+ // Size 32x16
516+ 32, 31, 37, 42, 48, 49, 52, 54, 57, 63, 66, 67, 68, 69, 71, 72,
517+ 31, 31, 38, 42, 47, 47, 50, 52, 54, 60, 63, 64, 65, 66, 67, 68,
518+ 30, 32, 40, 42, 46, 45, 48, 50, 52, 57, 60, 62, 63, 65, 66, 68,
519+ 32, 34, 41, 44, 46, 45, 48, 49, 51, 57, 59, 61, 62, 63, 64, 65,
520+ 33, 36, 43, 45, 47, 46, 47, 49, 51, 56, 59, 60, 60, 62, 63, 65,
521+ 37, 40, 47, 47, 47, 45, 47, 48, 50, 54, 57, 58, 60, 61, 62, 63,
522+ 42, 43, 47, 48, 50, 49, 50, 52, 53, 57, 60, 58, 59, 60, 62, 63,
523+ 45, 44, 47, 49, 51, 51, 52, 54, 55, 59, 61, 61, 61, 60, 61, 61,
524+ 49, 46, 48, 50, 53, 53, 54, 55, 57, 60, 62, 63, 62, 63, 62, 62,
525+ 48, 46, 47, 50, 53, 56, 57, 59, 60, 64, 66, 65, 65, 64, 64, 65,
526+ 49, 45, 46, 49, 53, 58, 61, 62, 64, 67, 69, 67, 66, 66, 66, 65,
527+ 49, 46, 46, 49, 53, 59, 62, 64, 65, 69, 71, 70, 68, 68, 67, 68,
528+ 50, 46, 46, 50, 54, 59, 64, 65, 67, 71, 73, 72, 72, 70, 70, 69,
529+ 52, 48, 47, 50, 54, 61, 66, 68, 71, 75, 77, 74, 73, 73, 71, 72,
530+ 54, 50, 49, 52, 55, 62, 68, 71, 73, 78, 80, 78, 76, 74, 75, 73,
531+ 55, 51, 49, 52, 56, 63, 69, 72, 75, 80, 82, 80, 79, 78, 76, 77,
532+ 57, 52, 50, 53, 56, 64, 70, 73, 76, 82, 84, 82, 80, 80, 79, 77,
533+ 60, 54, 52, 55, 58, 65, 72, 75, 79, 85, 88, 86, 84, 82, 81, 81,
534+ 63, 57, 55, 58, 60, 67, 75, 78, 82, 89, 92, 88, 87, 85, 84, 81,
535+ 64, 58, 55, 58, 61, 68, 75, 78, 82, 89, 92, 90, 89, 87, 86, 86,
536+ 64, 59, 56, 58, 61, 68, 75, 79, 83, 90, 93, 95, 93, 91, 89, 87,
537+ 67, 61, 58, 60, 63, 69, 76, 79, 85, 92, 95, 96, 94, 92, 91, 91,
538+ 68, 62, 59, 60, 64, 71, 74, 78, 86, 91, 94, 96, 98, 96, 94, 91,
539+ 69, 62, 60, 60, 65, 70, 72, 79, 85, 88, 95, 98, 99, 98, 97, 96,
540+ 70, 63, 62, 60, 66, 69, 73, 81, 83, 89, 96, 97, 99, 101, 98, 97,
541+ 71, 64, 63, 61, 67, 68, 74, 79, 82, 90, 93, 98, 102, 102, 102, 101,
542+ 72, 65, 64, 62, 66, 68, 75, 78, 83, 89, 92, 100, 101, 103, 104, 102,
543+ 73, 66, 65, 63, 66, 69, 75, 76, 84, 87, 93, 98, 102, 105, 106, 107,
544+ 74, 67, 67, 64, 66, 70, 74, 77, 84, 86, 94, 96, 103, 105, 106, 107,
545+ 75, 68, 68, 65, 66, 71, 74, 78, 83, 87, 93, 96, 103, 105, 109, 109,
546+ 76, 69, 69, 66, 67, 72, 73, 80, 82, 88, 91, 97, 101, 107, 109, 110,
547+ 77, 70, 70, 67, 67, 73, 73, 81, 81, 90, 90, 99, 99, 108, 108, 113,
548+ // Size 4x16
549+ 31, 32, 36, 43, 46, 45, 46, 50, 52, 57, 59, 62, 63, 65, 67, 69,
550+ 49, 45, 46, 49, 53, 58, 59, 62, 64, 67, 68, 71, 69, 68, 70, 72,
551+ 63, 57, 56, 57, 60, 67, 71, 78, 82, 89, 90, 91, 89, 89, 86, 88,
552+ 69, 65, 62, 60, 63, 66, 70, 74, 80, 85, 91, 96, 101, 103, 105, 107,
553+ // Size 16x4
554+ 31, 49, 63, 69, 32, 45, 57, 65, 36, 46, 56, 62, 43, 49, 57, 60,
555+ 46, 53, 60, 63, 45, 58, 67, 66, 46, 59, 71, 70, 50, 62, 78, 74,
556+ 52, 64, 82, 80, 57, 67, 89, 85, 59, 68, 90, 91, 62, 71, 91, 96,
557+ 63, 69, 89, 101, 65, 68, 89, 103, 67, 70, 86, 105, 69, 72, 88, 107,
558+ // Size 8x32
559+ 32, 31, 30, 32, 33, 37, 42, 45, 49, 48, 49, 49, 50, 52, 54, 55,
560+ 57, 60, 63, 64, 64, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
561+ 37, 38, 40, 41, 43, 47, 47, 47, 48, 47, 46, 46, 46, 47, 49, 49,
562+ 50, 52, 55, 55, 56, 58, 59, 60, 62, 63, 64, 65, 67, 68, 69, 70,
563+ 48, 47, 46, 46, 47, 47, 50, 51, 53, 53, 53, 53, 54, 54, 55, 56,
564+ 56, 58, 60, 61, 61, 63, 64, 65, 66, 67, 66, 66, 66, 66, 67, 67,
565+ 52, 50, 48, 48, 47, 47, 50, 52, 54, 57, 61, 62, 64, 66, 68, 69,
566+ 70, 72, 75, 75, 75, 76, 74, 72, 73, 74, 75, 75, 74, 74, 73, 73,
567+ 57, 54, 52, 51, 51, 50, 53, 55, 57, 60, 64, 65, 67, 71, 73, 75,
568+ 76, 79, 82, 82, 83, 85, 86, 85, 83, 82, 83, 84, 84, 83, 82, 81,
569+ 66, 63, 60, 59, 59, 57, 60, 61, 62, 66, 69, 71, 73, 77, 80, 82,
570+ 84, 88, 92, 92, 93, 95, 94, 95, 96, 93, 92, 93, 94, 93, 91, 90,
571+ 68, 65, 63, 62, 60, 60, 59, 61, 62, 65, 66, 68, 72, 73, 76, 79,
572+ 80, 84, 87, 89, 93, 94, 98, 99, 99, 102, 101, 102, 103, 103, 101, 99,
573+ 71, 67, 66, 64, 63, 62, 62, 61, 62, 64, 66, 67, 70, 71, 75, 76,
574+ 79, 81, 84, 86, 89, 91, 94, 97, 98, 102, 104, 106, 106, 109, 109, 108,
575+ // Size 32x8
576+ 32, 37, 48, 52, 57, 66, 68, 71, 31, 38, 47, 50, 54, 63, 65, 67,
577+ 30, 40, 46, 48, 52, 60, 63, 66, 32, 41, 46, 48, 51, 59, 62, 64,
578+ 33, 43, 47, 47, 51, 59, 60, 63, 37, 47, 47, 47, 50, 57, 60, 62,
579+ 42, 47, 50, 50, 53, 60, 59, 62, 45, 47, 51, 52, 55, 61, 61, 61,
580+ 49, 48, 53, 54, 57, 62, 62, 62, 48, 47, 53, 57, 60, 66, 65, 64,
581+ 49, 46, 53, 61, 64, 69, 66, 66, 49, 46, 53, 62, 65, 71, 68, 67,
582+ 50, 46, 54, 64, 67, 73, 72, 70, 52, 47, 54, 66, 71, 77, 73, 71,
583+ 54, 49, 55, 68, 73, 80, 76, 75, 55, 49, 56, 69, 75, 82, 79, 76,
584+ 57, 50, 56, 70, 76, 84, 80, 79, 60, 52, 58, 72, 79, 88, 84, 81,
585+ 63, 55, 60, 75, 82, 92, 87, 84, 64, 55, 61, 75, 82, 92, 89, 86,
586+ 64, 56, 61, 75, 83, 93, 93, 89, 67, 58, 63, 76, 85, 95, 94, 91,
587+ 68, 59, 64, 74, 86, 94, 98, 94, 69, 60, 65, 72, 85, 95, 99, 97,
588+ 70, 62, 66, 73, 83, 96, 99, 98, 71, 63, 67, 74, 82, 93, 102, 102,
589+ 72, 64, 66, 75, 83, 92, 101, 104, 73, 65, 66, 75, 84, 93, 102, 106,
590+ 74, 67, 66, 74, 84, 94, 103, 106, 75, 68, 66, 74, 83, 93, 103, 109,
591+ 76, 69, 67, 73, 82, 91, 101, 109, 77, 70, 67, 73, 81, 90, 99, 108,
592+ ],
593+ ],
594+ // qm level 1
595+ [
596+ [// luma
597+ // Size 4x4
598+ 32, 41, 69, 92, 41, 63, 88, 103, 69, 88, 127, 140, 92, 103, 140, 184,
599+ // Size 8x8
600+ 32, 32, 37, 47, 62, 78, 90, 102, 32, 35, 39, 46, 58, 72, 84, 96,
601+ 37, 39, 51, 60, 71, 84, 93, 100, 47, 46, 60, 73, 87, 100, 106, 113,
602+ 62, 58, 71, 87, 105, 121, 129, 132, 78, 72, 84, 100, 121, 140, 148, 155,
603+ 90, 84, 93, 106, 129, 148, 169, 183, 102, 96, 100, 113, 132, 155, 183, 201,
604+ // Size 16x16
605+ 32, 31, 31, 32, 36, 39, 47, 54, 61, 71, 80, 86, 92, 98, 104, 111,
606+ 31, 32, 32, 33, 34, 37, 44, 50, 56, 65, 73, 79, 85, 91, 98, 105,
607+ 31, 32, 33, 34, 36, 39, 45, 50, 56, 64, 71, 77, 82, 88, 94, 100,
608+ 32, 33, 34, 36, 40, 42, 47, 51, 57, 65, 71, 76, 80, 85, 91, 98,
609+ 36, 34, 36, 40, 48, 50, 56, 60, 65, 73, 79, 84, 86, 90, 95, 98,
610+ 39, 37, 39, 42, 50, 54, 60, 65, 70, 78, 84, 89, 95, 96, 102, 105,
611+ 47, 44, 45, 47, 56, 60, 69, 75, 81, 89, 95, 100, 102, 104, 109, 112,
612+ 54, 50, 50, 51, 60, 65, 75, 82, 89, 97, 104, 109, 110, 114, 117, 121,
613+ 61, 56, 56, 57, 65, 70, 81, 89, 97, 106, 113, 119, 122, 126, 125, 130,
614+ 71, 65, 64, 65, 73, 78, 89, 97, 106, 117, 125, 131, 134, 134, 136, 141,
615+ 80, 73, 71, 71, 79, 84, 95, 104, 113, 125, 134, 140, 142, 145, 146, 152,
616+ 86, 79, 77, 76, 84, 89, 100, 109, 119, 131, 140, 147, 154, 157, 160, 165,
617+ 92, 85, 82, 80, 86, 95, 102, 110, 122, 134, 142, 154, 162, 168, 174, 178,
618+ 98, 91, 88, 85, 90, 96, 104, 114, 126, 134, 145, 157, 168, 176, 184, 193,
619+ 104, 98, 94, 91, 95, 102, 109, 117, 125, 136, 146, 160, 174, 184, 193, 201,
620+ 111, 105, 100, 98, 98, 105, 112, 121, 130, 141, 152, 165, 178, 193, 201, 210,
621+ // Size 32x32
622+ 32, 31, 31, 31, 31, 32, 32, 34, 36, 38, 39, 44, 47, 49, 54, 59,
623+ 61, 65, 71, 76, 80, 83, 86, 89, 92, 95, 98, 101, 104, 108, 111, 114,
624+ 31, 32, 32, 32, 32, 32, 33, 34, 35, 37, 38, 42, 45, 47, 51, 56,
625+ 58, 62, 68, 72, 76, 78, 82, 85, 88, 90, 93, 96, 99, 102, 105, 109,
626+ 31, 32, 32, 32, 32, 32, 33, 33, 34, 36, 37, 41, 44, 46, 50, 54,
627+ 56, 60, 65, 70, 73, 76, 79, 82, 85, 88, 91, 95, 98, 101, 105, 109,
628+ 31, 32, 32, 32, 32, 33, 33, 34, 35, 36, 38, 41, 44, 45, 49, 54,
629+ 56, 59, 65, 69, 72, 75, 78, 81, 84, 86, 89, 92, 95, 98, 101, 104,
630+ 31, 32, 32, 32, 33, 34, 34, 35, 36, 38, 39, 42, 45, 46, 50, 54,
631+ 56, 59, 64, 68, 71, 74, 77, 79, 82, 85, 88, 91, 94, 97, 100, 104,
632+ 32, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 45, 46, 49, 53,
633+ 55, 58, 63, 66, 69, 72, 74, 78, 81, 84, 87, 90, 93, 96, 99, 102,
634+ 32, 33, 33, 33, 34, 36, 36, 38, 40, 41, 42, 44, 47, 48, 51, 55,
635+ 57, 60, 65, 68, 71, 73, 76, 78, 80, 82, 85, 88, 91, 95, 98, 102,
636+ 34, 34, 33, 34, 35, 37, 38, 39, 42, 44, 45, 47, 50, 51, 54, 58,
637+ 60, 63, 68, 71, 74, 76, 79, 82, 85, 86, 87, 88, 90, 93, 96, 99,
638+ 36, 35, 34, 35, 36, 38, 40, 42, 48, 50, 50, 54, 56, 57, 60, 64,
639+ 65, 68, 73, 76, 79, 81, 84, 86, 86, 88, 90, 93, 95, 97, 98, 100,
640+ 38, 37, 36, 36, 38, 39, 41, 44, 50, 51, 52, 56, 58, 60, 63, 67,
641+ 68, 71, 76, 79, 82, 84, 87, 87, 90, 93, 94, 95, 96, 100, 103, 106,
642+ 39, 38, 37, 38, 39, 40, 42, 45, 50, 52, 54, 58, 60, 62, 65, 69,
643+ 70, 73, 78, 81, 84, 86, 89, 92, 95, 95, 96, 99, 102, 104, 105, 106,
644+ 44, 42, 41, 41, 42, 42, 44, 47, 54, 56, 58, 63, 66, 68, 71, 75,
645+ 77, 79, 84, 88, 90, 92, 95, 97, 97, 99, 102, 103, 103, 106, 109, 113,
646+ 47, 45, 44, 44, 45, 45, 47, 50, 56, 58, 60, 66, 69, 71, 75, 79,
647+ 81, 84, 89, 92, 95, 97, 100, 100, 102, 105, 104, 106, 109, 111, 112, 113,
648+ 49, 47, 46, 45, 46, 46, 48, 51, 57, 60, 62, 68, 71, 73, 77, 81,
649+ 83, 87, 92, 95, 98, 100, 103, 105, 107, 106, 109, 112, 112, 113, 117, 120,
650+ 54, 51, 50, 49, 50, 49, 51, 54, 60, 63, 65, 71, 75, 77, 82, 87,
651+ 89, 92, 97, 101, 104, 106, 109, 112, 110, 113, 114, 114, 117, 121, 121, 121,
652+ 59, 56, 54, 54, 54, 53, 55, 58, 64, 67, 69, 75, 79, 81, 87, 92,
653+ 94, 98, 103, 107, 110, 113, 116, 114, 117, 118, 117, 121, 122, 122, 125, 129,
654+ 61, 58, 56, 56, 56, 55, 57, 60, 65, 68, 70, 77, 81, 83, 89, 94,
655+ 97, 101, 106, 110, 113, 116, 119, 120, 122, 121, 126, 124, 125, 130, 130, 130,
656+ 65, 62, 60, 59, 59, 58, 60, 63, 68, 71, 73, 79, 84, 87, 92, 98,
657+ 101, 105, 111, 115, 118, 121, 124, 128, 125, 129, 128, 131, 133, 132, 135, 139,
658+ 71, 68, 65, 65, 64, 63, 65, 68, 73, 76, 78, 84, 89, 92, 97, 103,
659+ 106, 111, 117, 122, 125, 128, 131, 131, 134, 132, 134, 136, 136, 140, 141, 140,
660+ 76, 72, 70, 69, 68, 66, 68, 71, 76, 79, 81, 88, 92, 95, 101, 107,
661+ 110, 115, 122, 127, 130, 133, 136, 136, 138, 139, 141, 140, 145, 143, 146, 151,
662+ 80, 76, 73, 72, 71, 69, 71, 74, 79, 82, 84, 90, 95, 98, 104, 110,
663+ 113, 118, 125, 130, 134, 137, 140, 146, 142, 146, 145, 149, 146, 150, 152, 151,
664+ 83, 78, 76, 75, 74, 72, 73, 76, 81, 84, 86, 92, 97, 100, 106, 113,
665+ 116, 121, 128, 133, 137, 140, 144, 147, 152, 148, 154, 151, 156, 155, 156, 162,
666+ 86, 82, 79, 78, 77, 74, 76, 79, 84, 87, 89, 95, 100, 103, 109, 116,
667+ 119, 124, 131, 136, 140, 144, 147, 150, 154, 159, 157, 160, 160, 162, 165, 162,
668+ 89, 85, 82, 81, 79, 78, 78, 82, 86, 87, 92, 97, 100, 105, 112, 114,
669+ 120, 128, 131, 136, 146, 147, 150, 155, 156, 161, 166, 165, 167, 169, 169, 175,
670+ 92, 88, 85, 84, 82, 81, 80, 85, 86, 90, 95, 97, 102, 107, 110, 117,
671+ 122, 125, 134, 138, 142, 152, 154, 156, 162, 163, 168, 173, 174, 174, 178, 176,
672+ 95, 90, 88, 86, 85, 84, 82, 86, 88, 93, 95, 99, 105, 106, 113, 118,
673+ 121, 129, 132, 139, 146, 148, 159, 161, 163, 169, 170, 176, 180, 183, 181, 187,
674+ 98, 93, 91, 89, 88, 87, 85, 87, 90, 94, 96, 102, 104, 109, 114, 117,
675+ 126, 128, 134, 141, 145, 154, 157, 166, 168, 170, 176, 178, 184, 188, 193, 188,
676+ 101, 96, 95, 92, 91, 90, 88, 88, 93, 95, 99, 103, 106, 112, 114, 121,
677+ 124, 131, 136, 140, 149, 151, 160, 165, 173, 176, 178, 184, 186, 192, 196, 203,
678+ 104, 99, 98, 95, 94, 93, 91, 90, 95, 96, 102, 103, 109, 112, 117, 122,
679+ 125, 133, 136, 145, 146, 156, 160, 167, 174, 180, 184, 186, 193, 194, 201, 204,
680+ 108, 102, 101, 98, 97, 96, 95, 93, 97, 100, 104, 106, 111, 113, 121, 122,
681+ 130, 132, 140, 143, 150, 155, 162, 169, 174, 183, 188, 192, 194, 201, 202, 210,
682+ 111, 105, 105, 101, 100, 99, 98, 96, 98, 103, 105, 109, 112, 117, 121, 125,
683+ 130, 135, 141, 146, 152, 156, 165, 169, 178, 181, 193, 196, 201, 202, 210, 211,
684+ 114, 109, 109, 104, 104, 102, 102, 99, 100, 106, 106, 113, 113, 120, 121, 129,
685+ 130, 139, 140, 151, 151, 162, 162, 175, 176, 187, 188, 203, 204, 210, 211, 219,
686+ // Size 4x8
687+ 32, 33, 36, 46, 60, 75, 86, 98, 42, 42, 56, 67, 79, 92, 95, 105,
688+ 69, 64, 77, 93, 112, 130, 136, 136, 88, 83, 88, 105, 122, 144, 167, 177,
689+ // Size 8x4
690+ 32, 42, 69, 88, 33, 42, 64, 83, 36, 56, 77, 88, 46, 67, 93, 105,
691+ 60, 79, 112, 122, 75, 92, 130, 144, 86, 95, 136, 167, 98, 105, 136, 177,
692+ // Size 8x16
693+ 32, 31, 32, 32, 36, 39, 47, 53, 61, 71, 79, 86, 92, 98, 104, 110,
694+ 32, 32, 34, 35, 37, 40, 45, 50, 56, 64, 70, 76, 82, 88, 94, 100,
695+ 36, 35, 36, 40, 48, 50, 56, 60, 65, 73, 79, 84, 89, 93, 95, 98,
696+ 47, 44, 45, 47, 56, 60, 69, 75, 81, 89, 95, 100, 101, 108, 110, 111,
697+ 65, 60, 59, 60, 68, 73, 84, 92, 100, 111, 118, 124, 121, 124, 129, 127,
698+ 79, 72, 71, 71, 78, 84, 95, 103, 113, 125, 133, 140, 148, 141, 151, 147,
699+ 90, 84, 80, 78, 83, 91, 101, 108, 116, 129, 142, 153, 157, 163, 171, 169,
700+ 96, 90, 87, 85, 87, 94, 101, 110, 118, 129, 138, 150, 161, 174, 181, 188,
701+ // Size 16x8
702+ 32, 32, 36, 47, 65, 79, 90, 96, 31, 32, 35, 44, 60, 72, 84, 90,
703+ 32, 34, 36, 45, 59, 71, 80, 87, 32, 35, 40, 47, 60, 71, 78, 85,
704+ 36, 37, 48, 56, 68, 78, 83, 87, 39, 40, 50, 60, 73, 84, 91, 94,
705+ 47, 45, 56, 69, 84, 95, 101, 101, 53, 50, 60, 75, 92, 103, 108, 110,
706+ 61, 56, 65, 81, 100, 113, 116, 118, 71, 64, 73, 89, 111, 125, 129, 129,
707+ 79, 70, 79, 95, 118, 133, 142, 138, 86, 76, 84, 100, 124, 140, 153, 150,
708+ 92, 82, 89, 101, 121, 148, 157, 161, 98, 88, 93, 108, 124, 141, 163, 174,
709+ 104, 94, 95, 110, 129, 151, 171, 181, 110, 100, 98, 111, 127, 147, 169, 188,
710+ // Size 16x32
711+ 32, 31, 31, 31, 32, 32, 32, 34, 36, 38, 39, 44, 47, 49, 53, 58,
712+ 61, 65, 71, 76, 79, 82, 86, 89, 92, 95, 98, 101, 104, 107, 110, 114,
713+ 31, 32, 32, 32, 32, 33, 33, 34, 34, 36, 37, 41, 44, 46, 49, 54,
714+ 56, 60, 65, 69, 72, 75, 78, 81, 84, 86, 89, 92, 95, 98, 101, 104,
715+ 32, 32, 32, 33, 34, 35, 35, 36, 37, 39, 40, 42, 45, 47, 50, 54,
716+ 56, 59, 64, 68, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 104,
717+ 32, 33, 33, 33, 34, 36, 36, 38, 40, 41, 42, 45, 47, 48, 51, 55,
718+ 57, 60, 65, 69, 71, 74, 77, 78, 80, 83, 85, 88, 91, 94, 97, 100,
719+ 36, 35, 35, 35, 36, 38, 40, 42, 48, 49, 50, 53, 56, 57, 60, 63,
720+ 65, 68, 73, 76, 79, 81, 84, 87, 89, 92, 93, 94, 95, 96, 98, 100,
721+ 44, 42, 41, 41, 42, 42, 44, 48, 54, 56, 58, 63, 66, 67, 71, 75,
722+ 77, 79, 84, 88, 90, 92, 95, 95, 95, 95, 95, 98, 101, 105, 108, 111,
723+ 47, 45, 44, 44, 45, 45, 47, 50, 56, 58, 60, 66, 69, 71, 75, 79,
724+ 81, 84, 89, 92, 95, 97, 100, 99, 101, 105, 108, 110, 110, 110, 111, 111,
725+ 53, 51, 49, 49, 50, 49, 51, 54, 60, 63, 65, 71, 75, 77, 82, 87,
726+ 89, 92, 97, 101, 104, 106, 109, 112, 116, 114, 113, 112, 115, 119, 123, 126,
727+ 65, 62, 60, 59, 59, 58, 60, 63, 68, 71, 73, 79, 84, 86, 92, 98,
728+ 100, 105, 111, 115, 118, 121, 124, 124, 121, 120, 124, 128, 129, 128, 127, 127,
729+ 73, 69, 67, 66, 65, 64, 66, 69, 74, 77, 79, 85, 90, 93, 99, 105,
730+ 107, 112, 119, 123, 127, 130, 133, 130, 132, 136, 136, 133, 132, 136, 141, 145,
731+ 79, 75, 72, 71, 71, 69, 71, 73, 78, 81, 84, 90, 95, 97, 103, 110,
732+ 113, 118, 125, 130, 133, 136, 140, 145, 148, 143, 141, 146, 151, 149, 147, 145,
733+ 87, 83, 80, 79, 78, 76, 76, 80, 84, 86, 90, 96, 99, 103, 111, 114,
734+ 118, 126, 130, 134, 143, 146, 147, 152, 151, 155, 160, 158, 154, 156, 161, 166,
735+ 90, 86, 84, 82, 80, 80, 78, 82, 83, 88, 91, 94, 101, 103, 108, 114,
736+ 116, 124, 129, 134, 142, 145, 153, 156, 157, 163, 163, 166, 171, 173, 169, 166,
737+ 93, 88, 87, 84, 83, 83, 81, 81, 86, 88, 92, 96, 98, 105, 107, 113,
738+ 117, 122, 129, 131, 141, 144, 151, 157, 163, 167, 169, 175, 175, 177, 183, 189,
739+ 96, 91, 90, 87, 87, 86, 85, 84, 87, 90, 94, 96, 101, 102, 110, 111,
740+ 118, 121, 129, 132, 138, 144, 150, 156, 161, 171, 174, 179, 181, 188, 188, 190,
741+ 99, 94, 94, 90, 90, 88, 89, 86, 87, 93, 93, 99, 99, 106, 107, 115,
742+ 116, 124, 125, 135, 136, 145, 146, 158, 159, 170, 171, 185, 186, 192, 193, 201,
743+ // Size 32x16
744+ 32, 31, 32, 32, 36, 44, 47, 53, 65, 73, 79, 87, 90, 93, 96, 99,
745+ 31, 32, 32, 33, 35, 42, 45, 51, 62, 69, 75, 83, 86, 88, 91, 94,
746+ 31, 32, 32, 33, 35, 41, 44, 49, 60, 67, 72, 80, 84, 87, 90, 94,
747+ 31, 32, 33, 33, 35, 41, 44, 49, 59, 66, 71, 79, 82, 84, 87, 90,
748+ 32, 32, 34, 34, 36, 42, 45, 50, 59, 65, 71, 78, 80, 83, 87, 90,
749+ 32, 33, 35, 36, 38, 42, 45, 49, 58, 64, 69, 76, 80, 83, 86, 88,
750+ 32, 33, 35, 36, 40, 44, 47, 51, 60, 66, 71, 76, 78, 81, 85, 89,
751+ 34, 34, 36, 38, 42, 48, 50, 54, 63, 69, 73, 80, 82, 81, 84, 86,
752+ 36, 34, 37, 40, 48, 54, 56, 60, 68, 74, 78, 84, 83, 86, 87, 87,
753+ 38, 36, 39, 41, 49, 56, 58, 63, 71, 77, 81, 86, 88, 88, 90, 93,
754+ 39, 37, 40, 42, 50, 58, 60, 65, 73, 79, 84, 90, 91, 92, 94, 93,
755+ 44, 41, 42, 45, 53, 63, 66, 71, 79, 85, 90, 96, 94, 96, 96, 99,
756+ 47, 44, 45, 47, 56, 66, 69, 75, 84, 90, 95, 99, 101, 98, 101, 99,
757+ 49, 46, 47, 48, 57, 67, 71, 77, 86, 93, 97, 103, 103, 105, 102, 106,
758+ 53, 49, 50, 51, 60, 71, 75, 82, 92, 99, 103, 111, 108, 107, 110, 107,
759+ 58, 54, 54, 55, 63, 75, 79, 87, 98, 105, 110, 114, 114, 113, 111, 115,
760+ 61, 56, 56, 57, 65, 77, 81, 89, 100, 107, 113, 118, 116, 117, 118, 116,
761+ 65, 60, 59, 60, 68, 79, 84, 92, 105, 112, 118, 126, 124, 122, 121, 124,
762+ 71, 65, 64, 65, 73, 84, 89, 97, 111, 119, 125, 130, 129, 129, 129, 125,
763+ 76, 69, 68, 69, 76, 88, 92, 101, 115, 123, 130, 134, 134, 131, 132, 135,
764+ 79, 72, 70, 71, 79, 90, 95, 104, 118, 127, 133, 143, 142, 141, 138, 136,
765+ 82, 75, 73, 74, 81, 92, 97, 106, 121, 130, 136, 146, 145, 144, 144, 145,
766+ 86, 78, 76, 77, 84, 95, 100, 109, 124, 133, 140, 147, 153, 151, 150, 146,
767+ 89, 81, 79, 78, 87, 95, 99, 112, 124, 130, 145, 152, 156, 157, 156, 158,
768+ 92, 84, 82, 80, 89, 95, 101, 116, 121, 132, 148, 151, 157, 163, 161, 159,
769+ 95, 86, 85, 83, 92, 95, 105, 114, 120, 136, 143, 155, 163, 167, 171, 170,
770+ 98, 89, 88, 85, 93, 95, 108, 113, 124, 136, 141, 160, 163, 169, 174, 171,
771+ 101, 92, 91, 88, 94, 98, 110, 112, 128, 133, 146, 158, 166, 175, 179, 185,
772+ 104, 95, 94, 91, 95, 101, 110, 115, 129, 132, 151, 154, 171, 175, 181, 186,
773+ 107, 98, 97, 94, 96, 105, 110, 119, 128, 136, 149, 156, 173, 177, 188, 192,
774+ 110, 101, 100, 97, 98, 108, 111, 123, 127, 141, 147, 161, 169, 183, 188, 193,
775+ 114, 104, 104, 100, 100, 111, 111, 126, 127, 145, 145, 166, 166, 189, 190, 201,
776+ // Size 4x16
777+ 31, 32, 32, 33, 34, 37, 44, 49, 56, 65, 72, 78, 84, 89, 95, 101,
778+ 44, 41, 42, 44, 54, 58, 66, 71, 77, 84, 90, 95, 95, 95, 101, 108,
779+ 73, 67, 65, 66, 74, 79, 90, 99, 107, 119, 127, 133, 132, 136, 132, 141,
780+ 93, 87, 83, 81, 86, 92, 98, 107, 117, 129, 141, 151, 163, 169, 175, 183,
781+ // Size 16x4
782+ 31, 44, 73, 93, 32, 41, 67, 87, 32, 42, 65, 83, 33, 44, 66, 81,
783+ 34, 54, 74, 86, 37, 58, 79, 92, 44, 66, 90, 98, 49, 71, 99, 107,
784+ 56, 77, 107, 117, 65, 84, 119, 129, 72, 90, 127, 141, 78, 95, 133, 151,
785+ 84, 95, 132, 163, 89, 95, 136, 169, 95, 101, 132, 175, 101, 108, 141, 183,
786+ // Size 8x32
787+ 32, 31, 31, 31, 32, 32, 32, 34, 36, 38, 39, 44, 47, 49, 53, 58,
788+ 61, 65, 71, 76, 79, 82, 86, 89, 92, 95, 98, 101, 104, 107, 110, 114,
789+ 32, 32, 32, 33, 34, 35, 35, 36, 37, 39, 40, 42, 45, 47, 50, 54,
790+ 56, 59, 64, 68, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 104,
791+ 36, 35, 35, 35, 36, 38, 40, 42, 48, 49, 50, 53, 56, 57, 60, 63,
792+ 65, 68, 73, 76, 79, 81, 84, 87, 89, 92, 93, 94, 95, 96, 98, 100,
793+ 47, 45, 44, 44, 45, 45, 47, 50, 56, 58, 60, 66, 69, 71, 75, 79,
794+ 81, 84, 89, 92, 95, 97, 100, 99, 101, 105, 108, 110, 110, 110, 111, 111,
795+ 65, 62, 60, 59, 59, 58, 60, 63, 68, 71, 73, 79, 84, 86, 92, 98,
796+ 100, 105, 111, 115, 118, 121, 124, 124, 121, 120, 124, 128, 129, 128, 127, 127,
797+ 79, 75, 72, 71, 71, 69, 71, 73, 78, 81, 84, 90, 95, 97, 103, 110,
798+ 113, 118, 125, 130, 133, 136, 140, 145, 148, 143, 141, 146, 151, 149, 147, 145,
799+ 90, 86, 84, 82, 80, 80, 78, 82, 83, 88, 91, 94, 101, 103, 108, 114,
800+ 116, 124, 129, 134, 142, 145, 153, 156, 157, 163, 163, 166, 171, 173, 169, 166,
801+ 96, 91, 90, 87, 87, 86, 85, 84, 87, 90, 94, 96, 101, 102, 110, 111,
802+ 118, 121, 129, 132, 138, 144, 150, 156, 161, 171, 174, 179, 181, 188, 188, 190,
803+ // Size 32x8
804+ 32, 32, 36, 47, 65, 79, 90, 96, 31, 32, 35, 45, 62, 75, 86, 91,
805+ 31, 32, 35, 44, 60, 72, 84, 90, 31, 33, 35, 44, 59, 71, 82, 87,
806+ 32, 34, 36, 45, 59, 71, 80, 87, 32, 35, 38, 45, 58, 69, 80, 86,
807+ 32, 35, 40, 47, 60, 71, 78, 85, 34, 36, 42, 50, 63, 73, 82, 84,
808+ 36, 37, 48, 56, 68, 78, 83, 87, 38, 39, 49, 58, 71, 81, 88, 90,
809+ 39, 40, 50, 60, 73, 84, 91, 94, 44, 42, 53, 66, 79, 90, 94, 96,
810+ 47, 45, 56, 69, 84, 95, 101, 101, 49, 47, 57, 71, 86, 97, 103, 102,
811+ 53, 50, 60, 75, 92, 103, 108, 110, 58, 54, 63, 79, 98, 110, 114, 111,
812+ 61, 56, 65, 81, 100, 113, 116, 118, 65, 59, 68, 84, 105, 118, 124, 121,
813+ 71, 64, 73, 89, 111, 125, 129, 129, 76, 68, 76, 92, 115, 130, 134, 132,
814+ 79, 70, 79, 95, 118, 133, 142, 138, 82, 73, 81, 97, 121, 136, 145, 144,
815+ 86, 76, 84, 100, 124, 140, 153, 150, 89, 79, 87, 99, 124, 145, 156, 156,
816+ 92, 82, 89, 101, 121, 148, 157, 161, 95, 85, 92, 105, 120, 143, 163, 171,
817+ 98, 88, 93, 108, 124, 141, 163, 174, 101, 91, 94, 110, 128, 146, 166, 179,
818+ 104, 94, 95, 110, 129, 151, 171, 181, 107, 97, 96, 110, 128, 149, 173, 188,
819+ 110, 100, 98, 111, 127, 147, 169, 188, 114, 104, 100, 111, 127, 145, 166, 190,
820+ ],
821+ [// chroma
822+ // Size 4x4
823+ 33, 45, 56, 64, 45, 58, 66, 69, 56, 66, 86, 87, 64, 69, 87, 105,
824+ // Size 8x8
825+ 31, 38, 47, 48, 54, 61, 66, 69, 38, 47, 47, 46, 50, 55, 61, 65,
826+ 47, 47, 53, 55, 58, 63, 65, 66, 48, 46, 55, 62, 67, 72, 73, 73,
827+ 54, 50, 58, 67, 76, 83, 84, 82, 61, 55, 63, 72, 83, 91, 92, 92,
828+ 66, 61, 65, 73, 84, 92, 101, 103, 69, 65, 66, 73, 82, 92, 103, 109,
829+ // Size 16x16
830+ 32, 30, 33, 38, 49, 48, 50, 52, 55, 60, 63, 66, 68, 70, 72, 74,
831+ 30, 31, 35, 41, 46, 46, 46, 48, 51, 55, 58, 60, 63, 65, 68, 70,
832+ 33, 35, 39, 44, 47, 46, 46, 47, 50, 53, 56, 58, 60, 62, 65, 67,
833+ 38, 41, 44, 47, 49, 48, 47, 48, 50, 53, 55, 58, 58, 60, 62, 65,
834+ 49, 46, 47, 49, 53, 53, 54, 54, 56, 58, 60, 62, 62, 63, 64, 64,
835+ 48, 46, 46, 48, 53, 54, 56, 57, 59, 61, 63, 65, 67, 66, 68, 68,
836+ 50, 46, 46, 47, 54, 56, 61, 63, 65, 68, 70, 72, 71, 71, 72, 72,
837+ 52, 48, 47, 48, 54, 57, 63, 66, 69, 72, 75, 76, 75, 76, 76, 76,
838+ 55, 51, 50, 50, 56, 59, 65, 69, 73, 77, 79, 81, 81, 81, 80, 80,
839+ 60, 55, 53, 53, 58, 61, 68, 72, 77, 82, 85, 87, 87, 85, 84, 85,
840+ 63, 58, 56, 55, 60, 63, 70, 75, 79, 85, 89, 91, 91, 90, 89, 90,
841+ 66, 60, 58, 58, 62, 65, 72, 76, 81, 87, 91, 94, 96, 95, 95, 95,
842+ 68, 63, 60, 58, 62, 67, 71, 75, 81, 87, 91, 96, 99, 100, 100, 100,
843+ 70, 65, 62, 60, 63, 66, 71, 76, 81, 85, 90, 95, 100, 103, 104, 105,
844+ 72, 68, 65, 62, 64, 68, 72, 76, 80, 84, 89, 95, 100, 104, 107, 108,
845+ 74, 70, 67, 65, 64, 68, 72, 76, 80, 85, 90, 95, 100, 105, 108, 111,
846+ // Size 32x32
847+ 32, 31, 30, 31, 33, 36, 38, 41, 49, 49, 48, 49, 50, 51, 52, 54,
848+ 55, 57, 60, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
849+ 31, 31, 31, 32, 34, 38, 40, 42, 47, 47, 47, 47, 48, 48, 50, 52,
850+ 53, 54, 57, 59, 60, 61, 63, 64, 65, 66, 67, 67, 68, 69, 70, 71,
851+ 30, 31, 31, 32, 35, 39, 41, 42, 46, 46, 46, 45, 46, 47, 48, 50,
852+ 51, 52, 55, 57, 58, 59, 60, 62, 63, 64, 65, 67, 68, 69, 70, 71,
853+ 31, 32, 32, 33, 36, 40, 41, 43, 46, 46, 45, 45, 46, 46, 47, 49,
854+ 50, 51, 54, 56, 57, 58, 59, 61, 62, 63, 63, 64, 65, 66, 67, 68,
855+ 33, 34, 35, 36, 39, 43, 44, 45, 47, 46, 46, 45, 46, 47, 47, 49,
856+ 50, 51, 53, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68,
857+ 36, 38, 39, 40, 43, 47, 47, 47, 48, 47, 46, 45, 46, 46, 47, 48,
858+ 49, 50, 52, 53, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65, 66, 66,
859+ 38, 40, 41, 41, 44, 47, 47, 48, 49, 48, 48, 47, 47, 47, 48, 49,
860+ 50, 51, 53, 54, 55, 56, 58, 58, 58, 59, 60, 61, 62, 64, 65, 66,
861+ 41, 42, 42, 43, 45, 47, 48, 48, 50, 50, 49, 49, 50, 50, 50, 52,
862+ 52, 53, 55, 56, 57, 58, 59, 60, 61, 61, 61, 61, 62, 63, 63, 64,
863+ 49, 47, 46, 46, 47, 48, 49, 50, 53, 53, 53, 53, 54, 54, 54, 55,
864+ 56, 56, 58, 59, 60, 61, 62, 63, 62, 62, 63, 64, 64, 64, 64, 64,
865+ 49, 47, 46, 46, 46, 47, 48, 50, 53, 53, 54, 55, 55, 55, 56, 57,
866+ 58, 58, 60, 61, 62, 63, 64, 64, 64, 65, 65, 65, 65, 66, 67, 68,
867+ 48, 47, 46, 45, 46, 46, 48, 49, 53, 54, 54, 55, 56, 56, 57, 58,
868+ 59, 60, 61, 63, 63, 64, 65, 66, 67, 66, 66, 67, 68, 68, 68, 68,
869+ 49, 47, 45, 45, 45, 45, 47, 49, 53, 55, 55, 58, 59, 60, 61, 62,
870+ 63, 63, 65, 66, 67, 68, 69, 69, 68, 68, 69, 69, 69, 69, 70, 71,
871+ 50, 48, 46, 46, 46, 46, 47, 50, 54, 55, 56, 59, 61, 61, 63, 64,
872+ 65, 66, 68, 69, 70, 71, 72, 71, 71, 72, 71, 71, 72, 72, 72, 71,
873+ 51, 48, 47, 46, 47, 46, 47, 50, 54, 55, 56, 60, 61, 62, 64, 66,
874+ 66, 67, 69, 70, 71, 72, 73, 73, 74, 73, 73, 74, 73, 73, 74, 75,
875+ 52, 50, 48, 47, 47, 47, 48, 50, 54, 56, 57, 61, 63, 64, 66, 68,
876+ 69, 70, 72, 74, 75, 75, 76, 77, 75, 76, 76, 75, 76, 77, 76, 75,
877+ 54, 52, 50, 49, 49, 48, 49, 52, 55, 57, 58, 62, 64, 66, 68, 71,
878+ 72, 73, 75, 77, 78, 79, 80, 78, 79, 78, 77, 78, 78, 77, 78, 79,
879+ 55, 53, 51, 50, 50, 49, 50, 52, 56, 58, 59, 63, 65, 66, 69, 72,
880+ 73, 74, 77, 78, 79, 80, 81, 81, 81, 80, 81, 80, 80, 81, 80, 79,
881+ 57, 54, 52, 51, 51, 50, 51, 53, 56, 58, 60, 63, 66, 67, 70, 73,
882+ 74, 76, 79, 80, 82, 83, 84, 85, 83, 84, 83, 83, 83, 82, 82, 83,
883+ 60, 57, 55, 54, 53, 52, 53, 55, 58, 60, 61, 65, 68, 69, 72, 75,
884+ 77, 79, 82, 84, 85, 86, 87, 86, 87, 85, 85, 85, 84, 86, 85, 84,
885+ 62, 59, 57, 56, 55, 53, 54, 56, 59, 61, 63, 66, 69, 70, 74, 77,
886+ 78, 80, 84, 86, 87, 88, 90, 89, 89, 88, 88, 87, 88, 87, 87, 88,
887+ 63, 60, 58, 57, 56, 54, 55, 57, 60, 62, 63, 67, 70, 71, 75, 78,
888+ 79, 82, 85, 87, 89, 90, 91, 93, 91, 91, 90, 91, 89, 90, 90, 89,
889+ 65, 61, 59, 58, 57, 55, 56, 58, 61, 63, 64, 68, 71, 72, 75, 79,
890+ 80, 83, 86, 88, 90, 91, 93, 94, 95, 92, 94, 92, 93, 92, 91, 93,
891+ 66, 63, 60, 59, 58, 56, 58, 59, 62, 64, 65, 69, 72, 73, 76, 80,
892+ 81, 84, 87, 90, 91, 93, 94, 95, 96, 97, 95, 95, 95, 95, 95, 93,
893+ 67, 64, 62, 61, 59, 58, 58, 60, 63, 64, 66, 69, 71, 73, 77, 78,
894+ 81, 85, 86, 89, 93, 94, 95, 97, 97, 98, 99, 97, 97, 97, 96, 98,
895+ 68, 65, 63, 62, 60, 59, 58, 61, 62, 64, 67, 68, 71, 74, 75, 79,
896+ 81, 83, 87, 89, 91, 95, 96, 97, 99, 98, 100, 100, 100, 99, 100, 98,
897+ 69, 66, 64, 63, 61, 61, 59, 61, 62, 65, 66, 68, 72, 73, 76, 78,
898+ 80, 84, 85, 88, 91, 92, 97, 98, 98, 101, 100, 102, 102, 103, 101, 102,
899+ 70, 67, 65, 63, 62, 62, 60, 61, 63, 65, 66, 69, 71, 73, 76, 77,
900+ 81, 83, 85, 88, 90, 94, 95, 99, 100, 100, 103, 102, 104, 104, 105, 103,
901+ 71, 67, 67, 64, 63, 63, 61, 61, 64, 65, 67, 69, 71, 74, 75, 78,
902+ 80, 83, 85, 87, 91, 92, 95, 97, 100, 102, 102, 105, 104, 106, 106, 108,
903+ 72, 68, 68, 65, 65, 64, 62, 62, 64, 65, 68, 69, 72, 73, 76, 78,
904+ 80, 83, 84, 88, 89, 93, 95, 97, 100, 102, 104, 104, 107, 106, 108, 108,
905+ 73, 69, 69, 66, 66, 65, 64, 63, 64, 66, 68, 69, 72, 73, 77, 77,
906+ 81, 82, 86, 87, 90, 92, 95, 97, 99, 103, 104, 106, 106, 109, 108, 110,
907+ 74, 70, 70, 67, 67, 66, 65, 63, 64, 67, 68, 70, 72, 74, 76, 78,
908+ 80, 82, 85, 87, 90, 91, 95, 96, 100, 101, 105, 106, 108, 108, 111, 110,
909+ 75, 71, 71, 68, 68, 66, 66, 64, 64, 68, 68, 71, 71, 75, 75, 79,
910+ 79, 83, 84, 88, 89, 93, 93, 98, 98, 102, 103, 108, 108, 110, 110, 113,
911+ // Size 4x8
912+ 31, 40, 46, 47, 52, 59, 63, 66, 47, 45, 55, 60, 64, 68, 66, 69,
913+ 57, 52, 61, 70, 79, 87, 88, 85, 65, 61, 63, 72, 81, 90, 99, 102,
914+ // Size 8x4
915+ 31, 47, 57, 65, 40, 45, 52, 61, 46, 55, 61, 63, 47, 60, 70, 72,
916+ 52, 64, 79, 81, 59, 68, 87, 90, 63, 66, 88, 99, 66, 69, 85, 102,
917+ // Size 8x16
918+ 32, 30, 33, 39, 49, 48, 50, 52, 55, 60, 63, 66, 68, 70, 72, 74,
919+ 35, 38, 41, 46, 48, 46, 46, 47, 49, 53, 55, 58, 60, 62, 65, 67,
920+ 48, 46, 47, 48, 53, 53, 54, 54, 56, 58, 60, 62, 64, 65, 65, 65,
921+ 50, 46, 46, 47, 54, 56, 61, 63, 65, 68, 70, 72, 71, 73, 72, 71,
922+ 57, 52, 51, 51, 57, 60, 66, 71, 74, 79, 82, 84, 81, 81, 82, 79,
923+ 63, 58, 56, 55, 60, 64, 70, 75, 79, 85, 89, 91, 94, 89, 92, 89,
924+ 68, 63, 60, 58, 61, 65, 71, 75, 79, 85, 91, 95, 97, 98, 100, 98,
925+ 70, 65, 63, 61, 61, 65, 69, 74, 78, 82, 87, 91, 96, 101, 103, 105,
926+ // Size 16x8
927+ 32, 35, 48, 50, 57, 63, 68, 70, 30, 38, 46, 46, 52, 58, 63, 65,
928+ 33, 41, 47, 46, 51, 56, 60, 63, 39, 46, 48, 47, 51, 55, 58, 61,
929+ 49, 48, 53, 54, 57, 60, 61, 61, 48, 46, 53, 56, 60, 64, 65, 65,
930+ 50, 46, 54, 61, 66, 70, 71, 69, 52, 47, 54, 63, 71, 75, 75, 74,
931+ 55, 49, 56, 65, 74, 79, 79, 78, 60, 53, 58, 68, 79, 85, 85, 82,
932+ 63, 55, 60, 70, 82, 89, 91, 87, 66, 58, 62, 72, 84, 91, 95, 91,
933+ 68, 60, 64, 71, 81, 94, 97, 96, 70, 62, 65, 73, 81, 89, 98, 101,
934+ 72, 65, 65, 72, 82, 92, 100, 103, 74, 67, 65, 71, 79, 89, 98, 105,
935+ // Size 16x32
936+ 32, 31, 30, 31, 33, 37, 39, 42, 49, 48, 48, 49, 50, 51, 52, 54,
937+ 55, 57, 60, 62, 63, 64, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
938+ 31, 31, 32, 33, 36, 40, 41, 43, 46, 46, 46, 45, 46, 47, 48, 50,
939+ 51, 52, 54, 56, 57, 59, 60, 61, 62, 63, 64, 65, 65, 66, 67, 68,
940+ 35, 37, 38, 38, 41, 45, 46, 46, 48, 47, 46, 45, 46, 47, 47, 49,
941+ 49, 50, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68,
942+ 38, 40, 40, 41, 44, 47, 47, 48, 49, 48, 48, 47, 48, 48, 48, 50,
943+ 50, 51, 53, 55, 56, 57, 58, 58, 59, 60, 60, 61, 62, 63, 64, 65,
944+ 48, 47, 46, 46, 47, 47, 48, 50, 53, 53, 53, 53, 54, 54, 54, 55,
945+ 56, 56, 58, 60, 60, 61, 62, 63, 64, 65, 65, 65, 65, 65, 65, 65,
946+ 49, 47, 45, 45, 46, 45, 47, 49, 53, 55, 56, 58, 59, 60, 61, 62,
947+ 63, 64, 65, 66, 67, 68, 69, 68, 67, 66, 66, 67, 68, 69, 70, 71,
948+ 50, 48, 46, 46, 46, 46, 47, 50, 54, 55, 56, 59, 61, 61, 63, 65,
949+ 65, 66, 68, 69, 70, 71, 72, 71, 71, 72, 73, 73, 72, 72, 71, 71,
950+ 52, 50, 48, 48, 47, 47, 48, 50, 54, 56, 57, 61, 63, 64, 66, 68,
951+ 69, 70, 72, 74, 75, 75, 76, 78, 79, 77, 76, 74, 75, 76, 77, 78,
952+ 57, 54, 52, 52, 51, 50, 51, 53, 57, 58, 60, 64, 66, 68, 71, 73,
953+ 74, 76, 79, 81, 82, 83, 84, 83, 81, 80, 81, 82, 82, 81, 79, 78,
954+ 61, 57, 55, 55, 54, 52, 54, 56, 59, 61, 62, 66, 68, 70, 73, 76,
955+ 77, 79, 82, 84, 86, 87, 88, 86, 86, 88, 87, 85, 83, 85, 86, 87,
956+ 63, 60, 58, 57, 56, 54, 55, 57, 60, 62, 64, 67, 70, 71, 75, 78,
957+ 79, 82, 85, 87, 89, 90, 91, 93, 94, 91, 89, 90, 92, 90, 89, 87,
958+ 67, 63, 61, 60, 59, 57, 57, 60, 63, 64, 66, 69, 71, 73, 77, 79,
959+ 81, 85, 87, 88, 92, 93, 94, 96, 95, 96, 97, 95, 93, 93, 94, 96,
960+ 68, 64, 63, 61, 60, 59, 58, 60, 61, 64, 65, 67, 71, 72, 75, 78,
961+ 79, 83, 85, 87, 91, 92, 95, 96, 97, 99, 98, 99, 100, 100, 98, 96,
962+ 69, 65, 64, 62, 61, 61, 59, 59, 62, 63, 65, 67, 68, 72, 73, 76,
963+ 78, 81, 84, 85, 89, 90, 93, 96, 98, 99, 100, 102, 102, 102, 103, 105,
964+ 70, 66, 65, 63, 63, 62, 61, 60, 61, 63, 65, 66, 69, 70, 74, 74,
965+ 78, 79, 82, 84, 87, 89, 91, 94, 96, 100, 101, 103, 103, 105, 105, 105,
966+ 71, 67, 67, 64, 64, 62, 62, 60, 61, 64, 64, 67, 67, 71, 71, 75,
967+ 75, 79, 80, 84, 84, 89, 89, 94, 94, 98, 99, 104, 104, 106, 106, 109,
968+ // Size 32x16
969+ 32, 31, 35, 38, 48, 49, 50, 52, 57, 61, 63, 67, 68, 69, 70, 71,
970+ 31, 31, 37, 40, 47, 47, 48, 50, 54, 57, 60, 63, 64, 65, 66, 67,
971+ 30, 32, 38, 40, 46, 45, 46, 48, 52, 55, 58, 61, 63, 64, 65, 67,
972+ 31, 33, 38, 41, 46, 45, 46, 48, 52, 55, 57, 60, 61, 62, 63, 64,
973+ 33, 36, 41, 44, 47, 46, 46, 47, 51, 54, 56, 59, 60, 61, 63, 64,
974+ 37, 40, 45, 47, 47, 45, 46, 47, 50, 52, 54, 57, 59, 61, 62, 62,
975+ 39, 41, 46, 47, 48, 47, 47, 48, 51, 54, 55, 57, 58, 59, 61, 62,
976+ 42, 43, 46, 48, 50, 49, 50, 50, 53, 56, 57, 60, 60, 59, 60, 60,
977+ 49, 46, 48, 49, 53, 53, 54, 54, 57, 59, 60, 63, 61, 62, 61, 61,
978+ 48, 46, 47, 48, 53, 55, 55, 56, 58, 61, 62, 64, 64, 63, 63, 64,
979+ 48, 46, 46, 48, 53, 56, 56, 57, 60, 62, 64, 66, 65, 65, 65, 64,
980+ 49, 45, 45, 47, 53, 58, 59, 61, 64, 66, 67, 69, 67, 67, 66, 67,
981+ 50, 46, 46, 48, 54, 59, 61, 63, 66, 68, 70, 71, 71, 68, 69, 67,
982+ 51, 47, 47, 48, 54, 60, 61, 64, 68, 70, 71, 73, 72, 72, 70, 71,
983+ 52, 48, 47, 48, 54, 61, 63, 66, 71, 73, 75, 77, 75, 73, 74, 71,
984+ 54, 50, 49, 50, 55, 62, 65, 68, 73, 76, 78, 79, 78, 76, 74, 75,
985+ 55, 51, 49, 50, 56, 63, 65, 69, 74, 77, 79, 81, 79, 78, 78, 75,
986+ 57, 52, 50, 51, 56, 64, 66, 70, 76, 79, 82, 85, 83, 81, 79, 79,
987+ 60, 54, 53, 53, 58, 65, 68, 72, 79, 82, 85, 87, 85, 84, 82, 80,
988+ 62, 56, 54, 55, 60, 66, 69, 74, 81, 84, 87, 88, 87, 85, 84, 84,
989+ 63, 57, 55, 56, 60, 67, 70, 75, 82, 86, 89, 92, 91, 89, 87, 84,
990+ 64, 59, 56, 57, 61, 68, 71, 75, 83, 87, 90, 93, 92, 90, 89, 89,
991+ 66, 60, 58, 58, 62, 69, 72, 76, 84, 88, 91, 94, 95, 93, 91, 89,
992+ 67, 61, 59, 58, 63, 68, 71, 78, 83, 86, 93, 96, 96, 96, 94, 94,
993+ 68, 62, 60, 59, 64, 67, 71, 79, 81, 86, 94, 95, 97, 98, 96, 94,
994+ 69, 63, 61, 60, 65, 66, 72, 77, 80, 88, 91, 96, 99, 99, 100, 98,
995+ 70, 64, 62, 60, 65, 66, 73, 76, 81, 87, 89, 97, 98, 100, 101, 99,
996+ 71, 65, 64, 61, 65, 67, 73, 74, 82, 85, 90, 95, 99, 102, 103, 104,
997+ 72, 65, 65, 62, 65, 68, 72, 75, 82, 83, 92, 93, 100, 102, 103, 104,
998+ 73, 66, 66, 63, 65, 69, 72, 76, 81, 85, 90, 93, 100, 102, 105, 106,
999+ 74, 67, 67, 64, 65, 70, 71, 77, 79, 86, 89, 94, 98, 103, 105, 106,
1000+ 75, 68, 68, 65, 65, 71, 71, 78, 78, 87, 87, 96, 96, 105, 105, 109,
1001+ // Size 4x16
1002+ 31, 32, 36, 41, 46, 46, 46, 48, 51, 54, 57, 60, 62, 64, 65, 67,
1003+ 49, 45, 46, 47, 53, 56, 59, 61, 63, 65, 67, 69, 67, 66, 68, 70,
1004+ 61, 55, 54, 54, 59, 62, 68, 73, 77, 82, 86, 88, 86, 87, 83, 86,
1005+ 69, 64, 61, 59, 62, 65, 68, 73, 78, 84, 89, 93, 98, 100, 102, 103,
1006+ // Size 16x4
1007+ 31, 49, 61, 69, 32, 45, 55, 64, 36, 46, 54, 61, 41, 47, 54, 59,
1008+ 46, 53, 59, 62, 46, 56, 62, 65, 46, 59, 68, 68, 48, 61, 73, 73,
1009+ 51, 63, 77, 78, 54, 65, 82, 84, 57, 67, 86, 89, 60, 69, 88, 93,
1010+ 62, 67, 86, 98, 64, 66, 87, 100, 65, 68, 83, 102, 67, 70, 86, 103,
1011+ // Size 8x32
1012+ 32, 31, 30, 31, 33, 37, 39, 42, 49, 48, 48, 49, 50, 51, 52, 54,
1013+ 55, 57, 60, 62, 63, 64, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
1014+ 35, 37, 38, 38, 41, 45, 46, 46, 48, 47, 46, 45, 46, 47, 47, 49,
1015+ 49, 50, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68,
1016+ 48, 47, 46, 46, 47, 47, 48, 50, 53, 53, 53, 53, 54, 54, 54, 55,
1017+ 56, 56, 58, 60, 60, 61, 62, 63, 64, 65, 65, 65, 65, 65, 65, 65,
1018+ 50, 48, 46, 46, 46, 46, 47, 50, 54, 55, 56, 59, 61, 61, 63, 65,
1019+ 65, 66, 68, 69, 70, 71, 72, 71, 71, 72, 73, 73, 72, 72, 71, 71,
1020+ 57, 54, 52, 52, 51, 50, 51, 53, 57, 58, 60, 64, 66, 68, 71, 73,
1021+ 74, 76, 79, 81, 82, 83, 84, 83, 81, 80, 81, 82, 82, 81, 79, 78,
1022+ 63, 60, 58, 57, 56, 54, 55, 57, 60, 62, 64, 67, 70, 71, 75, 78,
1023+ 79, 82, 85, 87, 89, 90, 91, 93, 94, 91, 89, 90, 92, 90, 89, 87,
1024+ 68, 64, 63, 61, 60, 59, 58, 60, 61, 64, 65, 67, 71, 72, 75, 78,
1025+ 79, 83, 85, 87, 91, 92, 95, 96, 97, 99, 98, 99, 100, 100, 98, 96,
1026+ 70, 66, 65, 63, 63, 62, 61, 60, 61, 63, 65, 66, 69, 70, 74, 74,
1027+ 78, 79, 82, 84, 87, 89, 91, 94, 96, 100, 101, 103, 103, 105, 105, 105,
1028+ // Size 32x8
1029+ 32, 35, 48, 50, 57, 63, 68, 70, 31, 37, 47, 48, 54, 60, 64, 66,
1030+ 30, 38, 46, 46, 52, 58, 63, 65, 31, 38, 46, 46, 52, 57, 61, 63,
1031+ 33, 41, 47, 46, 51, 56, 60, 63, 37, 45, 47, 46, 50, 54, 59, 62,
1032+ 39, 46, 48, 47, 51, 55, 58, 61, 42, 46, 50, 50, 53, 57, 60, 60,
1033+ 49, 48, 53, 54, 57, 60, 61, 61, 48, 47, 53, 55, 58, 62, 64, 63,
1034+ 48, 46, 53, 56, 60, 64, 65, 65, 49, 45, 53, 59, 64, 67, 67, 66,
1035+ 50, 46, 54, 61, 66, 70, 71, 69, 51, 47, 54, 61, 68, 71, 72, 70,
1036+ 52, 47, 54, 63, 71, 75, 75, 74, 54, 49, 55, 65, 73, 78, 78, 74,
1037+ 55, 49, 56, 65, 74, 79, 79, 78, 57, 50, 56, 66, 76, 82, 83, 79,
1038+ 60, 53, 58, 68, 79, 85, 85, 82, 62, 54, 60, 69, 81, 87, 87, 84,
1039+ 63, 55, 60, 70, 82, 89, 91, 87, 64, 56, 61, 71, 83, 90, 92, 89,
1040+ 66, 58, 62, 72, 84, 91, 95, 91, 67, 59, 63, 71, 83, 93, 96, 94,
1041+ 68, 60, 64, 71, 81, 94, 97, 96, 69, 61, 65, 72, 80, 91, 99, 100,
1042+ 70, 62, 65, 73, 81, 89, 98, 101, 71, 64, 65, 73, 82, 90, 99, 103,
1043+ 72, 65, 65, 72, 82, 92, 100, 103, 73, 66, 65, 72, 81, 90, 100, 105,
1044+ 74, 67, 65, 71, 79, 89, 98, 105, 75, 68, 65, 71, 78, 87, 96, 105,
1045+ ],
1046+ ],
1047+ // qm level 2
1048+ [
1049+ [// luma
1050+ // Size 4x4
1051+ 32, 38, 63, 86, 38, 56, 78, 97, 63, 78, 113, 130, 86, 97, 130, 169,
1052+ // Size 8x8
1053+ 32, 32, 35, 46, 57, 76, 85, 96, 32, 34, 37, 45, 54, 70, 79, 90,
1054+ 35, 37, 48, 56, 64, 79, 87, 93, 46, 45, 56, 70, 80, 96, 100, 105,
1055+ 57, 54, 64, 80, 93, 111, 121, 122, 76, 70, 79, 96, 111, 134, 138, 144,
1056+ 85, 79, 87, 100, 121, 138, 156, 168, 96, 90, 93, 105, 122, 144, 168, 184,
1057+ // Size 16x16
1058+ 32, 31, 31, 32, 34, 39, 44, 49, 58, 65, 71, 81, 87, 93, 98, 104,
1059+ 31, 32, 32, 32, 34, 38, 41, 46, 54, 60, 66, 75, 81, 86, 92, 98,
1060+ 31, 32, 33, 34, 36, 39, 42, 46, 53, 59, 64, 73, 78, 83, 88, 94,
1061+ 32, 32, 34, 35, 37, 40, 42, 46, 52, 58, 63, 71, 75, 80, 86, 92,
1062+ 34, 34, 36, 37, 42, 47, 50, 53, 59, 65, 70, 77, 82, 85, 89, 92,
1063+ 39, 38, 39, 40, 47, 54, 58, 62, 68, 73, 78, 85, 90, 90, 96, 98,
1064+ 44, 41, 42, 42, 50, 58, 63, 68, 74, 79, 84, 91, 96, 98, 102, 104,
1065+ 49, 46, 46, 46, 53, 62, 68, 73, 81, 87, 92, 99, 103, 107, 109, 112,
1066+ 58, 54, 53, 52, 59, 68, 74, 81, 90, 97, 102, 110, 114, 118, 117, 121,
1067+ 65, 60, 59, 58, 65, 73, 79, 87, 97, 105, 111, 120, 125, 125, 126, 130,
1068+ 71, 66, 64, 63, 70, 78, 84, 92, 102, 111, 117, 127, 133, 134, 136, 141,
1069+ 81, 75, 73, 71, 77, 85, 91, 99, 110, 120, 127, 137, 143, 145, 148, 152,
1070+ 87, 81, 78, 75, 82, 90, 96, 103, 114, 125, 133, 143, 150, 156, 160, 163,
1071+ 93, 86, 83, 80, 85, 90, 98, 107, 118, 125, 134, 145, 156, 163, 169, 177,
1072+ 98, 92, 88, 86, 89, 96, 102, 109, 117, 126, 136, 148, 160, 169, 176, 184,
1073+ 104, 98, 94, 92, 92, 98, 104, 112, 121, 130, 141, 152, 163, 177, 184, 191,
1074+ // Size 32x32
1075+ 32, 31, 31, 31, 31, 32, 32, 34, 34, 36, 39, 41, 44, 48, 49, 54,
1076+ 58, 59, 65, 69, 71, 80, 81, 83, 87, 90, 93, 95, 98, 101, 104, 107,
1077+ 31, 32, 32, 32, 32, 32, 32, 34, 34, 35, 38, 39, 42, 46, 47, 51,
1078+ 55, 57, 62, 66, 68, 76, 77, 78, 83, 85, 88, 90, 93, 96, 99, 101,
1079+ 31, 32, 32, 32, 32, 32, 32, 33, 34, 34, 38, 39, 41, 45, 46, 50,
1080+ 54, 55, 60, 64, 66, 73, 75, 76, 81, 83, 86, 89, 92, 95, 98, 101,
1081+ 31, 32, 32, 32, 32, 32, 32, 33, 34, 34, 37, 38, 41, 44, 45, 49,
1082+ 53, 54, 59, 63, 65, 72, 74, 75, 79, 81, 84, 86, 89, 91, 94, 97,
1083+ 31, 32, 32, 32, 33, 33, 34, 35, 36, 36, 39, 40, 42, 45, 46, 50,
1084+ 53, 54, 59, 63, 64, 71, 73, 74, 78, 80, 83, 85, 88, 91, 94, 97,
1085+ 32, 32, 32, 32, 33, 34, 34, 36, 36, 37, 40, 40, 42, 45, 46, 49,
1086+ 53, 54, 58, 62, 63, 70, 72, 73, 77, 79, 82, 85, 87, 90, 92, 95,
1087+ 32, 32, 32, 32, 34, 34, 35, 37, 37, 38, 40, 41, 42, 45, 46, 49,
1088+ 52, 54, 58, 61, 63, 69, 71, 72, 75, 78, 80, 83, 86, 89, 92, 95,
1089+ 34, 34, 33, 33, 35, 36, 37, 39, 41, 42, 45, 46, 47, 50, 51, 54,
1090+ 57, 59, 63, 66, 68, 74, 75, 76, 80, 81, 82, 83, 85, 87, 90, 93,
1091+ 34, 34, 34, 34, 36, 36, 37, 41, 42, 45, 47, 48, 50, 53, 53, 56,
1092+ 59, 61, 65, 68, 70, 76, 77, 78, 82, 83, 85, 88, 89, 90, 92, 93,
1093+ 36, 35, 34, 34, 36, 37, 38, 42, 45, 48, 50, 51, 54, 56, 57, 60,
1094+ 63, 64, 68, 71, 73, 79, 80, 81, 85, 87, 89, 89, 90, 93, 96, 99,
1095+ 39, 38, 38, 37, 39, 40, 40, 45, 47, 50, 54, 55, 58, 61, 62, 65,
1096+ 68, 69, 73, 76, 78, 84, 85, 86, 90, 89, 90, 93, 96, 97, 98, 99,
1097+ 41, 39, 39, 38, 40, 40, 41, 46, 48, 51, 55, 56, 59, 62, 63, 67,
1098+ 70, 71, 75, 78, 80, 86, 87, 88, 91, 93, 96, 97, 97, 99, 102, 105,
1099+ 44, 42, 41, 41, 42, 42, 42, 47, 50, 54, 58, 59, 63, 66, 68, 71,
1100+ 74, 75, 79, 83, 84, 90, 91, 92, 96, 98, 98, 99, 102, 104, 104, 105,
1101+ 48, 46, 45, 44, 45, 45, 45, 50, 53, 56, 61, 62, 66, 70, 71, 76,
1102+ 79, 80, 85, 88, 90, 96, 97, 98, 101, 100, 102, 105, 105, 105, 109, 112,
1103+ 49, 47, 46, 45, 46, 46, 46, 51, 53, 57, 62, 63, 68, 71, 73, 77,
1104+ 81, 82, 87, 90, 92, 98, 99, 100, 103, 106, 107, 106, 109, 112, 112, 112,
1105+ 54, 51, 50, 49, 50, 49, 49, 54, 56, 60, 65, 67, 71, 76, 77, 82,
1106+ 86, 87, 92, 96, 97, 104, 105, 106, 110, 110, 109, 113, 114, 113, 116, 120,
1107+ 58, 55, 54, 53, 53, 53, 52, 57, 59, 63, 68, 70, 74, 79, 81, 86,
1108+ 90, 91, 97, 100, 102, 109, 110, 111, 114, 114, 118, 116, 117, 121, 121, 120,
1109+ 59, 57, 55, 54, 54, 54, 54, 59, 61, 64, 69, 71, 75, 80, 82, 87,
1110+ 91, 93, 99, 102, 104, 111, 112, 113, 117, 121, 120, 122, 124, 122, 125, 129,
1111+ 65, 62, 60, 59, 59, 58, 58, 63, 65, 68, 73, 75, 79, 85, 87, 92,
1112+ 97, 99, 105, 109, 111, 118, 120, 121, 125, 124, 125, 127, 126, 130, 130, 129,
1113+ 69, 66, 64, 63, 63, 62, 61, 66, 68, 71, 76, 78, 83, 88, 90, 96,
1114+ 100, 102, 109, 113, 115, 123, 125, 126, 129, 130, 131, 130, 134, 133, 135, 139,
1115+ 71, 68, 66, 65, 64, 63, 63, 68, 70, 73, 78, 80, 84, 90, 92, 97,
1116+ 102, 104, 111, 115, 117, 125, 127, 128, 133, 136, 134, 139, 136, 139, 141, 140,
1117+ 80, 76, 73, 72, 71, 70, 69, 74, 76, 79, 84, 86, 90, 96, 98, 104,
1118+ 109, 111, 118, 123, 125, 134, 136, 137, 142, 138, 143, 140, 144, 144, 144, 149,
1119+ 81, 77, 75, 74, 73, 72, 71, 75, 77, 80, 85, 87, 91, 97, 99, 105,
1120+ 110, 112, 120, 125, 127, 136, 137, 139, 143, 148, 145, 148, 148, 150, 152, 149,
1121+ 83, 78, 76, 75, 74, 73, 72, 76, 78, 81, 86, 88, 92, 98, 100, 106,
1122+ 111, 113, 121, 126, 128, 137, 139, 140, 145, 149, 153, 153, 154, 155, 155, 161,
1123+ 87, 83, 81, 79, 78, 77, 75, 80, 82, 85, 90, 91, 96, 101, 103, 110,
1124+ 114, 117, 125, 129, 133, 142, 143, 145, 150, 151, 156, 159, 160, 160, 163, 161,
1125+ 90, 85, 83, 81, 80, 79, 78, 81, 83, 87, 89, 93, 98, 100, 106, 110,
1126+ 114, 121, 124, 130, 136, 138, 148, 149, 151, 156, 157, 162, 166, 168, 166, 172,
1127+ 93, 88, 86, 84, 83, 82, 80, 82, 85, 89, 90, 96, 98, 102, 107, 109,
1128+ 118, 120, 125, 131, 134, 143, 145, 153, 156, 157, 163, 164, 169, 172, 177, 172,
1129+ 95, 90, 89, 86, 85, 85, 83, 83, 88, 89, 93, 97, 99, 105, 106, 113,
1130+ 116, 122, 127, 130, 139, 140, 148, 153, 159, 162, 164, 169, 170, 176, 179, 185,
1131+ 98, 93, 92, 89, 88, 87, 86, 85, 89, 90, 96, 97, 102, 105, 109, 114,
1132+ 117, 124, 126, 134, 136, 144, 148, 154, 160, 166, 169, 170, 176, 177, 184, 186,
1133+ 101, 96, 95, 91, 91, 90, 89, 87, 90, 93, 97, 99, 104, 105, 112, 113,
1134+ 121, 122, 130, 133, 139, 144, 150, 155, 160, 168, 172, 176, 177, 184, 185, 191,
1135+ 104, 99, 98, 94, 94, 92, 92, 90, 92, 96, 98, 102, 104, 109, 112, 116,
1136+ 121, 125, 130, 135, 141, 144, 152, 155, 163, 166, 177, 179, 184, 185, 191, 192,
1137+ 107, 101, 101, 97, 97, 95, 95, 93, 93, 99, 99, 105, 105, 112, 112, 120,
1138+ 120, 129, 129, 139, 140, 149, 149, 161, 161, 172, 172, 185, 186, 191, 192, 199,
1139+ // Size 4x8
1140+ 32, 32, 34, 44, 54, 72, 82, 92, 38, 40, 51, 61, 69, 84, 89, 98,
1141+ 62, 58, 68, 85, 98, 118, 129, 127, 86, 80, 85, 101, 117, 136, 157, 165,
1142+ // Size 8x4
1143+ 32, 38, 62, 86, 32, 40, 58, 80, 34, 51, 68, 85, 44, 61, 85, 101,
1144+ 54, 69, 98, 117, 72, 84, 118, 136, 82, 89, 129, 157, 92, 98, 127, 165,
1145+ // Size 8x16
1146+ 32, 31, 32, 32, 34, 39, 44, 49, 57, 65, 71, 81, 87, 92, 98, 103,
1147+ 32, 32, 33, 34, 36, 39, 42, 46, 53, 59, 64, 72, 77, 83, 88, 94,
1148+ 36, 35, 36, 38, 44, 50, 53, 57, 63, 68, 73, 80, 85, 88, 89, 92,
1149+ 44, 41, 42, 42, 50, 58, 63, 67, 74, 79, 84, 91, 96, 102, 103, 103,
1150+ 58, 54, 53, 52, 59, 68, 74, 81, 90, 97, 102, 110, 114, 117, 121, 119,
1151+ 79, 73, 71, 69, 75, 84, 90, 97, 108, 118, 125, 135, 140, 133, 141, 137,
1152+ 88, 81, 78, 76, 81, 88, 97, 104, 111, 123, 135, 145, 148, 153, 160, 158,
1153+ 93, 88, 84, 82, 84, 90, 97, 105, 113, 122, 131, 141, 151, 163, 169, 175,
1154+ // Size 16x8
1155+ 32, 32, 36, 44, 58, 79, 88, 93, 31, 32, 35, 41, 54, 73, 81, 88,
1156+ 32, 33, 36, 42, 53, 71, 78, 84, 32, 34, 38, 42, 52, 69, 76, 82,
1157+ 34, 36, 44, 50, 59, 75, 81, 84, 39, 39, 50, 58, 68, 84, 88, 90,
1158+ 44, 42, 53, 63, 74, 90, 97, 97, 49, 46, 57, 67, 81, 97, 104, 105,
1159+ 57, 53, 63, 74, 90, 108, 111, 113, 65, 59, 68, 79, 97, 118, 123, 122,
1160+ 71, 64, 73, 84, 102, 125, 135, 131, 81, 72, 80, 91, 110, 135, 145, 141,
1161+ 87, 77, 85, 96, 114, 140, 148, 151, 92, 83, 88, 102, 117, 133, 153, 163,
1162+ 98, 88, 89, 103, 121, 141, 160, 169, 103, 94, 92, 103, 119, 137, 158, 175,
1163+ // Size 16x32
1164+ 32, 31, 31, 31, 32, 32, 32, 34, 34, 36, 39, 40, 44, 47, 49, 53,
1165+ 57, 59, 65, 69, 71, 79, 81, 82, 87, 90, 92, 95, 98, 100, 103, 106,
1166+ 31, 32, 32, 32, 32, 32, 33, 34, 34, 34, 37, 38, 41, 44, 46, 49,
1167+ 53, 54, 60, 63, 65, 72, 74, 75, 79, 82, 84, 87, 89, 92, 94, 97,
1168+ 32, 32, 32, 32, 33, 34, 34, 35, 36, 37, 39, 40, 42, 45, 46, 50,
1169+ 53, 54, 59, 62, 64, 71, 72, 73, 77, 80, 83, 85, 88, 91, 94, 97,
1170+ 32, 32, 32, 33, 34, 34, 35, 37, 37, 38, 40, 41, 43, 46, 47, 50,
1171+ 53, 54, 58, 62, 63, 70, 71, 72, 76, 78, 81, 83, 85, 88, 90, 93,
1172+ 36, 35, 35, 34, 36, 37, 38, 42, 44, 48, 50, 51, 53, 56, 57, 60,
1173+ 63, 64, 68, 71, 73, 79, 80, 81, 85, 87, 88, 88, 89, 90, 92, 93,
1174+ 39, 38, 38, 37, 39, 40, 40, 45, 47, 51, 54, 55, 58, 61, 62, 65,
1175+ 68, 69, 73, 76, 78, 84, 85, 86, 90, 89, 90, 92, 95, 98, 101, 104,
1176+ 44, 42, 41, 41, 42, 42, 42, 48, 50, 54, 58, 59, 63, 66, 67, 71,
1177+ 74, 75, 79, 83, 84, 90, 91, 92, 96, 99, 102, 103, 103, 103, 103, 104,
1178+ 53, 51, 50, 49, 50, 49, 49, 54, 56, 60, 65, 67, 71, 75, 77, 82,
1179+ 86, 87, 92, 96, 97, 104, 105, 106, 110, 108, 106, 105, 108, 111, 114, 118,
1180+ 58, 55, 54, 53, 53, 53, 52, 57, 59, 63, 68, 70, 74, 79, 81, 86,
1181+ 90, 91, 97, 100, 102, 109, 110, 111, 114, 113, 117, 120, 121, 120, 119, 118,
1182+ 65, 62, 60, 59, 59, 58, 58, 63, 65, 68, 73, 75, 79, 85, 86, 92,
1183+ 97, 98, 105, 109, 111, 118, 120, 121, 125, 129, 128, 125, 124, 127, 131, 135,
1184+ 79, 75, 73, 72, 71, 70, 69, 73, 75, 78, 84, 85, 90, 95, 97, 103,
1185+ 108, 111, 118, 122, 125, 133, 135, 136, 140, 135, 133, 137, 141, 139, 137, 135,
1186+ 81, 77, 75, 74, 72, 71, 70, 75, 77, 80, 85, 87, 91, 97, 99, 105,
1187+ 110, 112, 119, 124, 127, 135, 137, 139, 143, 146, 150, 148, 144, 146, 150, 154,
1188+ 88, 83, 81, 79, 78, 77, 76, 79, 81, 85, 88, 91, 97, 99, 104, 109,
1189+ 111, 119, 123, 127, 135, 137, 145, 147, 148, 153, 153, 155, 160, 161, 158, 155,
1190+ 90, 86, 84, 82, 81, 80, 78, 79, 83, 85, 89, 92, 94, 101, 102, 108,
1191+ 112, 117, 123, 125, 134, 136, 143, 148, 154, 157, 158, 164, 164, 165, 170, 175,
1192+ 93, 88, 88, 84, 84, 83, 82, 81, 84, 86, 90, 92, 97, 98, 105, 106,
1193+ 113, 115, 122, 125, 131, 136, 141, 147, 151, 160, 163, 168, 169, 175, 175, 176,
1194+ 96, 91, 91, 87, 87, 85, 86, 83, 84, 89, 89, 95, 95, 102, 102, 110,
1195+ 110, 118, 119, 128, 129, 137, 138, 149, 149, 159, 160, 173, 174, 179, 180, 187,
1196+ // Size 32x16
1197+ 32, 31, 32, 32, 36, 39, 44, 53, 58, 65, 79, 81, 88, 90, 93, 96,
1198+ 31, 32, 32, 32, 35, 38, 42, 51, 55, 62, 75, 77, 83, 86, 88, 91,
1199+ 31, 32, 32, 32, 35, 38, 41, 50, 54, 60, 73, 75, 81, 84, 88, 91,
1200+ 31, 32, 32, 33, 34, 37, 41, 49, 53, 59, 72, 74, 79, 82, 84, 87,
1201+ 32, 32, 33, 34, 36, 39, 42, 50, 53, 59, 71, 72, 78, 81, 84, 87,
1202+ 32, 32, 34, 34, 37, 40, 42, 49, 53, 58, 70, 71, 77, 80, 83, 85,
1203+ 32, 33, 34, 35, 38, 40, 42, 49, 52, 58, 69, 70, 76, 78, 82, 86,
1204+ 34, 34, 35, 37, 42, 45, 48, 54, 57, 63, 73, 75, 79, 79, 81, 83,
1205+ 34, 34, 36, 37, 44, 47, 50, 56, 59, 65, 75, 77, 81, 83, 84, 84,
1206+ 36, 34, 37, 38, 48, 51, 54, 60, 63, 68, 78, 80, 85, 85, 86, 89,
1207+ 39, 37, 39, 40, 50, 54, 58, 65, 68, 73, 84, 85, 88, 89, 90, 89,
1208+ 40, 38, 40, 41, 51, 55, 59, 67, 70, 75, 85, 87, 91, 92, 92, 95,
1209+ 44, 41, 42, 43, 53, 58, 63, 71, 74, 79, 90, 91, 97, 94, 97, 95,
1210+ 47, 44, 45, 46, 56, 61, 66, 75, 79, 85, 95, 97, 99, 101, 98, 102,
1211+ 49, 46, 46, 47, 57, 62, 67, 77, 81, 86, 97, 99, 104, 102, 105, 102,
1212+ 53, 49, 50, 50, 60, 65, 71, 82, 86, 92, 103, 105, 109, 108, 106, 110,
1213+ 57, 53, 53, 53, 63, 68, 74, 86, 90, 97, 108, 110, 111, 112, 113, 110,
1214+ 59, 54, 54, 54, 64, 69, 75, 87, 91, 98, 111, 112, 119, 117, 115, 118,
1215+ 65, 60, 59, 58, 68, 73, 79, 92, 97, 105, 118, 119, 123, 123, 122, 119,
1216+ 69, 63, 62, 62, 71, 76, 83, 96, 100, 109, 122, 124, 127, 125, 125, 128,
1217+ 71, 65, 64, 63, 73, 78, 84, 97, 102, 111, 125, 127, 135, 134, 131, 129,
1218+ 79, 72, 71, 70, 79, 84, 90, 104, 109, 118, 133, 135, 137, 136, 136, 137,
1219+ 81, 74, 72, 71, 80, 85, 91, 105, 110, 120, 135, 137, 145, 143, 141, 138,
1220+ 82, 75, 73, 72, 81, 86, 92, 106, 111, 121, 136, 139, 147, 148, 147, 149,
1221+ 87, 79, 77, 76, 85, 90, 96, 110, 114, 125, 140, 143, 148, 154, 151, 149,
1222+ 90, 82, 80, 78, 87, 89, 99, 108, 113, 129, 135, 146, 153, 157, 160, 159,
1223+ 92, 84, 83, 81, 88, 90, 102, 106, 117, 128, 133, 150, 153, 158, 163, 160,
1224+ 95, 87, 85, 83, 88, 92, 103, 105, 120, 125, 137, 148, 155, 164, 168, 173,
1225+ 98, 89, 88, 85, 89, 95, 103, 108, 121, 124, 141, 144, 160, 164, 169, 174,
1226+ 100, 92, 91, 88, 90, 98, 103, 111, 120, 127, 139, 146, 161, 165, 175, 179,
1227+ 103, 94, 94, 90, 92, 101, 103, 114, 119, 131, 137, 150, 158, 170, 175, 180,
1228+ 106, 97, 97, 93, 93, 104, 104, 118, 118, 135, 135, 154, 155, 175, 176, 187,
1229+ // Size 4x16
1230+ 31, 32, 32, 33, 34, 37, 41, 46, 53, 60, 65, 74, 79, 84, 89, 94,
1231+ 39, 38, 39, 40, 47, 54, 58, 62, 68, 73, 78, 85, 90, 90, 95, 101,
1232+ 65, 60, 59, 58, 65, 73, 79, 86, 97, 105, 111, 120, 125, 128, 124, 131,
1233+ 90, 84, 81, 78, 83, 89, 94, 102, 112, 123, 134, 143, 154, 158, 164, 170,
1234+ // Size 16x4
1235+ 31, 39, 65, 90, 32, 38, 60, 84, 32, 39, 59, 81, 33, 40, 58, 78,
1236+ 34, 47, 65, 83, 37, 54, 73, 89, 41, 58, 79, 94, 46, 62, 86, 102,
1237+ 53, 68, 97, 112, 60, 73, 105, 123, 65, 78, 111, 134, 74, 85, 120, 143,
1238+ 79, 90, 125, 154, 84, 90, 128, 158, 89, 95, 124, 164, 94, 101, 131, 170,
1239+ // Size 8x32
1240+ 32, 31, 31, 31, 32, 32, 32, 34, 34, 36, 39, 40, 44, 47, 49, 53,
1241+ 57, 59, 65, 69, 71, 79, 81, 82, 87, 90, 92, 95, 98, 100, 103, 106,
1242+ 32, 32, 32, 32, 33, 34, 34, 35, 36, 37, 39, 40, 42, 45, 46, 50,
1243+ 53, 54, 59, 62, 64, 71, 72, 73, 77, 80, 83, 85, 88, 91, 94, 97,
1244+ 36, 35, 35, 34, 36, 37, 38, 42, 44, 48, 50, 51, 53, 56, 57, 60,
1245+ 63, 64, 68, 71, 73, 79, 80, 81, 85, 87, 88, 88, 89, 90, 92, 93,
1246+ 44, 42, 41, 41, 42, 42, 42, 48, 50, 54, 58, 59, 63, 66, 67, 71,
1247+ 74, 75, 79, 83, 84, 90, 91, 92, 96, 99, 102, 103, 103, 103, 103, 104,
1248+ 58, 55, 54, 53, 53, 53, 52, 57, 59, 63, 68, 70, 74, 79, 81, 86,
1249+ 90, 91, 97, 100, 102, 109, 110, 111, 114, 113, 117, 120, 121, 120, 119, 118,
1250+ 79, 75, 73, 72, 71, 70, 69, 73, 75, 78, 84, 85, 90, 95, 97, 103,
1251+ 108, 111, 118, 122, 125, 133, 135, 136, 140, 135, 133, 137, 141, 139, 137, 135,
1252+ 88, 83, 81, 79, 78, 77, 76, 79, 81, 85, 88, 91, 97, 99, 104, 109,
1253+ 111, 119, 123, 127, 135, 137, 145, 147, 148, 153, 153, 155, 160, 161, 158, 155,
1254+ 93, 88, 88, 84, 84, 83, 82, 81, 84, 86, 90, 92, 97, 98, 105, 106,
1255+ 113, 115, 122, 125, 131, 136, 141, 147, 151, 160, 163, 168, 169, 175, 175, 176,
1256+ // Size 32x8
1257+ 32, 32, 36, 44, 58, 79, 88, 93, 31, 32, 35, 42, 55, 75, 83, 88,
1258+ 31, 32, 35, 41, 54, 73, 81, 88, 31, 32, 34, 41, 53, 72, 79, 84,
1259+ 32, 33, 36, 42, 53, 71, 78, 84, 32, 34, 37, 42, 53, 70, 77, 83,
1260+ 32, 34, 38, 42, 52, 69, 76, 82, 34, 35, 42, 48, 57, 73, 79, 81,
1261+ 34, 36, 44, 50, 59, 75, 81, 84, 36, 37, 48, 54, 63, 78, 85, 86,
1262+ 39, 39, 50, 58, 68, 84, 88, 90, 40, 40, 51, 59, 70, 85, 91, 92,
1263+ 44, 42, 53, 63, 74, 90, 97, 97, 47, 45, 56, 66, 79, 95, 99, 98,
1264+ 49, 46, 57, 67, 81, 97, 104, 105, 53, 50, 60, 71, 86, 103, 109, 106,
1265+ 57, 53, 63, 74, 90, 108, 111, 113, 59, 54, 64, 75, 91, 111, 119, 115,
1266+ 65, 59, 68, 79, 97, 118, 123, 122, 69, 62, 71, 83, 100, 122, 127, 125,
1267+ 71, 64, 73, 84, 102, 125, 135, 131, 79, 71, 79, 90, 109, 133, 137, 136,
1268+ 81, 72, 80, 91, 110, 135, 145, 141, 82, 73, 81, 92, 111, 136, 147, 147,
1269+ 87, 77, 85, 96, 114, 140, 148, 151, 90, 80, 87, 99, 113, 135, 153, 160,
1270+ 92, 83, 88, 102, 117, 133, 153, 163, 95, 85, 88, 103, 120, 137, 155, 168,
1271+ 98, 88, 89, 103, 121, 141, 160, 169, 100, 91, 90, 103, 120, 139, 161, 175,
1272+ 103, 94, 92, 103, 119, 137, 158, 175, 106, 97, 93, 104, 118, 135, 155, 176,
1273+ ],
1274+ [// chroma
1275+ // Size 4x4
1276+ 32, 45, 53, 63, 45, 55, 62, 67, 53, 62, 80, 84, 63, 67, 84, 101,
1277+ // Size 8x8
1278+ 31, 36, 47, 48, 52, 60, 64, 67, 36, 43, 47, 46, 49, 55, 59, 63,
1279+ 47, 47, 53, 54, 55, 60, 63, 64, 48, 46, 54, 61, 65, 70, 71, 71,
1280+ 52, 49, 55, 65, 71, 78, 81, 79, 60, 55, 60, 70, 78, 89, 89, 89,
1281+ 64, 59, 63, 71, 81, 89, 97, 99, 67, 63, 64, 71, 79, 89, 99, 104,
1282+ // Size 16x16
1283+ 32, 30, 33, 36, 44, 48, 49, 51, 54, 57, 60, 64, 67, 68, 70, 72,
1284+ 30, 31, 35, 39, 44, 46, 46, 47, 50, 53, 55, 59, 61, 64, 66, 68,
1285+ 33, 35, 39, 43, 46, 46, 45, 47, 49, 51, 53, 57, 59, 61, 63, 65,
1286+ 36, 39, 43, 47, 47, 46, 45, 46, 48, 50, 52, 55, 57, 58, 61, 63,
1287+ 44, 44, 46, 47, 50, 51, 51, 51, 53, 54, 56, 59, 61, 61, 63, 62,
1288+ 48, 46, 46, 46, 51, 54, 55, 56, 58, 60, 61, 64, 65, 64, 66, 66,
1289+ 49, 46, 45, 45, 51, 55, 58, 60, 62, 63, 65, 68, 69, 69, 69, 69,
1290+ 51, 47, 47, 46, 51, 56, 60, 62, 65, 67, 69, 72, 73, 74, 73, 73,
1291+ 54, 50, 49, 48, 53, 58, 62, 65, 70, 73, 75, 78, 79, 79, 77, 77,
1292+ 57, 53, 51, 50, 54, 60, 63, 67, 73, 76, 79, 82, 84, 83, 82, 82,
1293+ 60, 55, 53, 52, 56, 61, 65, 69, 75, 79, 82, 86, 88, 87, 86, 87,
1294+ 64, 59, 57, 55, 59, 64, 68, 72, 78, 82, 86, 90, 93, 92, 91, 92,
1295+ 67, 61, 59, 57, 61, 65, 69, 73, 79, 84, 88, 93, 95, 96, 96, 96,
1296+ 68, 64, 61, 58, 61, 64, 69, 74, 79, 83, 87, 92, 96, 99, 100, 101,
1297+ 70, 66, 63, 61, 63, 66, 69, 73, 77, 82, 86, 91, 96, 100, 103, 104,
1298+ 72, 68, 65, 63, 62, 66, 69, 73, 77, 82, 87, 92, 96, 101, 104, 106,
1299+ // Size 32x32
1300+ 32, 31, 30, 30, 33, 35, 36, 41, 44, 49, 48, 48, 49, 50, 51, 52,
1301+ 54, 55, 57, 59, 60, 63, 64, 65, 67, 68, 68, 69, 70, 71, 72, 73,
1302+ 31, 31, 31, 31, 34, 36, 38, 42, 44, 47, 47, 47, 47, 48, 48, 50,
1303+ 51, 52, 54, 56, 57, 60, 61, 61, 63, 64, 65, 66, 67, 67, 68, 69,
1304+ 30, 31, 31, 31, 35, 37, 39, 42, 44, 47, 46, 46, 46, 47, 47, 48,
1305+ 50, 51, 53, 54, 55, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69,
1306+ 30, 31, 31, 32, 35, 37, 40, 42, 44, 46, 45, 45, 45, 46, 46, 47,
1307+ 49, 50, 52, 53, 54, 57, 58, 58, 60, 61, 62, 63, 63, 64, 65, 66,
1308+ 33, 34, 35, 35, 39, 41, 43, 45, 46, 47, 46, 46, 45, 46, 47, 47,
1309+ 49, 49, 51, 53, 53, 56, 57, 57, 59, 60, 61, 62, 63, 64, 65, 66,
1310+ 35, 36, 37, 37, 41, 43, 45, 46, 46, 47, 46, 46, 45, 46, 46, 47,
1311+ 48, 49, 50, 52, 53, 55, 56, 56, 58, 59, 60, 61, 62, 63, 64, 64,
1312+ 36, 38, 39, 40, 43, 45, 47, 47, 47, 48, 46, 46, 45, 46, 46, 47,
1313+ 48, 48, 50, 51, 52, 54, 55, 55, 57, 58, 58, 59, 61, 62, 63, 64,
1314+ 41, 42, 42, 42, 45, 46, 47, 48, 49, 50, 49, 49, 49, 50, 50, 50,
1315+ 51, 52, 53, 54, 55, 57, 58, 58, 60, 60, 59, 59, 60, 61, 61, 62,
1316+ 44, 44, 44, 44, 46, 46, 47, 49, 50, 51, 51, 51, 51, 51, 51, 52,
1317+ 53, 53, 54, 56, 56, 59, 59, 59, 61, 61, 61, 62, 63, 62, 62, 62,
1318+ 49, 47, 47, 46, 47, 47, 48, 50, 51, 53, 53, 53, 53, 54, 54, 54,
1319+ 55, 55, 56, 58, 58, 60, 61, 61, 63, 63, 64, 63, 63, 64, 65, 66,
1320+ 48, 47, 46, 45, 46, 46, 46, 49, 51, 53, 54, 54, 55, 56, 56, 57,
1321+ 58, 59, 60, 61, 61, 63, 64, 64, 65, 65, 64, 65, 66, 66, 66, 66,
1322+ 48, 47, 46, 45, 46, 46, 46, 49, 51, 53, 54, 55, 56, 57, 57, 58,
1323+ 59, 60, 61, 62, 63, 65, 65, 65, 66, 67, 68, 67, 67, 67, 68, 69,
1324+ 49, 47, 46, 45, 45, 45, 45, 49, 51, 53, 55, 56, 58, 59, 60, 61,
1325+ 62, 62, 63, 65, 65, 67, 68, 68, 69, 70, 69, 69, 69, 70, 69, 69,
1326+ 50, 48, 47, 46, 46, 46, 46, 50, 51, 54, 56, 57, 59, 61, 62, 63,
1327+ 64, 65, 66, 68, 68, 70, 71, 71, 72, 71, 71, 72, 71, 71, 71, 72,
1328+ 51, 48, 47, 46, 47, 46, 46, 50, 51, 54, 56, 57, 60, 62, 62, 64,
1329+ 65, 66, 67, 69, 69, 71, 72, 72, 73, 74, 74, 72, 73, 74, 73, 73,
1330+ 52, 50, 48, 47, 47, 47, 47, 50, 52, 54, 57, 58, 61, 63, 64, 66,
1331+ 68, 68, 70, 72, 72, 75, 75, 75, 77, 76, 75, 76, 76, 74, 75, 76,
1332+ 54, 51, 50, 49, 49, 48, 48, 51, 53, 55, 58, 59, 62, 64, 65, 68,
1333+ 70, 70, 73, 74, 75, 77, 78, 78, 79, 78, 79, 78, 77, 78, 77, 77,
1334+ 55, 52, 51, 50, 49, 49, 48, 52, 53, 55, 59, 60, 62, 65, 66, 68,
1335+ 70, 71, 73, 75, 76, 78, 79, 79, 80, 81, 80, 80, 81, 79, 79, 81,
1336+ 57, 54, 53, 52, 51, 50, 50, 53, 54, 56, 60, 61, 63, 66, 67, 70,
1337+ 73, 73, 76, 78, 79, 82, 82, 83, 84, 83, 83, 83, 82, 83, 82, 81,
1338+ 59, 56, 54, 53, 53, 52, 51, 54, 56, 58, 61, 62, 65, 68, 69, 72,
1339+ 74, 75, 78, 80, 81, 84, 85, 85, 86, 86, 86, 84, 85, 84, 84, 85,
1340+ 60, 57, 55, 54, 53, 53, 52, 55, 56, 58, 61, 63, 65, 68, 69, 72,
1341+ 75, 76, 79, 81, 82, 85, 86, 86, 88, 88, 87, 88, 86, 87, 87, 85,
1342+ 63, 60, 58, 57, 56, 55, 54, 57, 59, 60, 63, 65, 67, 70, 71, 75,
1343+ 77, 78, 82, 84, 85, 89, 89, 90, 92, 89, 91, 89, 90, 89, 88, 89,
1344+ 64, 61, 59, 58, 57, 56, 55, 58, 59, 61, 64, 65, 68, 71, 72, 75,
1345+ 78, 79, 82, 85, 86, 89, 90, 91, 93, 94, 92, 92, 91, 91, 92, 90,
1346+ 65, 61, 60, 58, 57, 56, 55, 58, 59, 61, 64, 65, 68, 71, 72, 75,
1347+ 78, 79, 83, 85, 86, 90, 91, 91, 93, 94, 95, 94, 94, 94, 93, 94,
1348+ 67, 63, 61, 60, 59, 58, 57, 60, 61, 63, 65, 66, 69, 72, 73, 77,
1349+ 79, 80, 84, 86, 88, 92, 93, 93, 95, 95, 96, 97, 96, 95, 96, 94,
1350+ 68, 64, 63, 61, 60, 59, 58, 60, 61, 63, 65, 67, 70, 71, 74, 76,
1351+ 78, 81, 83, 86, 88, 89, 94, 94, 95, 97, 97, 98, 99, 99, 97, 99,
1352+ 68, 65, 64, 62, 61, 60, 58, 59, 61, 64, 64, 68, 69, 71, 74, 75,
1353+ 79, 80, 83, 86, 87, 91, 92, 95, 96, 97, 99, 99, 100, 100, 101, 99,
1354+ 69, 66, 65, 63, 62, 61, 59, 59, 62, 63, 65, 67, 69, 72, 72, 76,
1355+ 78, 80, 83, 84, 88, 89, 92, 94, 97, 98, 99, 101, 100, 102, 102, 104,
1356+ 70, 67, 66, 63, 63, 62, 61, 60, 63, 63, 66, 67, 69, 71, 73, 76,
1357+ 77, 81, 82, 85, 86, 90, 91, 94, 96, 99, 100, 100, 103, 102, 104, 104,
1358+ 71, 67, 67, 64, 64, 63, 62, 61, 62, 64, 66, 67, 70, 71, 74, 74,
1359+ 78, 79, 83, 84, 87, 89, 91, 94, 95, 99, 100, 102, 102, 104, 104, 106,
1360+ 72, 68, 68, 65, 65, 64, 63, 61, 62, 65, 66, 68, 69, 71, 73, 75,
1361+ 77, 79, 82, 84, 87, 88, 92, 93, 96, 97, 101, 102, 104, 104, 106, 106,
1362+ 73, 69, 69, 66, 66, 64, 64, 62, 62, 66, 66, 69, 69, 72, 73, 76,
1363+ 77, 81, 81, 85, 85, 89, 90, 94, 94, 99, 99, 104, 104, 106, 106, 108,
1364+ // Size 4x8
1365+ 31, 38, 46, 46, 50, 57, 61, 65, 47, 46, 53, 56, 59, 64, 65, 67,
1366+ 54, 50, 57, 66, 74, 82, 85, 82, 64, 60, 62, 71, 79, 88, 97, 99,
1367+ // Size 8x4
1368+ 31, 47, 54, 64, 38, 46, 50, 60, 46, 53, 57, 62, 46, 56, 66, 71,
1369+ 50, 59, 74, 79, 57, 64, 82, 88, 61, 65, 85, 97, 65, 67, 82, 99,
1370+ // Size 8x16
1371+ 32, 31, 33, 37, 44, 48, 49, 51, 54, 57, 60, 64, 66, 68, 70, 72,
1372+ 34, 36, 40, 44, 46, 46, 45, 47, 49, 51, 53, 57, 59, 61, 63, 65,
1373+ 48, 46, 47, 47, 51, 53, 53, 54, 55, 56, 58, 61, 63, 63, 63, 63,
1374+ 49, 46, 46, 45, 51, 56, 58, 60, 62, 64, 65, 68, 69, 71, 70, 69,
1375+ 54, 50, 49, 48, 53, 58, 62, 65, 70, 73, 75, 78, 79, 79, 80, 77,
1376+ 63, 58, 56, 54, 59, 64, 67, 71, 77, 82, 85, 89, 91, 87, 89, 86,
1377+ 67, 62, 59, 57, 60, 64, 70, 73, 77, 83, 89, 93, 94, 96, 97, 95,
1378+ 69, 65, 62, 60, 61, 64, 68, 72, 76, 81, 85, 89, 93, 98, 100, 102,
1379+ // Size 16x8
1380+ 32, 34, 48, 49, 54, 63, 67, 69, 31, 36, 46, 46, 50, 58, 62, 65,
1381+ 33, 40, 47, 46, 49, 56, 59, 62, 37, 44, 47, 45, 48, 54, 57, 60,
1382+ 44, 46, 51, 51, 53, 59, 60, 61, 48, 46, 53, 56, 58, 64, 64, 64,
1383+ 49, 45, 53, 58, 62, 67, 70, 68, 51, 47, 54, 60, 65, 71, 73, 72,
1384+ 54, 49, 55, 62, 70, 77, 77, 76, 57, 51, 56, 64, 73, 82, 83, 81,
1385+ 60, 53, 58, 65, 75, 85, 89, 85, 64, 57, 61, 68, 78, 89, 93, 89,
1386+ 66, 59, 63, 69, 79, 91, 94, 93, 68, 61, 63, 71, 79, 87, 96, 98,
1387+ 70, 63, 63, 70, 80, 89, 97, 100, 72, 65, 63, 69, 77, 86, 95, 102,
1388+ // Size 16x32
1389+ 32, 31, 31, 30, 33, 35, 37, 42, 44, 49, 48, 48, 49, 50, 51, 52,
1390+ 54, 54, 57, 59, 60, 63, 64, 64, 66, 67, 68, 69, 70, 71, 72, 73,
1391+ 31, 31, 32, 32, 36, 38, 40, 43, 44, 46, 46, 45, 45, 46, 47, 48,
1392+ 49, 50, 52, 54, 54, 57, 58, 59, 60, 61, 62, 63, 64, 65, 65, 66,
1393+ 34, 35, 36, 36, 40, 42, 44, 45, 46, 47, 46, 46, 45, 46, 47, 47,
1394+ 49, 49, 51, 52, 53, 56, 57, 57, 59, 60, 61, 62, 63, 64, 65, 66,
1395+ 37, 38, 39, 40, 43, 45, 47, 47, 47, 48, 47, 46, 46, 46, 47, 47,
1396+ 48, 49, 50, 52, 52, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63,
1397+ 48, 47, 46, 46, 47, 47, 47, 50, 51, 53, 53, 53, 53, 54, 54, 54,
1398+ 55, 55, 56, 58, 58, 60, 61, 61, 63, 63, 63, 63, 63, 63, 63, 63,
1399+ 48, 47, 46, 45, 46, 46, 46, 50, 51, 53, 54, 55, 56, 56, 57, 57,
1400+ 58, 59, 60, 61, 62, 64, 64, 65, 66, 65, 64, 65, 66, 67, 68, 69,
1401+ 49, 47, 46, 45, 46, 45, 45, 49, 51, 53, 56, 56, 58, 59, 60, 61,
1402+ 62, 62, 64, 65, 65, 67, 68, 68, 69, 70, 71, 71, 70, 70, 69, 69,
1403+ 52, 50, 48, 48, 47, 47, 47, 50, 52, 54, 57, 58, 61, 63, 64, 66,
1404+ 68, 68, 70, 72, 72, 75, 75, 75, 77, 75, 74, 72, 73, 74, 75, 76,
1405+ 54, 51, 50, 49, 49, 48, 48, 51, 53, 55, 58, 59, 62, 65, 65, 68,
1406+ 70, 70, 73, 74, 75, 77, 78, 78, 79, 78, 79, 80, 80, 78, 77, 76,
1407+ 57, 54, 53, 52, 51, 50, 50, 53, 54, 57, 60, 61, 64, 66, 68, 71,
1408+ 73, 74, 76, 78, 79, 82, 82, 83, 84, 85, 84, 82, 81, 82, 83, 84,
1409+ 63, 60, 58, 57, 56, 55, 54, 57, 59, 60, 64, 65, 67, 70, 71, 75,
1410+ 77, 78, 82, 84, 85, 89, 89, 90, 91, 88, 87, 88, 89, 88, 86, 84,
1411+ 64, 61, 59, 58, 57, 56, 55, 58, 59, 61, 64, 65, 68, 71, 72, 75,
1412+ 78, 79, 82, 85, 86, 90, 90, 91, 93, 93, 94, 93, 90, 90, 92, 93,
1413+ 67, 63, 62, 60, 59, 58, 57, 59, 60, 63, 64, 66, 70, 70, 73, 76,
1414+ 77, 81, 83, 85, 89, 90, 93, 94, 94, 96, 96, 96, 97, 97, 95, 93,
1415+ 68, 64, 63, 61, 60, 60, 58, 58, 61, 62, 64, 66, 67, 71, 71, 75,
1416+ 77, 79, 82, 83, 87, 88, 91, 93, 95, 97, 97, 99, 99, 99, 100, 101,
1417+ 69, 65, 65, 62, 62, 61, 60, 59, 61, 62, 64, 65, 68, 68, 72, 73,
1418+ 76, 77, 81, 82, 85, 87, 89, 92, 93, 97, 98, 100, 100, 102, 102, 101,
1419+ 69, 66, 66, 63, 63, 61, 61, 59, 60, 63, 63, 66, 66, 70, 70, 73,
1420+ 74, 78, 78, 82, 82, 86, 87, 91, 91, 95, 96, 101, 101, 103, 103, 105,
1421+ // Size 32x16
1422+ 32, 31, 34, 37, 48, 48, 49, 52, 54, 57, 63, 64, 67, 68, 69, 69,
1423+ 31, 31, 35, 38, 47, 47, 47, 50, 51, 54, 60, 61, 63, 64, 65, 66,
1424+ 31, 32, 36, 39, 46, 46, 46, 48, 50, 53, 58, 59, 62, 63, 65, 66,
1425+ 30, 32, 36, 40, 46, 45, 45, 48, 49, 52, 57, 58, 60, 61, 62, 63,
1426+ 33, 36, 40, 43, 47, 46, 46, 47, 49, 51, 56, 57, 59, 60, 62, 63,
1427+ 35, 38, 42, 45, 47, 46, 45, 47, 48, 50, 55, 56, 58, 60, 61, 61,
1428+ 37, 40, 44, 47, 47, 46, 45, 47, 48, 50, 54, 55, 57, 58, 60, 61,
1429+ 42, 43, 45, 47, 50, 50, 49, 50, 51, 53, 57, 58, 59, 58, 59, 59,
1430+ 44, 44, 46, 47, 51, 51, 51, 52, 53, 54, 59, 59, 60, 61, 61, 60,
1431+ 49, 46, 47, 48, 53, 53, 53, 54, 55, 57, 60, 61, 63, 62, 62, 63,
1432+ 48, 46, 46, 47, 53, 54, 56, 57, 58, 60, 64, 64, 64, 64, 64, 63,
1433+ 48, 45, 46, 46, 53, 55, 56, 58, 59, 61, 65, 65, 66, 66, 65, 66,
1434+ 49, 45, 45, 46, 53, 56, 58, 61, 62, 64, 67, 68, 70, 67, 68, 66,
1435+ 50, 46, 46, 46, 54, 56, 59, 63, 65, 66, 70, 71, 70, 71, 68, 70,
1436+ 51, 47, 47, 47, 54, 57, 60, 64, 65, 68, 71, 72, 73, 71, 72, 70,
1437+ 52, 48, 47, 47, 54, 57, 61, 66, 68, 71, 75, 75, 76, 75, 73, 73,
1438+ 54, 49, 49, 48, 55, 58, 62, 68, 70, 73, 77, 78, 77, 77, 76, 74,
1439+ 54, 50, 49, 49, 55, 59, 62, 68, 70, 74, 78, 79, 81, 79, 77, 78,
1440+ 57, 52, 51, 50, 56, 60, 64, 70, 73, 76, 82, 82, 83, 82, 81, 78,
1441+ 59, 54, 52, 52, 58, 61, 65, 72, 74, 78, 84, 85, 85, 83, 82, 82,
1442+ 60, 54, 53, 52, 58, 62, 65, 72, 75, 79, 85, 86, 89, 87, 85, 82,
1443+ 63, 57, 56, 55, 60, 64, 67, 75, 77, 82, 89, 90, 90, 88, 87, 86,
1444+ 64, 58, 57, 55, 61, 64, 68, 75, 78, 82, 89, 90, 93, 91, 89, 87,
1445+ 64, 59, 57, 56, 61, 65, 68, 75, 78, 83, 90, 91, 94, 93, 92, 91,
1446+ 66, 60, 59, 57, 63, 66, 69, 77, 79, 84, 91, 93, 94, 95, 93, 91,
1447+ 67, 61, 60, 58, 63, 65, 70, 75, 78, 85, 88, 93, 96, 97, 97, 95,
1448+ 68, 62, 61, 59, 63, 64, 71, 74, 79, 84, 87, 94, 96, 97, 98, 96,
1449+ 69, 63, 62, 60, 63, 65, 71, 72, 80, 82, 88, 93, 96, 99, 100, 101,
1450+ 70, 64, 63, 60, 63, 66, 70, 73, 80, 81, 89, 90, 97, 99, 100, 101,
1451+ 71, 65, 64, 61, 63, 67, 70, 74, 78, 82, 88, 90, 97, 99, 102, 103,
1452+ 72, 65, 65, 62, 63, 68, 69, 75, 77, 83, 86, 92, 95, 100, 102, 103,
1453+ 73, 66, 66, 63, 63, 69, 69, 76, 76, 84, 84, 93, 93, 101, 101, 105,
1454+ // Size 4x16
1455+ 31, 32, 36, 40, 44, 46, 45, 47, 49, 52, 54, 58, 60, 62, 64, 65,
1456+ 48, 46, 46, 46, 51, 54, 56, 57, 58, 60, 62, 64, 66, 64, 66, 68,
1457+ 57, 53, 51, 50, 54, 60, 64, 68, 73, 76, 79, 82, 84, 84, 81, 83,
1458+ 68, 63, 60, 58, 61, 64, 67, 71, 77, 82, 87, 91, 95, 97, 99, 100,
1459+ // Size 16x4
1460+ 31, 48, 57, 68, 32, 46, 53, 63, 36, 46, 51, 60, 40, 46, 50, 58,
1461+ 44, 51, 54, 61, 46, 54, 60, 64, 45, 56, 64, 67, 47, 57, 68, 71,
1462+ 49, 58, 73, 77, 52, 60, 76, 82, 54, 62, 79, 87, 58, 64, 82, 91,
1463+ 60, 66, 84, 95, 62, 64, 84, 97, 64, 66, 81, 99, 65, 68, 83, 100,
1464+ // Size 8x32
1465+ 32, 31, 31, 30, 33, 35, 37, 42, 44, 49, 48, 48, 49, 50, 51, 52,
1466+ 54, 54, 57, 59, 60, 63, 64, 64, 66, 67, 68, 69, 70, 71, 72, 73,
1467+ 34, 35, 36, 36, 40, 42, 44, 45, 46, 47, 46, 46, 45, 46, 47, 47,
1468+ 49, 49, 51, 52, 53, 56, 57, 57, 59, 60, 61, 62, 63, 64, 65, 66,
1469+ 48, 47, 46, 46, 47, 47, 47, 50, 51, 53, 53, 53, 53, 54, 54, 54,
1470+ 55, 55, 56, 58, 58, 60, 61, 61, 63, 63, 63, 63, 63, 63, 63, 63,
1471+ 49, 47, 46, 45, 46, 45, 45, 49, 51, 53, 56, 56, 58, 59, 60, 61,
1472+ 62, 62, 64, 65, 65, 67, 68, 68, 69, 70, 71, 71, 70, 70, 69, 69,
1473+ 54, 51, 50, 49, 49, 48, 48, 51, 53, 55, 58, 59, 62, 65, 65, 68,
1474+ 70, 70, 73, 74, 75, 77, 78, 78, 79, 78, 79, 80, 80, 78, 77, 76,
1475+ 63, 60, 58, 57, 56, 55, 54, 57, 59, 60, 64, 65, 67, 70, 71, 75,
1476+ 77, 78, 82, 84, 85, 89, 89, 90, 91, 88, 87, 88, 89, 88, 86, 84,
1477+ 67, 63, 62, 60, 59, 58, 57, 59, 60, 63, 64, 66, 70, 70, 73, 76,
1478+ 77, 81, 83, 85, 89, 90, 93, 94, 94, 96, 96, 96, 97, 97, 95, 93,
1479+ 69, 65, 65, 62, 62, 61, 60, 59, 61, 62, 64, 65, 68, 68, 72, 73,
1480+ 76, 77, 81, 82, 85, 87, 89, 92, 93, 97, 98, 100, 100, 102, 102, 101,
1481+ // Size 32x8
1482+ 32, 34, 48, 49, 54, 63, 67, 69, 31, 35, 47, 47, 51, 60, 63, 65,
1483+ 31, 36, 46, 46, 50, 58, 62, 65, 30, 36, 46, 45, 49, 57, 60, 62,
1484+ 33, 40, 47, 46, 49, 56, 59, 62, 35, 42, 47, 45, 48, 55, 58, 61,
1485+ 37, 44, 47, 45, 48, 54, 57, 60, 42, 45, 50, 49, 51, 57, 59, 59,
1486+ 44, 46, 51, 51, 53, 59, 60, 61, 49, 47, 53, 53, 55, 60, 63, 62,
1487+ 48, 46, 53, 56, 58, 64, 64, 64, 48, 46, 53, 56, 59, 65, 66, 65,
1488+ 49, 45, 53, 58, 62, 67, 70, 68, 50, 46, 54, 59, 65, 70, 70, 68,
1489+ 51, 47, 54, 60, 65, 71, 73, 72, 52, 47, 54, 61, 68, 75, 76, 73,
1490+ 54, 49, 55, 62, 70, 77, 77, 76, 54, 49, 55, 62, 70, 78, 81, 77,
1491+ 57, 51, 56, 64, 73, 82, 83, 81, 59, 52, 58, 65, 74, 84, 85, 82,
1492+ 60, 53, 58, 65, 75, 85, 89, 85, 63, 56, 60, 67, 77, 89, 90, 87,
1493+ 64, 57, 61, 68, 78, 89, 93, 89, 64, 57, 61, 68, 78, 90, 94, 92,
1494+ 66, 59, 63, 69, 79, 91, 94, 93, 67, 60, 63, 70, 78, 88, 96, 97,
1495+ 68, 61, 63, 71, 79, 87, 96, 98, 69, 62, 63, 71, 80, 88, 96, 100,
1496+ 70, 63, 63, 70, 80, 89, 97, 100, 71, 64, 63, 70, 78, 88, 97, 102,
1497+ 72, 65, 63, 69, 77, 86, 95, 102, 73, 66, 63, 69, 76, 84, 93, 101,
1498+ ],
1499+ ],
1500+ // qm level 3
1501+ [
1502+ [// luma
1503+ // Size 4x4
1504+ 32, 37, 58, 81, 37, 54, 72, 91, 58, 72, 102, 121, 81, 91, 121, 156,
1505+ // Size 8x8
1506+ 32, 32, 35, 42, 53, 68, 78, 90, 32, 33, 36, 42, 51, 64, 74, 84,
1507+ 35, 36, 46, 52, 60, 72, 80, 87, 42, 42, 52, 63, 73, 84, 92, 98,
1508+ 53, 51, 60, 73, 86, 100, 109, 114, 68, 64, 72, 84, 100, 117, 128, 133,
1509+ 78, 74, 80, 92, 109, 128, 140, 155, 90, 84, 87, 98, 114, 133, 155, 168,
1510+ // Size 16x16
1511+ 32, 31, 31, 32, 34, 36, 41, 47, 54, 59, 65, 74, 82, 87, 92, 97,
1512+ 31, 32, 32, 32, 34, 35, 39, 45, 50, 55, 61, 69, 76, 81, 87, 92,
1513+ 31, 32, 33, 33, 35, 36, 40, 44, 49, 54, 59, 67, 73, 78, 83, 88,
1514+ 32, 32, 33, 35, 37, 38, 41, 45, 49, 53, 58, 65, 71, 75, 80, 86,
1515+ 34, 34, 35, 37, 39, 42, 46, 50, 54, 58, 63, 70, 76, 80, 84, 85,
1516+ 36, 35, 36, 38, 42, 48, 52, 56, 60, 64, 68, 75, 80, 85, 90, 91,
1517+ 41, 39, 40, 41, 46, 52, 57, 62, 67, 71, 75, 83, 88, 92, 95, 97,
1518+ 47, 45, 44, 45, 50, 56, 62, 69, 75, 79, 84, 91, 97, 100, 102, 104,
1519+ 54, 50, 49, 49, 54, 60, 67, 75, 82, 87, 92, 100, 106, 110, 109, 112,
1520+ 59, 55, 54, 53, 58, 64, 71, 79, 87, 92, 98, 106, 112, 117, 117, 121,
1521+ 65, 61, 59, 58, 63, 68, 75, 84, 92, 98, 105, 114, 120, 125, 126, 130,
1522+ 74, 69, 67, 65, 70, 75, 83, 91, 100, 106, 114, 123, 131, 135, 137, 140,
1523+ 82, 76, 73, 71, 76, 80, 88, 97, 106, 112, 120, 131, 139, 144, 148, 150,
1524+ 87, 81, 78, 75, 80, 85, 92, 100, 110, 117, 125, 135, 144, 150, 155, 162,
1525+ 92, 87, 83, 80, 84, 90, 95, 102, 109, 117, 126, 137, 148, 155, 162, 168,
1526+ 97, 92, 88, 86, 85, 91, 97, 104, 112, 121, 130, 140, 150, 162, 168, 174,
1527+ // Size 32x32
1528+ 32, 31, 31, 31, 31, 31, 32, 32, 34, 35, 36, 39, 41, 44, 47, 48,
1529+ 54, 56, 59, 64, 65, 71, 74, 80, 82, 83, 87, 90, 92, 95, 97, 100,
1530+ 31, 32, 32, 32, 32, 32, 32, 33, 34, 35, 35, 38, 40, 42, 45, 46,
1531+ 51, 53, 56, 61, 62, 68, 71, 76, 78, 78, 83, 85, 88, 90, 92, 95,
1532+ 31, 32, 32, 32, 32, 32, 32, 33, 34, 34, 35, 38, 39, 42, 45, 45,
1533+ 50, 52, 55, 60, 61, 67, 69, 74, 76, 77, 81, 84, 87, 89, 92, 95,
1534+ 31, 32, 32, 32, 32, 32, 32, 33, 33, 34, 34, 37, 38, 41, 44, 44,
1535+ 49, 51, 54, 58, 59, 65, 68, 72, 74, 75, 79, 81, 84, 86, 88, 90,
1536+ 31, 32, 32, 32, 33, 33, 33, 34, 35, 36, 36, 39, 40, 42, 44, 45,
1537+ 49, 51, 54, 58, 59, 64, 67, 71, 73, 74, 78, 80, 83, 85, 88, 90,
1538+ 31, 32, 32, 32, 33, 33, 34, 34, 35, 36, 36, 39, 40, 42, 45, 45,
1539+ 50, 51, 54, 58, 59, 64, 67, 71, 73, 74, 78, 80, 82, 84, 86, 89,
1540+ 32, 32, 32, 32, 33, 34, 35, 36, 37, 38, 38, 40, 41, 42, 45, 46,
1541+ 49, 51, 53, 57, 58, 63, 65, 69, 71, 72, 75, 78, 80, 83, 86, 89,
1542+ 32, 33, 33, 33, 34, 34, 36, 36, 38, 39, 40, 42, 43, 44, 47, 47,
1543+ 51, 53, 55, 59, 60, 65, 67, 71, 73, 73, 77, 78, 80, 82, 84, 86,
1544+ 34, 34, 34, 33, 35, 35, 37, 38, 39, 42, 42, 45, 46, 47, 50, 51,
1545+ 54, 56, 58, 62, 63, 68, 70, 74, 76, 76, 80, 82, 84, 85, 85, 86,
1546+ 35, 35, 34, 34, 36, 36, 38, 39, 42, 46, 47, 49, 50, 52, 55, 55,
1547+ 59, 60, 62, 66, 67, 72, 74, 78, 79, 80, 83, 84, 85, 87, 90, 92,
1548+ 36, 35, 35, 34, 36, 36, 38, 40, 42, 47, 48, 50, 52, 54, 56, 57,
1549+ 60, 61, 64, 67, 68, 73, 75, 79, 80, 81, 85, 87, 90, 91, 91, 92,
1550+ 39, 38, 38, 37, 39, 39, 40, 42, 45, 49, 50, 54, 55, 58, 60, 61,
1551+ 65, 66, 69, 72, 73, 78, 80, 84, 86, 86, 90, 91, 91, 92, 95, 97,
1552+ 41, 40, 39, 38, 40, 40, 41, 43, 46, 50, 52, 55, 57, 60, 62, 63,
1553+ 67, 69, 71, 75, 75, 80, 83, 86, 88, 89, 92, 93, 95, 97, 97, 98,
1554+ 44, 42, 42, 41, 42, 42, 42, 44, 47, 52, 54, 58, 60, 63, 66, 67,
1555+ 71, 73, 75, 79, 79, 84, 86, 90, 92, 92, 96, 98, 98, 98, 101, 104,
1556+ 47, 45, 45, 44, 44, 45, 45, 47, 50, 55, 56, 60, 62, 66, 69, 70,
1557+ 75, 77, 79, 83, 84, 89, 91, 95, 97, 97, 100, 99, 102, 105, 104, 104,
1558+ 48, 46, 45, 44, 45, 45, 46, 47, 51, 55, 57, 61, 63, 67, 70, 71,
1559+ 76, 78, 80, 84, 85, 90, 93, 96, 98, 99, 102, 106, 106, 105, 108, 111,
1560+ 54, 51, 50, 49, 49, 50, 49, 51, 54, 59, 60, 65, 67, 71, 75, 76,
1561+ 82, 84, 87, 91, 92, 97, 100, 104, 106, 106, 110, 108, 109, 112, 112, 111,
1562+ 56, 53, 52, 51, 51, 51, 51, 53, 56, 60, 61, 66, 69, 73, 77, 78,
1563+ 84, 86, 89, 93, 94, 100, 102, 106, 108, 109, 112, 113, 115, 114, 116, 119,
1564+ 59, 56, 55, 54, 54, 54, 53, 55, 58, 62, 64, 69, 71, 75, 79, 80,
1565+ 87, 89, 92, 97, 98, 103, 106, 110, 112, 113, 117, 118, 117, 121, 121, 119,
1566+ 64, 61, 60, 58, 58, 58, 57, 59, 62, 66, 67, 72, 75, 79, 83, 84,
1567+ 91, 93, 97, 102, 103, 109, 112, 116, 118, 119, 122, 121, 125, 123, 125, 128,
1568+ 65, 62, 61, 59, 59, 59, 58, 60, 63, 67, 68, 73, 75, 79, 84, 85,
1569+ 92, 94, 98, 103, 105, 111, 114, 118, 120, 121, 125, 129, 126, 129, 130, 129,
1570+ 71, 68, 67, 65, 64, 64, 63, 65, 68, 72, 73, 78, 80, 84, 89, 90,
1571+ 97, 100, 103, 109, 111, 117, 120, 125, 127, 128, 133, 130, 134, 133, 133, 137,
1572+ 74, 71, 69, 68, 67, 67, 65, 67, 70, 74, 75, 80, 83, 86, 91, 93,
1573+ 100, 102, 106, 112, 114, 120, 123, 128, 131, 131, 135, 137, 137, 138, 140, 137,
1574+ 80, 76, 74, 72, 71, 71, 69, 71, 74, 78, 79, 84, 86, 90, 95, 96,
1575+ 104, 106, 110, 116, 118, 125, 128, 134, 136, 137, 142, 141, 142, 143, 143, 147,
1576+ 82, 78, 76, 74, 73, 73, 71, 73, 76, 79, 80, 86, 88, 92, 97, 98,
1577+ 106, 108, 112, 118, 120, 127, 131, 136, 139, 139, 144, 147, 148, 147, 150, 148,
1578+ 83, 78, 77, 75, 74, 74, 72, 73, 76, 80, 81, 86, 89, 92, 97, 99,
1579+ 106, 109, 113, 119, 121, 128, 131, 137, 139, 140, 145, 150, 152, 155, 152, 157,
1580+ 87, 83, 81, 79, 78, 78, 75, 77, 80, 83, 85, 90, 92, 96, 100, 102,
1581+ 110, 112, 117, 122, 125, 133, 135, 142, 144, 145, 150, 151, 155, 158, 162, 158,
1582+ 90, 85, 84, 81, 80, 80, 78, 78, 82, 84, 87, 91, 93, 98, 99, 106,
1583+ 108, 113, 118, 121, 129, 130, 137, 141, 147, 150, 151, 156, 156, 161, 164, 169,
1584+ 92, 88, 87, 84, 83, 82, 80, 80, 84, 85, 90, 91, 95, 98, 102, 106,
1585+ 109, 115, 117, 125, 126, 134, 137, 142, 148, 152, 155, 156, 162, 162, 168, 170,
1586+ 95, 90, 89, 86, 85, 84, 83, 82, 85, 87, 91, 92, 97, 98, 105, 105,
1587+ 112, 114, 121, 123, 129, 133, 138, 143, 147, 155, 158, 161, 162, 168, 168, 174,
1588+ 97, 92, 92, 88, 88, 86, 86, 84, 85, 90, 91, 95, 97, 101, 104, 108,
1589+ 112, 116, 121, 125, 130, 133, 140, 143, 150, 152, 162, 164, 168, 168, 174, 175,
1590+ 100, 95, 95, 90, 90, 89, 89, 86, 86, 92, 92, 97, 98, 104, 104, 111,
1591+ 111, 119, 119, 128, 129, 137, 137, 147, 148, 157, 158, 169, 170, 174, 175, 181,
1592+ // Size 4x8
1593+ 32, 32, 34, 41, 51, 65, 75, 86, 35, 36, 47, 53, 61, 73, 81, 92,
1594+ 59, 57, 65, 78, 92, 108, 117, 119, 83, 78, 82, 97, 111, 129, 148, 154,
1595+ // Size 8x4
1596+ 32, 35, 59, 83, 32, 36, 57, 78, 34, 47, 65, 82, 41, 53, 78, 97,
1597+ 51, 61, 92, 111, 65, 73, 108, 129, 75, 81, 117, 148, 86, 92, 119, 154,
1598+ // Size 8x16
1599+ 32, 31, 31, 32, 34, 36, 41, 47, 53, 58, 65, 74, 82, 87, 92, 97,
1600+ 31, 32, 33, 34, 35, 36, 40, 44, 50, 54, 59, 67, 73, 78, 83, 88,
1601+ 35, 34, 35, 37, 41, 46, 49, 53, 57, 61, 66, 73, 79, 83, 84, 86,
1602+ 44, 41, 42, 42, 48, 54, 60, 66, 71, 75, 79, 86, 92, 96, 97, 97,
1603+ 53, 50, 49, 49, 54, 60, 67, 75, 82, 87, 92, 100, 105, 110, 114, 111,
1604+ 65, 61, 59, 58, 63, 68, 76, 84, 92, 98, 105, 113, 120, 125, 132, 128,
1605+ 82, 76, 73, 71, 76, 80, 88, 97, 106, 112, 120, 131, 139, 144, 150, 147,
1606+ 90, 85, 81, 79, 81, 87, 93, 101, 108, 116, 124, 134, 142, 153, 157, 163,
1607+ // Size 16x8
1608+ 32, 31, 35, 44, 53, 65, 82, 90, 31, 32, 34, 41, 50, 61, 76, 85,
1609+ 31, 33, 35, 42, 49, 59, 73, 81, 32, 34, 37, 42, 49, 58, 71, 79,
1610+ 34, 35, 41, 48, 54, 63, 76, 81, 36, 36, 46, 54, 60, 68, 80, 87,
1611+ 41, 40, 49, 60, 67, 76, 88, 93, 47, 44, 53, 66, 75, 84, 97, 101,
1612+ 53, 50, 57, 71, 82, 92, 106, 108, 58, 54, 61, 75, 87, 98, 112, 116,
1613+ 65, 59, 66, 79, 92, 105, 120, 124, 74, 67, 73, 86, 100, 113, 131, 134,
1614+ 82, 73, 79, 92, 105, 120, 139, 142, 87, 78, 83, 96, 110, 125, 144, 153,
1615+ 92, 83, 84, 97, 114, 132, 150, 157, 97, 88, 86, 97, 111, 128, 147, 163,
1616+ // Size 16x32
1617+ 32, 31, 31, 31, 31, 32, 32, 32, 34, 35, 36, 39, 41, 44, 47, 48,
1618+ 53, 55, 58, 63, 65, 71, 74, 79, 82, 82, 87, 89, 92, 94, 97, 99,
1619+ 31, 32, 32, 32, 32, 32, 33, 33, 34, 34, 34, 37, 39, 41, 44, 45,
1620+ 49, 51, 54, 58, 60, 65, 68, 72, 75, 75, 79, 82, 84, 86, 88, 91,
1621+ 31, 32, 32, 32, 33, 33, 34, 34, 35, 36, 36, 39, 40, 42, 44, 45,
1622+ 50, 51, 54, 58, 59, 64, 67, 71, 73, 74, 78, 81, 83, 85, 88, 91,
1623+ 32, 32, 32, 33, 34, 34, 35, 36, 37, 38, 38, 40, 41, 43, 45, 46,
1624+ 50, 51, 54, 57, 58, 63, 66, 70, 72, 72, 76, 78, 80, 82, 85, 87,
1625+ 35, 35, 34, 34, 35, 36, 37, 39, 41, 45, 46, 48, 49, 51, 53, 54,
1626+ 57, 59, 61, 65, 66, 71, 73, 77, 79, 79, 83, 83, 84, 85, 86, 87,
1627+ 36, 35, 35, 34, 36, 36, 38, 40, 42, 47, 48, 50, 51, 53, 56, 56,
1628+ 60, 61, 63, 67, 68, 73, 75, 79, 81, 81, 85, 87, 89, 92, 94, 97,
1629+ 44, 42, 41, 41, 42, 42, 42, 44, 48, 52, 54, 58, 60, 63, 66, 67,
1630+ 71, 72, 75, 78, 79, 84, 86, 90, 92, 92, 96, 97, 97, 97, 97, 97,
1631+ 47, 45, 45, 44, 44, 45, 45, 47, 50, 55, 56, 60, 62, 66, 69, 70,
1632+ 75, 77, 79, 83, 84, 89, 91, 95, 97, 97, 100, 99, 101, 104, 107, 110,
1633+ 53, 51, 50, 49, 49, 50, 49, 51, 54, 59, 60, 65, 67, 71, 75, 76,
1634+ 82, 84, 87, 91, 92, 97, 100, 104, 105, 106, 110, 113, 114, 112, 111, 110,
1635+ 62, 59, 58, 57, 57, 57, 56, 58, 61, 65, 66, 71, 74, 78, 82, 83,
1636+ 90, 92, 95, 100, 102, 108, 110, 115, 117, 117, 120, 118, 116, 119, 123, 126,
1637+ 65, 62, 61, 59, 59, 59, 58, 60, 63, 67, 68, 73, 76, 79, 84, 85,
1638+ 92, 94, 98, 103, 105, 111, 113, 118, 120, 121, 125, 128, 132, 130, 128, 126,
1639+ 79, 75, 74, 72, 71, 71, 69, 71, 73, 77, 78, 84, 86, 90, 95, 96,
1640+ 103, 106, 110, 116, 118, 125, 128, 133, 136, 136, 141, 139, 135, 136, 140, 144,
1641+ 82, 78, 76, 74, 73, 73, 71, 73, 76, 79, 80, 86, 88, 92, 97, 98,
1642+ 106, 108, 112, 118, 120, 127, 131, 136, 139, 139, 144, 145, 150, 151, 147, 144,
1643+ 88, 83, 82, 79, 79, 78, 76, 76, 81, 82, 85, 89, 91, 97, 98, 104,
1644+ 107, 111, 117, 119, 127, 129, 135, 140, 145, 148, 148, 153, 153, 154, 159, 163,
1645+ 90, 86, 85, 82, 81, 80, 79, 78, 81, 83, 87, 88, 93, 94, 101, 101,
1646+ 108, 110, 116, 119, 124, 129, 134, 139, 142, 150, 153, 157, 157, 163, 163, 163,
1647+ 93, 88, 88, 84, 84, 82, 83, 80, 80, 86, 86, 91, 91, 97, 98, 105,
1648+ 105, 112, 113, 121, 122, 130, 130, 140, 140, 149, 150, 161, 162, 166, 167, 173,
1649+ // Size 32x16
1650+ 32, 31, 31, 32, 35, 36, 44, 47, 53, 62, 65, 79, 82, 88, 90, 93,
1651+ 31, 32, 32, 32, 35, 35, 42, 45, 51, 59, 62, 75, 78, 83, 86, 88,
1652+ 31, 32, 32, 32, 34, 35, 41, 45, 50, 58, 61, 74, 76, 82, 85, 88,
1653+ 31, 32, 32, 33, 34, 34, 41, 44, 49, 57, 59, 72, 74, 79, 82, 84,
1654+ 31, 32, 33, 34, 35, 36, 42, 44, 49, 57, 59, 71, 73, 79, 81, 84,
1655+ 32, 32, 33, 34, 36, 36, 42, 45, 50, 57, 59, 71, 73, 78, 80, 82,
1656+ 32, 33, 34, 35, 37, 38, 42, 45, 49, 56, 58, 69, 71, 76, 79, 83,
1657+ 32, 33, 34, 36, 39, 40, 44, 47, 51, 58, 60, 71, 73, 76, 78, 80,
1658+ 34, 34, 35, 37, 41, 42, 48, 50, 54, 61, 63, 73, 76, 81, 81, 80,
1659+ 35, 34, 36, 38, 45, 47, 52, 55, 59, 65, 67, 77, 79, 82, 83, 86,
1660+ 36, 34, 36, 38, 46, 48, 54, 56, 60, 66, 68, 78, 80, 85, 87, 86,
1661+ 39, 37, 39, 40, 48, 50, 58, 60, 65, 71, 73, 84, 86, 89, 88, 91,
1662+ 41, 39, 40, 41, 49, 51, 60, 62, 67, 74, 76, 86, 88, 91, 93, 91,
1663+ 44, 41, 42, 43, 51, 53, 63, 66, 71, 78, 79, 90, 92, 97, 94, 97,
1664+ 47, 44, 44, 45, 53, 56, 66, 69, 75, 82, 84, 95, 97, 98, 101, 98,
1665+ 48, 45, 45, 46, 54, 56, 67, 70, 76, 83, 85, 96, 98, 104, 101, 105,
1666+ 53, 49, 50, 50, 57, 60, 71, 75, 82, 90, 92, 103, 106, 107, 108, 105,
1667+ 55, 51, 51, 51, 59, 61, 72, 77, 84, 92, 94, 106, 108, 111, 110, 112,
1668+ 58, 54, 54, 54, 61, 63, 75, 79, 87, 95, 98, 110, 112, 117, 116, 113,
1669+ 63, 58, 58, 57, 65, 67, 78, 83, 91, 100, 103, 116, 118, 119, 119, 121,
1670+ 65, 60, 59, 58, 66, 68, 79, 84, 92, 102, 105, 118, 120, 127, 124, 122,
1671+ 71, 65, 64, 63, 71, 73, 84, 89, 97, 108, 111, 125, 127, 129, 129, 130,
1672+ 74, 68, 67, 66, 73, 75, 86, 91, 100, 110, 113, 128, 131, 135, 134, 130,
1673+ 79, 72, 71, 70, 77, 79, 90, 95, 104, 115, 118, 133, 136, 140, 139, 140,
1674+ 82, 75, 73, 72, 79, 81, 92, 97, 105, 117, 120, 136, 139, 145, 142, 140,
1675+ 82, 75, 74, 72, 79, 81, 92, 97, 106, 117, 121, 136, 139, 148, 150, 149,
1676+ 87, 79, 78, 76, 83, 85, 96, 100, 110, 120, 125, 141, 144, 148, 153, 150,
1677+ 89, 82, 81, 78, 83, 87, 97, 99, 113, 118, 128, 139, 145, 153, 157, 161,
1678+ 92, 84, 83, 80, 84, 89, 97, 101, 114, 116, 132, 135, 150, 153, 157, 162,
1679+ 94, 86, 85, 82, 85, 92, 97, 104, 112, 119, 130, 136, 151, 154, 163, 166,
1680+ 97, 88, 88, 85, 86, 94, 97, 107, 111, 123, 128, 140, 147, 159, 163, 167,
1681+ 99, 91, 91, 87, 87, 97, 97, 110, 110, 126, 126, 144, 144, 163, 163, 173,
1682+ // Size 4x16
1683+ 31, 32, 32, 33, 34, 34, 39, 44, 49, 54, 60, 68, 75, 79, 84, 88,
1684+ 36, 35, 36, 38, 42, 48, 51, 56, 60, 63, 68, 75, 81, 85, 89, 94,
1685+ 62, 58, 57, 56, 61, 66, 74, 82, 90, 95, 102, 110, 117, 120, 116, 123,
1686+ 88, 82, 79, 76, 81, 85, 91, 98, 107, 117, 127, 135, 145, 148, 153, 159,
1687+ // Size 16x4
1688+ 31, 36, 62, 88, 32, 35, 58, 82, 32, 36, 57, 79, 33, 38, 56, 76,
1689+ 34, 42, 61, 81, 34, 48, 66, 85, 39, 51, 74, 91, 44, 56, 82, 98,
1690+ 49, 60, 90, 107, 54, 63, 95, 117, 60, 68, 102, 127, 68, 75, 110, 135,
1691+ 75, 81, 117, 145, 79, 85, 120, 148, 84, 89, 116, 153, 88, 94, 123, 159,
1692+ // Size 8x32
1693+ 32, 31, 31, 31, 31, 32, 32, 32, 34, 35, 36, 39, 41, 44, 47, 48,
1694+ 53, 55, 58, 63, 65, 71, 74, 79, 82, 82, 87, 89, 92, 94, 97, 99,
1695+ 31, 32, 32, 32, 33, 33, 34, 34, 35, 36, 36, 39, 40, 42, 44, 45,
1696+ 50, 51, 54, 58, 59, 64, 67, 71, 73, 74, 78, 81, 83, 85, 88, 91,
1697+ 35, 35, 34, 34, 35, 36, 37, 39, 41, 45, 46, 48, 49, 51, 53, 54,
1698+ 57, 59, 61, 65, 66, 71, 73, 77, 79, 79, 83, 83, 84, 85, 86, 87,
1699+ 44, 42, 41, 41, 42, 42, 42, 44, 48, 52, 54, 58, 60, 63, 66, 67,
1700+ 71, 72, 75, 78, 79, 84, 86, 90, 92, 92, 96, 97, 97, 97, 97, 97,
1701+ 53, 51, 50, 49, 49, 50, 49, 51, 54, 59, 60, 65, 67, 71, 75, 76,
1702+ 82, 84, 87, 91, 92, 97, 100, 104, 105, 106, 110, 113, 114, 112, 111, 110,
1703+ 65, 62, 61, 59, 59, 59, 58, 60, 63, 67, 68, 73, 76, 79, 84, 85,
1704+ 92, 94, 98, 103, 105, 111, 113, 118, 120, 121, 125, 128, 132, 130, 128, 126,
1705+ 82, 78, 76, 74, 73, 73, 71, 73, 76, 79, 80, 86, 88, 92, 97, 98,
1706+ 106, 108, 112, 118, 120, 127, 131, 136, 139, 139, 144, 145, 150, 151, 147, 144,
1707+ 90, 86, 85, 82, 81, 80, 79, 78, 81, 83, 87, 88, 93, 94, 101, 101,
1708+ 108, 110, 116, 119, 124, 129, 134, 139, 142, 150, 153, 157, 157, 163, 163, 163,
1709+ // Size 32x8
1710+ 32, 31, 35, 44, 53, 65, 82, 90, 31, 32, 35, 42, 51, 62, 78, 86,
1711+ 31, 32, 34, 41, 50, 61, 76, 85, 31, 32, 34, 41, 49, 59, 74, 82,
1712+ 31, 33, 35, 42, 49, 59, 73, 81, 32, 33, 36, 42, 50, 59, 73, 80,
1713+ 32, 34, 37, 42, 49, 58, 71, 79, 32, 34, 39, 44, 51, 60, 73, 78,
1714+ 34, 35, 41, 48, 54, 63, 76, 81, 35, 36, 45, 52, 59, 67, 79, 83,
1715+ 36, 36, 46, 54, 60, 68, 80, 87, 39, 39, 48, 58, 65, 73, 86, 88,
1716+ 41, 40, 49, 60, 67, 76, 88, 93, 44, 42, 51, 63, 71, 79, 92, 94,
1717+ 47, 44, 53, 66, 75, 84, 97, 101, 48, 45, 54, 67, 76, 85, 98, 101,
1718+ 53, 50, 57, 71, 82, 92, 106, 108, 55, 51, 59, 72, 84, 94, 108, 110,
1719+ 58, 54, 61, 75, 87, 98, 112, 116, 63, 58, 65, 78, 91, 103, 118, 119,
1720+ 65, 59, 66, 79, 92, 105, 120, 124, 71, 64, 71, 84, 97, 111, 127, 129,
1721+ 74, 67, 73, 86, 100, 113, 131, 134, 79, 71, 77, 90, 104, 118, 136, 139,
1722+ 82, 73, 79, 92, 105, 120, 139, 142, 82, 74, 79, 92, 106, 121, 139, 150,
1723+ 87, 78, 83, 96, 110, 125, 144, 153, 89, 81, 83, 97, 113, 128, 145, 157,
1724+ 92, 83, 84, 97, 114, 132, 150, 157, 94, 85, 85, 97, 112, 130, 151, 163,
1725+ 97, 88, 86, 97, 111, 128, 147, 163, 99, 91, 87, 97, 110, 126, 144, 163,
1726+ ],
1727+ [// chroma
1728+ // Size 4x4
1729+ 32, 45, 51, 61, 45, 54, 59, 65, 51, 59, 75, 81, 61, 65, 81, 97,
1730+ // Size 8x8
1731+ 31, 34, 46, 47, 50, 57, 61, 65, 34, 39, 47, 45, 48, 53, 57, 61,
1732+ 46, 47, 52, 52, 54, 58, 61, 62, 47, 45, 52, 58, 62, 65, 68, 68,
1733+ 50, 48, 54, 62, 68, 73, 77, 76, 57, 53, 58, 65, 73, 82, 86, 86,
1734+ 61, 57, 61, 68, 77, 86, 91, 95, 65, 61, 62, 68, 76, 86, 95, 100,
1735+ // Size 16x16
1736+ 32, 31, 33, 36, 41, 49, 49, 50, 52, 54, 57, 61, 64, 67, 68, 70,
1737+ 31, 31, 34, 39, 42, 47, 46, 47, 49, 51, 53, 57, 60, 62, 64, 66,
1738+ 33, 34, 37, 42, 44, 47, 46, 46, 47, 49, 51, 55, 57, 59, 61, 63,
1739+ 36, 39, 42, 47, 47, 48, 46, 46, 47, 48, 50, 53, 55, 57, 59, 61,
1740+ 41, 42, 44, 47, 48, 50, 49, 50, 50, 52, 53, 56, 58, 60, 61, 60,
1741+ 49, 47, 47, 48, 50, 53, 53, 54, 54, 55, 56, 59, 61, 63, 64, 64,
1742+ 49, 46, 46, 46, 49, 53, 55, 57, 59, 60, 61, 64, 66, 67, 67, 67,
1743+ 50, 47, 46, 46, 50, 54, 57, 61, 63, 64, 66, 69, 70, 72, 71, 71,
1744+ 52, 49, 47, 47, 50, 54, 59, 63, 66, 68, 70, 73, 75, 77, 75, 75,
1745+ 54, 51, 49, 48, 52, 55, 60, 64, 68, 71, 73, 76, 79, 80, 79, 79,
1746+ 57, 53, 51, 50, 53, 56, 61, 66, 70, 73, 76, 80, 82, 84, 83, 84,
1747+ 61, 57, 55, 53, 56, 59, 64, 69, 73, 76, 80, 84, 87, 89, 88, 88,
1748+ 64, 60, 57, 55, 58, 61, 66, 70, 75, 79, 82, 87, 91, 93, 93, 93,
1749+ 67, 62, 59, 57, 60, 63, 67, 72, 77, 80, 84, 89, 93, 95, 96, 97,
1750+ 68, 64, 61, 59, 61, 64, 67, 71, 75, 79, 83, 88, 93, 96, 99, 100,
1751+ 70, 66, 63, 61, 60, 64, 67, 71, 75, 79, 84, 88, 93, 97, 100, 102,
1752+ // Size 32x32
1753+ 32, 31, 31, 30, 33, 33, 36, 38, 41, 47, 49, 48, 49, 49, 50, 50,
1754+ 52, 53, 54, 56, 57, 60, 61, 63, 64, 65, 67, 67, 68, 69, 70, 71,
1755+ 31, 31, 31, 31, 34, 34, 38, 40, 42, 46, 47, 47, 47, 47, 48, 48,
1756+ 50, 50, 52, 54, 54, 57, 58, 60, 61, 61, 63, 64, 65, 65, 66, 67,
1757+ 31, 31, 31, 31, 34, 35, 39, 40, 42, 46, 47, 46, 46, 46, 47, 47,
1758+ 49, 50, 51, 53, 53, 56, 57, 59, 60, 60, 62, 63, 64, 65, 66, 67,
1759+ 30, 31, 31, 32, 34, 35, 40, 41, 42, 45, 46, 45, 45, 45, 46, 46,
1760+ 47, 48, 49, 51, 52, 54, 55, 57, 58, 58, 60, 61, 62, 62, 63, 64,
1761+ 33, 34, 34, 34, 37, 38, 42, 43, 44, 46, 47, 46, 46, 45, 46, 46,
1762+ 47, 48, 49, 51, 51, 53, 55, 56, 57, 57, 59, 60, 61, 62, 63, 64,
1763+ 33, 34, 35, 35, 38, 39, 43, 44, 45, 47, 47, 46, 46, 45, 46, 46,
1764+ 47, 48, 49, 51, 51, 53, 54, 56, 57, 57, 59, 60, 60, 61, 62, 62,
1765+ 36, 38, 39, 40, 42, 43, 47, 47, 47, 47, 48, 46, 46, 45, 46, 46,
1766+ 47, 47, 48, 49, 50, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62,
1767+ 38, 40, 40, 41, 43, 44, 47, 47, 48, 48, 49, 48, 47, 47, 47, 47,
1768+ 48, 49, 49, 51, 51, 53, 54, 55, 56, 56, 58, 58, 58, 59, 60, 60,
1769+ 41, 42, 42, 42, 44, 45, 47, 48, 48, 50, 50, 49, 49, 49, 50, 50,
1770+ 50, 51, 52, 53, 53, 55, 56, 57, 58, 58, 60, 61, 61, 61, 60, 60,
1771+ 47, 46, 46, 45, 46, 47, 47, 48, 50, 52, 52, 52, 52, 52, 53, 53,
1772+ 53, 54, 55, 55, 56, 58, 58, 60, 60, 61, 62, 61, 61, 62, 63, 64,
1773+ 49, 47, 47, 46, 47, 47, 48, 49, 50, 52, 53, 53, 53, 53, 54, 54,
1774+ 54, 55, 55, 56, 56, 58, 59, 60, 61, 61, 63, 63, 64, 64, 64, 64,
1775+ 48, 47, 46, 45, 46, 46, 46, 48, 49, 52, 53, 54, 55, 55, 56, 56,
1776+ 57, 58, 58, 59, 60, 61, 62, 63, 64, 64, 66, 65, 65, 65, 66, 67,
1777+ 49, 47, 46, 45, 46, 46, 46, 47, 49, 52, 53, 55, 55, 57, 57, 58,
1778+ 59, 59, 60, 61, 61, 63, 64, 65, 66, 66, 67, 67, 67, 68, 67, 67,
1779+ 49, 47, 46, 45, 45, 45, 45, 47, 49, 52, 53, 55, 57, 58, 59, 60,
1780+ 61, 62, 62, 63, 63, 65, 66, 67, 68, 68, 69, 70, 69, 68, 69, 70,
1781+ 50, 48, 47, 46, 46, 46, 46, 47, 50, 53, 54, 56, 57, 59, 61, 61,
1782+ 63, 64, 64, 66, 66, 68, 69, 70, 70, 71, 72, 70, 71, 72, 71, 70,
1783+ 50, 48, 47, 46, 46, 46, 46, 47, 50, 53, 54, 56, 58, 60, 61, 61,
1784+ 63, 64, 65, 66, 67, 68, 69, 71, 71, 71, 73, 74, 73, 72, 73, 74,
1785+ 52, 50, 49, 47, 47, 47, 47, 48, 50, 53, 54, 57, 59, 61, 63, 63,
1786+ 66, 67, 68, 70, 70, 72, 73, 75, 75, 75, 77, 75, 75, 76, 75, 74,
1787+ 53, 50, 50, 48, 48, 48, 47, 49, 51, 54, 55, 58, 59, 62, 64, 64,
1788+ 67, 68, 69, 71, 71, 73, 74, 76, 77, 77, 78, 78, 78, 76, 77, 78,
1789+ 54, 52, 51, 49, 49, 49, 48, 49, 52, 55, 55, 58, 60, 62, 64, 65,
1790+ 68, 69, 71, 73, 73, 75, 76, 78, 79, 79, 80, 80, 79, 80, 79, 78,
1791+ 56, 54, 53, 51, 51, 51, 49, 51, 53, 55, 56, 59, 61, 63, 66, 66,
1792+ 70, 71, 73, 75, 76, 78, 79, 81, 82, 82, 83, 81, 83, 81, 81, 82,
1793+ 57, 54, 53, 52, 51, 51, 50, 51, 53, 56, 56, 60, 61, 63, 66, 67,
1794+ 70, 71, 73, 76, 76, 79, 80, 82, 82, 83, 84, 85, 83, 84, 84, 82,
1795+ 60, 57, 56, 54, 53, 53, 52, 53, 55, 58, 58, 61, 63, 65, 68, 68,
1796+ 72, 73, 75, 78, 79, 82, 83, 85, 86, 86, 88, 86, 87, 86, 85, 86,
1797+ 61, 58, 57, 55, 55, 54, 53, 54, 56, 58, 59, 62, 64, 66, 69, 69,
1798+ 73, 74, 76, 79, 80, 83, 84, 86, 87, 88, 89, 89, 88, 88, 88, 86,
1799+ 63, 60, 59, 57, 56, 56, 54, 55, 57, 60, 60, 63, 65, 67, 70, 71,
1800+ 75, 76, 78, 81, 82, 85, 86, 89, 90, 90, 92, 91, 91, 90, 89, 91,
1801+ 64, 61, 60, 58, 57, 57, 55, 56, 58, 60, 61, 64, 66, 68, 70, 71,
1802+ 75, 77, 79, 82, 82, 86, 87, 90, 91, 91, 93, 93, 93, 92, 93, 91,
1803+ 65, 61, 60, 58, 57, 57, 55, 56, 58, 61, 61, 64, 66, 68, 71, 71,
1804+ 75, 77, 79, 82, 83, 86, 88, 90, 91, 91, 93, 94, 95, 95, 93, 95,
1805+ 67, 63, 62, 60, 59, 59, 57, 58, 60, 62, 63, 66, 67, 69, 72, 73,
1806+ 77, 78, 80, 83, 84, 88, 89, 92, 93, 93, 95, 95, 96, 96, 97, 95,
1807+ 67, 64, 63, 61, 60, 60, 58, 58, 61, 61, 63, 65, 67, 70, 70, 74,
1808+ 75, 78, 80, 81, 85, 86, 89, 91, 93, 94, 95, 97, 97, 98, 98, 100,
1809+ 68, 65, 64, 62, 61, 60, 59, 58, 61, 61, 64, 65, 67, 69, 71, 73,
1810+ 75, 78, 79, 83, 83, 87, 88, 91, 93, 95, 96, 97, 99, 98, 100, 100,
1811+ 69, 65, 65, 62, 62, 61, 60, 59, 61, 62, 64, 65, 68, 68, 72, 72,
1812+ 76, 76, 80, 81, 84, 86, 88, 90, 92, 95, 96, 98, 98, 100, 100, 101,
1813+ 70, 66, 66, 63, 63, 62, 61, 60, 60, 63, 64, 66, 67, 69, 71, 73,
1814+ 75, 77, 79, 81, 84, 85, 88, 89, 93, 93, 97, 98, 100, 100, 102, 101,
1815+ 71, 67, 67, 64, 64, 62, 62, 60, 60, 64, 64, 67, 67, 70, 70, 74,
1816+ 74, 78, 78, 82, 82, 86, 86, 91, 91, 95, 95, 100, 100, 101, 101, 104,
1817+ // Size 4x8
1818+ 31, 36, 46, 45, 49, 54, 59, 63, 47, 47, 52, 53, 55, 58, 61, 65,
1819+ 53, 50, 55, 63, 71, 77, 81, 80, 63, 59, 61, 70, 77, 86, 94, 95,
1820+ // Size 8x4
1821+ 31, 47, 53, 63, 36, 47, 50, 59, 46, 52, 55, 61, 45, 53, 63, 70,
1822+ 49, 55, 71, 77, 54, 58, 77, 86, 59, 61, 81, 94, 63, 65, 80, 95,
1823+ // Size 8x16
1824+ 32, 31, 33, 37, 42, 49, 48, 50, 52, 54, 57, 61, 64, 66, 68, 70,
1825+ 33, 34, 37, 43, 44, 47, 46, 46, 47, 49, 51, 55, 57, 59, 61, 63,
1826+ 45, 45, 46, 47, 49, 52, 51, 52, 53, 54, 55, 58, 60, 61, 61, 61,
1827+ 49, 46, 45, 45, 49, 53, 57, 59, 61, 62, 64, 66, 68, 69, 68, 67,
1828+ 52, 49, 47, 47, 50, 54, 59, 63, 66, 68, 70, 73, 75, 77, 77, 75,
1829+ 57, 53, 51, 50, 53, 57, 61, 66, 71, 73, 76, 80, 83, 84, 86, 83,
1830+ 64, 60, 57, 55, 58, 61, 66, 71, 75, 79, 83, 87, 91, 93, 94, 92,
1831+ 68, 64, 61, 59, 60, 63, 67, 71, 74, 79, 83, 87, 91, 95, 97, 98,
1832+ // Size 16x8
1833+ 32, 33, 45, 49, 52, 57, 64, 68, 31, 34, 45, 46, 49, 53, 60, 64,
1834+ 33, 37, 46, 45, 47, 51, 57, 61, 37, 43, 47, 45, 47, 50, 55, 59,
1835+ 42, 44, 49, 49, 50, 53, 58, 60, 49, 47, 52, 53, 54, 57, 61, 63,
1836+ 48, 46, 51, 57, 59, 61, 66, 67, 50, 46, 52, 59, 63, 66, 71, 71,
1837+ 52, 47, 53, 61, 66, 71, 75, 74, 54, 49, 54, 62, 68, 73, 79, 79,
1838+ 57, 51, 55, 64, 70, 76, 83, 83, 61, 55, 58, 66, 73, 80, 87, 87,
1839+ 64, 57, 60, 68, 75, 83, 91, 91, 66, 59, 61, 69, 77, 84, 93, 95,
1840+ 68, 61, 61, 68, 77, 86, 94, 97, 70, 63, 61, 67, 75, 83, 92, 98,
1841+ // Size 16x32
1842+ 32, 31, 31, 30, 33, 33, 37, 39, 42, 47, 49, 48, 48, 49, 50, 50,
1843+ 52, 53, 54, 56, 57, 60, 61, 63, 64, 64, 66, 67, 68, 69, 70, 70,
1844+ 31, 31, 32, 32, 35, 36, 40, 41, 43, 46, 46, 46, 45, 45, 46, 46,
1845+ 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 60, 61, 62, 63, 63, 64,
1846+ 33, 34, 34, 35, 37, 38, 43, 43, 44, 46, 47, 46, 46, 45, 46, 46,
1847+ 47, 48, 49, 51, 51, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
1848+ 37, 38, 39, 40, 42, 43, 47, 47, 47, 48, 48, 47, 46, 46, 46, 46,
1849+ 47, 48, 49, 50, 50, 52, 53, 55, 56, 56, 57, 58, 59, 59, 60, 61,
1850+ 45, 45, 45, 44, 46, 46, 47, 48, 49, 51, 52, 51, 51, 51, 52, 52,
1851+ 53, 53, 54, 55, 55, 57, 58, 59, 60, 60, 61, 61, 61, 61, 61, 61,
1852+ 48, 47, 46, 46, 47, 47, 47, 48, 50, 52, 53, 53, 53, 53, 54, 54,
1853+ 54, 55, 55, 56, 56, 58, 59, 60, 61, 61, 63, 63, 64, 65, 66, 67,
1854+ 49, 47, 46, 45, 45, 46, 45, 47, 49, 53, 53, 56, 57, 58, 59, 59,
1855+ 61, 61, 62, 63, 64, 65, 66, 67, 68, 68, 69, 69, 68, 68, 67, 67,
1856+ 50, 48, 47, 46, 46, 46, 46, 47, 50, 53, 54, 56, 57, 59, 61, 61,
1857+ 63, 64, 65, 66, 66, 68, 69, 70, 71, 71, 72, 70, 71, 72, 73, 74,
1858+ 52, 50, 49, 48, 47, 47, 47, 48, 50, 53, 54, 57, 59, 61, 63, 64,
1859+ 66, 67, 68, 70, 70, 72, 73, 75, 75, 75, 77, 78, 77, 76, 75, 74,
1860+ 56, 53, 52, 51, 50, 50, 49, 50, 53, 55, 56, 59, 61, 63, 65, 66,
1861+ 70, 71, 72, 74, 75, 77, 79, 80, 81, 81, 82, 80, 79, 80, 81, 82,
1862+ 57, 54, 53, 52, 51, 51, 50, 51, 53, 56, 57, 60, 61, 64, 66, 67,
1863+ 71, 72, 73, 76, 76, 79, 80, 82, 83, 83, 84, 85, 86, 85, 83, 82,
1864+ 63, 60, 59, 57, 56, 56, 54, 55, 57, 60, 60, 64, 65, 67, 70, 71,
1865+ 75, 76, 78, 81, 82, 85, 86, 89, 90, 90, 92, 90, 88, 88, 89, 90,
1866+ 64, 61, 60, 58, 57, 57, 55, 56, 58, 61, 61, 64, 66, 68, 71, 71,
1867+ 75, 77, 79, 82, 83, 86, 87, 90, 91, 91, 93, 93, 94, 94, 92, 90,
1868+ 67, 63, 62, 60, 60, 59, 57, 57, 60, 61, 63, 65, 66, 70, 70, 73,
1869+ 75, 77, 80, 81, 85, 86, 89, 91, 93, 94, 94, 96, 96, 95, 97, 98,
1870+ 68, 64, 64, 61, 61, 60, 59, 58, 60, 61, 63, 64, 67, 67, 71, 71,
1871+ 74, 75, 79, 80, 83, 85, 87, 89, 91, 94, 95, 97, 97, 99, 98, 98,
1872+ 68, 65, 65, 62, 62, 60, 61, 59, 59, 62, 62, 65, 65, 68, 68, 72,
1873+ 72, 76, 76, 80, 80, 84, 84, 89, 89, 93, 93, 97, 98, 99, 99, 102,
1874+ // Size 32x16
1875+ 32, 31, 33, 37, 45, 48, 49, 50, 52, 56, 57, 63, 64, 67, 68, 68,
1876+ 31, 31, 34, 38, 45, 47, 47, 48, 50, 53, 54, 60, 61, 63, 64, 65,
1877+ 31, 32, 34, 39, 45, 46, 46, 47, 49, 52, 53, 59, 60, 62, 64, 65,
1878+ 30, 32, 35, 40, 44, 46, 45, 46, 48, 51, 52, 57, 58, 60, 61, 62,
1879+ 33, 35, 37, 42, 46, 47, 45, 46, 47, 50, 51, 56, 57, 60, 61, 62,
1880+ 33, 36, 38, 43, 46, 47, 46, 46, 47, 50, 51, 56, 57, 59, 60, 60,
1881+ 37, 40, 43, 47, 47, 47, 45, 46, 47, 49, 50, 54, 55, 57, 59, 61,
1882+ 39, 41, 43, 47, 48, 48, 47, 47, 48, 50, 51, 55, 56, 57, 58, 59,
1883+ 42, 43, 44, 47, 49, 50, 49, 50, 50, 53, 53, 57, 58, 60, 60, 59,
1884+ 47, 46, 46, 48, 51, 52, 53, 53, 53, 55, 56, 60, 61, 61, 61, 62,
1885+ 49, 46, 47, 48, 52, 53, 53, 54, 54, 56, 57, 60, 61, 63, 63, 62,
1886+ 48, 46, 46, 47, 51, 53, 56, 56, 57, 59, 60, 64, 64, 65, 64, 65,
1887+ 48, 45, 46, 46, 51, 53, 57, 57, 59, 61, 61, 65, 66, 66, 67, 65,
1888+ 49, 45, 45, 46, 51, 53, 58, 59, 61, 63, 64, 67, 68, 70, 67, 68,
1889+ 50, 46, 46, 46, 52, 54, 59, 61, 63, 65, 66, 70, 71, 70, 71, 68,
1890+ 50, 46, 46, 46, 52, 54, 59, 61, 64, 66, 67, 71, 71, 73, 71, 72,
1891+ 52, 48, 47, 47, 53, 54, 61, 63, 66, 70, 71, 75, 75, 75, 74, 72,
1892+ 53, 49, 48, 48, 53, 55, 61, 64, 67, 71, 72, 76, 77, 77, 75, 76,
1893+ 54, 50, 49, 49, 54, 55, 62, 65, 68, 72, 73, 78, 79, 80, 79, 76,
1894+ 56, 51, 51, 50, 55, 56, 63, 66, 70, 74, 76, 81, 82, 81, 80, 80,
1895+ 57, 52, 51, 50, 55, 56, 64, 66, 70, 75, 76, 82, 83, 85, 83, 80,
1896+ 60, 54, 54, 52, 57, 58, 65, 68, 72, 77, 79, 85, 86, 86, 85, 84,
1897+ 61, 56, 55, 53, 58, 59, 66, 69, 73, 79, 80, 86, 87, 89, 87, 84,
1898+ 63, 57, 56, 55, 59, 60, 67, 70, 75, 80, 82, 89, 90, 91, 89, 89,
1899+ 64, 58, 57, 56, 60, 61, 68, 71, 75, 81, 83, 90, 91, 93, 91, 89,
1900+ 64, 59, 58, 56, 60, 61, 68, 71, 75, 81, 83, 90, 91, 94, 94, 93,
1901+ 66, 60, 59, 57, 61, 63, 69, 72, 77, 82, 84, 92, 93, 94, 95, 93,
1902+ 67, 61, 60, 58, 61, 63, 69, 70, 78, 80, 85, 90, 93, 96, 97, 97,
1903+ 68, 62, 61, 59, 61, 64, 68, 71, 77, 79, 86, 88, 94, 96, 97, 98,
1904+ 69, 63, 62, 59, 61, 65, 68, 72, 76, 80, 85, 88, 94, 95, 99, 99,
1905+ 70, 63, 63, 60, 61, 66, 67, 73, 75, 81, 83, 89, 92, 97, 98, 99,
1906+ 70, 64, 64, 61, 61, 67, 67, 74, 74, 82, 82, 90, 90, 98, 98, 102,
1907+ // Size 4x16
1908+ 31, 32, 35, 40, 43, 46, 45, 46, 48, 50, 52, 56, 58, 60, 62, 63,
1909+ 48, 46, 47, 47, 50, 53, 53, 54, 54, 55, 56, 59, 61, 63, 64, 66,
1910+ 56, 52, 50, 49, 53, 56, 61, 65, 70, 72, 75, 79, 81, 82, 79, 81,
1911+ 67, 62, 60, 57, 60, 63, 66, 70, 75, 80, 85, 89, 93, 94, 96, 97,
1912+ // Size 16x4
1913+ 31, 48, 56, 67, 32, 46, 52, 62, 35, 47, 50, 60, 40, 47, 49, 57,
1914+ 43, 50, 53, 60, 46, 53, 56, 63, 45, 53, 61, 66, 46, 54, 65, 70,
1915+ 48, 54, 70, 75, 50, 55, 72, 80, 52, 56, 75, 85, 56, 59, 79, 89,
1916+ 58, 61, 81, 93, 60, 63, 82, 94, 62, 64, 79, 96, 63, 66, 81, 97,
1917+ // Size 8x32
1918+ 32, 31, 31, 30, 33, 33, 37, 39, 42, 47, 49, 48, 48, 49, 50, 50,
1919+ 52, 53, 54, 56, 57, 60, 61, 63, 64, 64, 66, 67, 68, 69, 70, 70,
1920+ 33, 34, 34, 35, 37, 38, 43, 43, 44, 46, 47, 46, 46, 45, 46, 46,
1921+ 47, 48, 49, 51, 51, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
1922+ 45, 45, 45, 44, 46, 46, 47, 48, 49, 51, 52, 51, 51, 51, 52, 52,
1923+ 53, 53, 54, 55, 55, 57, 58, 59, 60, 60, 61, 61, 61, 61, 61, 61,
1924+ 49, 47, 46, 45, 45, 46, 45, 47, 49, 53, 53, 56, 57, 58, 59, 59,
1925+ 61, 61, 62, 63, 64, 65, 66, 67, 68, 68, 69, 69, 68, 68, 67, 67,
1926+ 52, 50, 49, 48, 47, 47, 47, 48, 50, 53, 54, 57, 59, 61, 63, 64,
1927+ 66, 67, 68, 70, 70, 72, 73, 75, 75, 75, 77, 78, 77, 76, 75, 74,
1928+ 57, 54, 53, 52, 51, 51, 50, 51, 53, 56, 57, 60, 61, 64, 66, 67,
1929+ 71, 72, 73, 76, 76, 79, 80, 82, 83, 83, 84, 85, 86, 85, 83, 82,
1930+ 64, 61, 60, 58, 57, 57, 55, 56, 58, 61, 61, 64, 66, 68, 71, 71,
1931+ 75, 77, 79, 82, 83, 86, 87, 90, 91, 91, 93, 93, 94, 94, 92, 90,
1932+ 68, 64, 64, 61, 61, 60, 59, 58, 60, 61, 63, 64, 67, 67, 71, 71,
1933+ 74, 75, 79, 80, 83, 85, 87, 89, 91, 94, 95, 97, 97, 99, 98, 98,
1934+ // Size 32x8
1935+ 32, 33, 45, 49, 52, 57, 64, 68, 31, 34, 45, 47, 50, 54, 61, 64,
1936+ 31, 34, 45, 46, 49, 53, 60, 64, 30, 35, 44, 45, 48, 52, 58, 61,
1937+ 33, 37, 46, 45, 47, 51, 57, 61, 33, 38, 46, 46, 47, 51, 57, 60,
1938+ 37, 43, 47, 45, 47, 50, 55, 59, 39, 43, 48, 47, 48, 51, 56, 58,
1939+ 42, 44, 49, 49, 50, 53, 58, 60, 47, 46, 51, 53, 53, 56, 61, 61,
1940+ 49, 47, 52, 53, 54, 57, 61, 63, 48, 46, 51, 56, 57, 60, 64, 64,
1941+ 48, 46, 51, 57, 59, 61, 66, 67, 49, 45, 51, 58, 61, 64, 68, 67,
1942+ 50, 46, 52, 59, 63, 66, 71, 71, 50, 46, 52, 59, 64, 67, 71, 71,
1943+ 52, 47, 53, 61, 66, 71, 75, 74, 53, 48, 53, 61, 67, 72, 77, 75,
1944+ 54, 49, 54, 62, 68, 73, 79, 79, 56, 51, 55, 63, 70, 76, 82, 80,
1945+ 57, 51, 55, 64, 70, 76, 83, 83, 60, 54, 57, 65, 72, 79, 86, 85,
1946+ 61, 55, 58, 66, 73, 80, 87, 87, 63, 56, 59, 67, 75, 82, 90, 89,
1947+ 64, 57, 60, 68, 75, 83, 91, 91, 64, 58, 60, 68, 75, 83, 91, 94,
1948+ 66, 59, 61, 69, 77, 84, 93, 95, 67, 60, 61, 69, 78, 85, 93, 97,
1949+ 68, 61, 61, 68, 77, 86, 94, 97, 69, 62, 61, 68, 76, 85, 94, 99,
1950+ 70, 63, 61, 67, 75, 83, 92, 98, 70, 64, 61, 67, 74, 82, 90, 98,
1951+ ],
1952+ ],
1953+ // qm level 4
1954+ [
1955+ [// luma
1956+ // Size 4x4
1957+ 32, 34, 53, 75, 34, 49, 64, 81, 53, 64, 91, 112, 75, 81, 112, 140,
1958+ // Size 8x8
1959+ 32, 32, 34, 39, 50, 62, 76, 84, 32, 33, 35, 40, 48, 59, 71, 79,
1960+ 34, 35, 39, 46, 53, 63, 74, 81, 39, 40, 46, 56, 65, 75, 86, 92,
1961+ 50, 48, 53, 65, 78, 90, 101, 106, 62, 59, 63, 75, 90, 105, 118, 123,
1962+ 76, 71, 74, 86, 101, 118, 134, 142, 84, 79, 81, 92, 106, 123, 142, 153,
1963+ // Size 16x16
1964+ 32, 31, 31, 32, 33, 36, 39, 44, 48, 54, 59, 66, 74, 81, 86, 91,
1965+ 31, 32, 32, 32, 33, 35, 38, 42, 46, 51, 56, 63, 70, 77, 81, 86,
1966+ 31, 32, 32, 33, 34, 35, 38, 41, 45, 49, 54, 60, 67, 73, 77, 82,
1967+ 32, 32, 33, 34, 36, 37, 40, 42, 45, 49, 53, 59, 66, 71, 75, 80,
1968+ 33, 33, 34, 36, 38, 42, 44, 46, 50, 53, 57, 63, 69, 74, 78, 80,
1969+ 36, 35, 35, 37, 42, 48, 50, 54, 57, 60, 64, 69, 75, 80, 84, 85,
1970+ 39, 38, 38, 40, 44, 50, 54, 58, 61, 65, 69, 74, 80, 85, 89, 91,
1971+ 44, 42, 41, 42, 46, 54, 58, 63, 67, 71, 75, 80, 86, 91, 95, 97,
1972+ 48, 46, 45, 45, 50, 57, 61, 67, 71, 76, 80, 86, 93, 98, 101, 104,
1973+ 54, 51, 49, 49, 53, 60, 65, 71, 76, 82, 87, 93, 100, 105, 109, 112,
1974+ 59, 56, 54, 53, 57, 64, 69, 75, 80, 87, 92, 99, 106, 112, 116, 120,
1975+ 66, 63, 60, 59, 63, 69, 74, 80, 86, 93, 99, 107, 115, 121, 125, 129,
1976+ 74, 70, 67, 66, 69, 75, 80, 86, 93, 100, 106, 115, 123, 130, 135, 138,
1977+ 81, 77, 73, 71, 74, 80, 85, 91, 98, 105, 112, 121, 130, 137, 142, 148,
1978+ 86, 81, 77, 75, 78, 84, 89, 95, 101, 109, 116, 125, 135, 142, 147, 153,
1979+ 91, 86, 82, 80, 80, 85, 91, 97, 104, 112, 120, 129, 138, 148, 153, 159,
1980+ // Size 32x32
1981+ 32, 31, 31, 31, 31, 31, 32, 32, 33, 34, 36, 36, 39, 41, 44, 46,
1982+ 48, 52, 54, 58, 59, 65, 66, 71, 74, 80, 81, 83, 86, 89, 91, 93,
1983+ 31, 32, 32, 32, 32, 32, 32, 32, 33, 34, 35, 35, 38, 39, 42, 44,
1984+ 46, 50, 51, 56, 56, 62, 63, 68, 71, 76, 77, 78, 82, 84, 86, 88,
1985+ 31, 32, 32, 32, 32, 32, 32, 32, 33, 34, 35, 35, 38, 39, 42, 44,
1986+ 46, 49, 51, 55, 56, 61, 63, 67, 70, 75, 77, 78, 81, 84, 86, 88,
1987+ 31, 32, 32, 32, 32, 32, 32, 32, 33, 33, 34, 34, 37, 38, 41, 42,
1988+ 44, 48, 49, 53, 54, 59, 60, 65, 68, 72, 74, 75, 78, 80, 82, 84,
1989+ 31, 32, 32, 32, 32, 33, 33, 33, 34, 34, 35, 35, 38, 39, 41, 43,
1990+ 45, 48, 49, 53, 54, 59, 60, 65, 67, 72, 73, 74, 77, 80, 82, 84,
1991+ 31, 32, 32, 32, 33, 33, 33, 34, 35, 35, 36, 36, 39, 40, 42, 44,
1992+ 45, 48, 50, 53, 54, 59, 60, 64, 67, 71, 73, 74, 77, 79, 81, 83,
1993+ 32, 32, 32, 32, 33, 33, 34, 35, 36, 36, 37, 38, 40, 40, 42, 44,
1994+ 45, 48, 49, 53, 53, 58, 59, 63, 66, 70, 71, 72, 75, 78, 80, 83,
1995+ 32, 32, 32, 32, 33, 34, 35, 35, 36, 37, 38, 38, 40, 41, 42, 44,
1996+ 46, 48, 49, 53, 53, 58, 59, 63, 65, 69, 71, 72, 74, 77, 79, 80,
1997+ 33, 33, 33, 33, 34, 35, 36, 36, 38, 39, 42, 42, 44, 45, 46, 48,
1998+ 50, 52, 53, 57, 57, 62, 63, 67, 69, 73, 74, 75, 78, 79, 80, 81,
1999+ 34, 34, 34, 33, 34, 35, 36, 37, 39, 39, 42, 43, 45, 46, 47, 49,
2000+ 51, 53, 54, 58, 58, 63, 64, 68, 70, 74, 75, 76, 79, 81, 84, 86,
crates/ec-av1/src/quant.rsmodified+64 -10
@@ -216,18 +216,54 @@ pub fn dequant_coeff_wh(
216 dq2.clamp(-bound, bound - 1) as i32216 dq2.clamp(-bound, bound - 1) as i32
217 }217 }
218 218
219+use crate::hits::hit_do;
220+
221+thread_local! {
222+ /// lane-av1-qmatrix: transform units whose dequantisation actually
223+ /// scaled at least one NONZERO level through a quantisation matrix --
224+ /// the witness gate's non-vacuity proof (`qm_scaled_units() > 0` on the
225+ /// `--enable-qm=1` arm, `== 0` on the qm-off control). Same feature gate
226+ /// as every other `*_HITS` counter in this crate.
227+ static QM_SCALED_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
228+}
229+
230+/// Current value of [`QM_SCALED_UNITS`].
231+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
232+pub(crate) fn qm_scaled_units() -> usize {
233+ QM_SCALED_UNITS.with(|c| c.get())
234+}
235+
236+/// Zeroes [`QM_SCALED_UNITS`] (the witness gate's per-arm reset).
237+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
238+pub(crate) fn reset_qm_scaled_units() {
239+ QM_SCALED_UNITS.with(|c| c.set(0));
240+}
241+
219 /// Dequantize a whole raster-order coefficient grid of a `side` x `side`242 /// Dequantize a whole raster-order coefficient grid of a `side` x `side`
220 /// transform.243 /// transform.
221 pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i32> {244 pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i32> {
222- dequant_wh(levels, side, side, bit_depth, q_idx, 0, 0)245+ dequant_wh(levels, side, side, bit_depth, q_idx, 0, 0, None)
223 }246 }
224 247
225 /// [`dequant`] widened to `(w, h)` (lane-recttx), threading the same248 /// [`dequant`] widened to `(w, h)` (lane-recttx), threading the same
226 /// [`QuantDeltas`]-shaped `dc_delta`/`ac_delta` pair as [`dequant_coeff_wh`].249 /// [`QuantDeltas`]-shaped `dc_delta`/`ac_delta` pair as [`dequant_coeff_wh`].
250+/// `qm` is the block's inverse quantisation matrix (lane-av1-qmatrix);
251+/// `None` dequantises flat.
227 #[allow(clippy::too_many_arguments)]252 #[allow(clippy::too_many_arguments)]
228-pub fn dequant_wh(levels: &[i32], w: usize, h: usize, bit_depth: u8, q_idx: i32, dc_delta: i32, ac_delta: i32) -> Vec<i32> {253+pub fn dequant_wh(
254+ levels: &[i32],
255+ w: usize,
256+ h: usize,
257+ bit_depth: u8,
258+ q_idx: i32,
259+ dc_delta: i32,
260+ ac_delta: i32,
261+ qm: Option<crate::qm::Iqm>,
262+) -> Vec<i32> {
229 let mut out = Vec::new();263 let mut out = Vec::new();
230- dequant_wh_into(levels, &mut out, w, h, bit_depth, q_idx, dc_delta, ac_delta);264+ dequant_wh_into(
265+ levels, &mut out, w, h, bit_depth, q_idx, dc_delta, ac_delta, qm,
266+ );
231 out267 out
232 }268 }
233 269
@@ -238,6 +274,10 @@ pub fn dequant_wh(levels: &[i32], w: usize, h: usize, bit_depth: u8, q_idx: i32,
238 /// self time). A zero level dequantizes to zero for every parameter set, so274 /// self time). A zero level dequantizes to zero for every parameter set, so
239 /// the common all-but-EOB tail costs a compare instead of a 64-bit multiply275 /// the common all-but-EOB tail costs a compare instead of a 64-bit multiply
240 /// and divide. The arithmetic on a non-zero level is the spec's, unchanged.276 /// and divide. The arithmetic on a non-zero level is the spec's, unchanged.
277+/// `qm` (lane-av1-qmatrix, spec 7.12.3's per-position `dqv` scale, libaom
278+/// `get_dqv`) rescales the DC/AC quantizer per coefficient BEFORE the
279+/// multiply; `None` is the flat dequantization every caller before
280+/// lane-av1-qmatrix ran.
241 #[allow(clippy::too_many_arguments)]281 #[allow(clippy::too_many_arguments)]
242 pub fn dequant_wh_into(282 pub fn dequant_wh_into(
243 levels: &[i32],283 levels: &[i32],
@@ -248,22 +288,37 @@ pub fn dequant_wh_into(
248 q_idx: i32,288 q_idx: i32,
249 dc_delta: i32,289 dc_delta: i32,
250 ac_delta: i32,290 ac_delta: i32,
291+ qm: Option<crate::qm::Iqm>,
251 ) {292 ) {
252 let dcq = i64::from(dc_q(bit_depth, q_idx + dc_delta));293 let dcq = i64::from(dc_q(bit_depth, q_idx + dc_delta));
253 let acq = i64::from(ac_q(bit_depth, q_idx + ac_delta));294 let acq = i64::from(ac_q(bit_depth, q_idx + ac_delta));
254 let denom = dq_denom_area(w * h);295 let denom = dq_denom_area(w * h);
255 let bound = 1i64 << (7 + u32::from(bit_depth));296 let bound = 1i64 << (7 + u32::from(bit_depth));
256- let apply = |l: i32, q: i64| -> i32 {297+ let apply = |pos: usize, l: i32, q: i64| -> i32 {
298+ let q = match &qm {
299+ None => q,
300+ Some(m) => crate::qm::dqv_at(m, pos, w, q),
301+ };
257 let dq = i64::from(l) * q;302 let dq = i64::from(l) * q;
258 let sign = if dq < 0 { -1 } else { 1 };303 let sign = if dq < 0 { -1 } else { 1 };
259 let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / denom);304 let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / denom);
260 dq2.clamp(-bound, bound - 1) as i32305 dq2.clamp(-bound, bound - 1) as i32
261 };306 };
307+ hit_do! {
308+ if qm.is_some() && levels.iter().any(|&l| l != 0) {
309+ QM_SCALED_UNITS.with(|c| c.set(c.get() + 1));
310+ }
311+ }
262 out.clear();312 out.clear();
263 out.reserve(levels.len());313 out.reserve(levels.len());
264- out.extend(levels.iter().map(|&l| if l == 0 { 0 } else { apply(l, acq) }));314+ out.extend(
315+ levels
316+ .iter()
317+ .enumerate()
318+ .map(|(pos, &l)| if l == 0 { 0 } else { apply(pos, l, acq) }),
319+ );
265 if let (Some(dst), Some(&l)) = (out.first_mut(), levels.first()) {320 if let (Some(dst), Some(&l)) = (out.first_mut(), levels.first()) {
266- *dst = if l == 0 { 0 } else { apply(l, dcq) };321+ *dst = if l == 0 { 0 } else { apply(0, l, dcq) };
267 }322 }
268 }323 }
269 324
@@ -299,11 +354,10 @@ mod tests {
299 for bit_depth in [8u8, 10, 12] {354 for bit_depth in [8u8, 10, 12] {
300 for (q_idx, dc, ac) in [(20, 0, 0), (140, -8, 5), (255, 12, -12)] {355 for (q_idx, dc, ac) in [(20, 0, 0), (140, -8, 5), (255, 12, -12)] {
301 let n = (w * h).min(levels.len());356 let n = (w * h).min(levels.len());
302- dequant_wh_into(&levels[..n], &mut out, w, h, bit_depth, q_idx, dc, ac);357+ dequant_wh_into(&levels[..n], &mut out, w, h, bit_depth, q_idx, dc, ac, None);
303 for (i, &got) in out.iter().enumerate() {358 for (i, &got) in out.iter().enumerate() {
304- let want = dequant_coeff_wh(359+ let want =
305- levels[i], i == 0, bit_depth, q_idx, dc, ac, w, h,360+ dequant_coeff_wh(levels[i], i == 0, bit_depth, q_idx, dc, ac, w, h);
306- );
307 assert_eq!(got, want, "{w}x{h} depth {bit_depth} q {q_idx} coeff {i}");361 assert_eq!(got, want, "{w}x{h} depth {bit_depth} q {q_idx} coeff {i}");
308 }362 }
309 }363 }
crates/ec-av1/src/refusal_inventory.rsmodified+126 -46
@@ -79,14 +79,13 @@ const REFUSALS: &[&str] = &[
79 // at the sequence header. Gate:79 // at the sequence header. Gate:
80 // `a_non_420_subsampled_sequence_header_is_refused_by_name`.80 // `a_non_420_subsampled_sequence_header_is_refused_by_name`.
81 "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels",81 "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels",
82- // lane-av1txr-r2: the third member of the same silent-garbage family.82+ // lane-av1txr-r2 added, lane-av1-qmatrix RETIRED: the third member of the
83- // `using_qmatrix`/`qm_y`/`qm_u`/`qm_v` are parsed by `ec-av1-syntax` but83+ // silent-garbage family (`using_qmatrix`/`qm_y`/`qm_u`/`qm_v`, refused by
84- // read by NOTHING on the decode path -- dequantisation is `base_q_idx` +84+ // name at the frame header) now DEQUANTISES through libaom's
85- // the plane DC/AC deltas only -- so a real `aomenc --enable-qm=1` stream85+ // `iwt_matrix_ref`/`get_dqv` port (`crate::qm`): the parsed 4-bit levels
86- // decoded with no refusal and wrong pixels (measured: byte-diff vs ffmpeg86+ // select per-plane matrices, 1D/identity transforms and lossless blocks
87- // while the qm-off control is byte-exact). Refused at the frame header.87+ // stay flat. Witness:
88- // Gate: `a_frame_using_quantisation_matrices_is_refused_by_name`.88+ // `a_real_aomenc_quantisation_matrix_stream_decodes_pixel_exact`.
89- "a frame using quantisation matrices (using_qmatrix=1): dequantisation here is base_q_idx plus the plane DC/AC deltas only, so qm_y/qm_u/qm_v would be ignored and the frame would decode silently wrong pixels",
90 // lane-lossless/lane-lossless2: a lossless frame (base_q_idx 0) decodes89 // lane-lossless/lane-lossless2: a lossless frame (base_q_idx 0) decodes
91 // sample-exact, KEY and INTER -- TX_4X4 with the Walsh-Hadamard transform90 // sample-exact, KEY and INTER -- TX_4X4 with the Walsh-Hadamard transform
92 // on every plane, no `tx_type` symbol, `is_cfl_allowed` narrowed to a91 // on every plane, no `tx_type` symbol, `is_cfl_allowed` narrowed to a
@@ -359,14 +358,12 @@ const PROVEN: &[(&str, &str)] = &[
359 "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels",358 "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels",
360 "a_non_420_subsampled_sequence_header_is_refused_by_name",359 "a_non_420_subsampled_sequence_header_is_refused_by_name",
361 ),360 ),
362- // lane-av1txr-r2: the gate encodes the same source twice with real aomenc361+ // lane-av1txr-r2 paired, lane-av1-qmatrix RETIRED with its gate: the
363- // (`--enable-qm=1` and `--enable-qm=0`), asserts the qm-off CONTROL decodes362+ // refusal's witness is now
364- // and the qm-on stream is refused by name -- so the refusal is exercised363+ // `a_real_aomenc_quantisation_matrix_stream_decodes_pixel_exact`, which
365- // against the exact encoder path it names, not merely present.364+ // asserts the qm-on stream DECODES pixel-exact vs ffmpeg and the
366- (365+ // matrices were really applied (census counters), while the qm-off
367- "a frame using quantisation matrices (using_qmatrix=1): dequantisation here is base_q_idx plus the plane DC/AC deltas only, so qm_y/qm_u/qm_v would be ignored and the frame would decode silently wrong pixels",366+ // control stays exact and flat.
368- "a_frame_using_quantisation_matrices_is_refused_by_name",
369- ),
370 // lane-av1-rect14: the 16x16- and 32x32-level strips are reconstructed367 // lane-av1-rect14: the 16x16- and 32x32-level strips are reconstructed
371 // (`decode_intrabc_rect` / `decode_intrabc_pair_strip`). The string remains368 // (`decode_intrabc_rect` / `decode_intrabc_pair_strip`). The string remains
372 // only in `decode_block_128rect`. No sb128 screen recipe in this lane369 // only in `decode_block_128rect`. No sb128 screen recipe in this lane
@@ -589,7 +586,11 @@ mod tests {
589 .chain(REFUSALS)586 .chain(REFUSALS)
590 .map(|&s| s.to_owned())587 .map(|&s| s.to_owned())
591 .collect();588 .collect();
592- assert!(found.len() >= 30, "the refusal scan found only {} reasons -- it is broken, not the decoder", found.len());589+ assert!(
590+ found.len() >= 30,
591+ "the refusal scan found only {} reasons -- it is broken, not the decoder",
592+ found.len()
593+ );
593 594
594 let added: Vec<&String> = found.difference(&listed).collect();595 let added: Vec<&String> = found.difference(&listed).collect();
595 assert!(596 assert!(
@@ -635,9 +636,14 @@ mod tests {
635 let mut found: BTreeSet<String> = BTreeSet::new();636 let mut found: BTreeSet<String> = BTreeSet::new();
636 for (i, _) in src.match_indices("\"SKIP ") {637 for (i, _) in src.match_indices("\"SKIP ") {
637 let rest = &src[i + "\"SKIP ".len()..];638 let rest = &src[i + "\"SKIP ".len()..];
638- let Some(colon) = rest.find(':') else { continue };639+ let Some(colon) = rest.find(':') else {
640+ continue;
641+ };
639 let name = &rest[..colon];642 let name = &rest[..colon];
640- if !name.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') {643+ if !name
644+ .chars()
645+ .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
646+ {
641 continue;647 continue;
642 }648 }
643 // The tail of the message says what was skipped. Only "{e}" -- the649 // The tail of the message says what was skipped. Only "{e}" -- the
@@ -650,7 +656,10 @@ mod tests {
650 }656 }
651 }657 }
652 let listed: BTreeSet<&str> = GATES_THAT_SKIP_ON_A_DECODE_ERROR.iter().copied().collect();658 let listed: BTreeSet<&str> = GATES_THAT_SKIP_ON_A_DECODE_ERROR.iter().copied().collect();
653- let added: Vec<&String> = found.iter().filter(|n| !listed.contains(n.as_str())).collect();659+ let added: Vec<&String> = found
660+ .iter()
661+ .filter(|n| !listed.contains(n.as_str()))
662+ .collect();
654 assert!(663 assert!(
655 added.is_empty(),664 added.is_empty(),
656 "these gates turn a decode error into a printed SKIP and are not declared: \665 "these gates turn a decode error into a printed SKIP and are not declared: \
@@ -723,9 +732,18 @@ mod tests {
723 fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() {732 fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() {
724 // (the refusal in the fallback arm, the alphabet its CDF codes)733 // (the refusal in the fallback arm, the alphabet its CDF codes)
725 let blocks: [(&str, usize); 4] = [734 let blocks: [(&str, usize); 4] = [
726- ("a 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1),735+ (
727- ("an INTER 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1),736+ "a 32x32 partition type this decoder does not code",
728- ("a 128x128 superblock partition value outside the 8-symbol alphabet", crate::cdf::PARTITION_W128[0].len() - 1),737+ crate::cdf::PARTITION_W32[0].len() - 1,
738+ ),
739+ (
740+ "an INTER 32x32 partition type this decoder does not code",
741+ crate::cdf::PARTITION_W32[0].len() - 1,
742+ ),
743+ (
744+ "a 128x128 superblock partition value outside the 8-symbol alphabet",
745+ crate::cdf::PARTITION_W128[0].len() - 1,
746+ ),
729 // lane-t900 r24: the key-frame superblock root. Its `part` is747 // lane-t900 r24: the key-frame superblock root. Its `part` is
730 // either a `partition_w64` symbol or, at a frame edge, one of the748 // either a `partition_w64` symbol or, at a frame edge, one of the
731 // three gathered outcomes (PARTITION_HORZ, PARTITION_VERT,749 // three gathered outcomes (PARTITION_HORZ, PARTITION_VERT,
@@ -779,7 +797,10 @@ mod tests {
779 "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \797 "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \
780 is a real gap in its {alphabet}-value alphabet, not a dead refusal",798 is a real gap in its {alphabet}-value alphabet, not a dead refusal",
781 i + 1,799 i + 1,
782- missing.iter().map(|v| PARTITION_NAMES[*v]).collect::<Vec<_>>()800+ missing
801+ .iter()
802+ .map(|v| PARTITION_NAMES[*v])
803+ .collect::<Vec<_>>()
783 );804 );
784 }805 }
785 assert_eq!(806 assert_eq!(
@@ -833,7 +854,11 @@ mod tests {
833 .next()854 .next()
834 .unwrap_or("")855 .unwrap_or("")
835 .to_owned();856 .to_owned();
836- assert!(!var.is_empty(), "the chain at decode.rs:{} tests nothing", head + 1);857+ assert!(
858+ !var.is_empty(),
859+ "the chain at decode.rs:{} tests nothing",
860+ head + 1
861+ );
837 862
838 // Walk up collecting every branch head of this chain, each read to863 // Walk up collecting every branch head of this chain, each read to
839 // the line that opens its body.864 // the line that opens its body.
@@ -843,7 +868,8 @@ mod tests {
843 loop {868 loop {
844 i -= 1;869 i -= 1;
845 let t = lines[i].trim_start();870 let t = lines[i].trim_start();
846- if indent(lines[i]) != outer || !(t.starts_with("} else if ") || t.starts_with("if "))871+ if indent(lines[i]) != outer
872+ || !(t.starts_with("} else if ") || t.starts_with("if "))
847 {873 {
848 if i == 0 {874 if i == 0 {
849 break;875 break;
@@ -893,7 +919,10 @@ mod tests {
893 "the chain at decode.rs:{} refuses {reason:?} but no branch names {:?} -- that \919 "the chain at decode.rs:{} refuses {reason:?} but no branch names {:?} -- that \
894 is a real gap in its {alphabet}-value alphabet, not a dead refusal",920 is a real gap in its {alphabet}-value alphabet, not a dead refusal",
895 head + 1,921 head + 1,
896- missing.iter().map(|v| PARTITION_NAMES[*v]).collect::<Vec<_>>()922+ missing
923+ .iter()
924+ .map(|v| PARTITION_NAMES[*v])
925+ .collect::<Vec<_>>()
897 );926 );
898 }927 }
899 assert_eq!(found, chains.len());928 assert_eq!(found, chains.len());
@@ -996,11 +1025,18 @@ mod tests {
996 values.insert(((1u64 << length) - 2) as u32);1025 values.insert(((1u64 << length) - 2) as u32);
997 }1026 }
998 let max = *values.iter().max().unwrap();1027 let max = *values.iter().max().unwrap();
999- assert_eq!(max, (1u32 << 20) - 2, "the enumeration misses the spec's own ceiling");1028+ assert_eq!(
1029+ max,
1030+ (1u32 << 20) - 2,
1031+ "the enumeration misses the spec's own ceiling"
1032+ );
1000 // A coefficient is masked to 20 bits (spec 5.11.39), and the base and1033 // A coefficient is masked to 20 bits (spec 5.11.39), and the base and
1001 // base-range syntax contribute at most 15 of that before the tail, so1034 // base-range syntax contribute at most 15 of that before the tail, so
1002 // this is the largest tail a legal coefficient can need.1035 // this is the largest tail a legal coefficient can need.
1003- assert!(max >= 0xF_FFFF - 15, "the ceiling is below the largest legal tail");1036+ assert!(
1037+ max >= 0xF_FFFF - 15,
1038+ "the ceiling is below the largest legal tail"
1039+ );
1004 for value in values {1040 for value in values {
1005 match roundtrip(value) {1041 match roundtrip(value) {
1006 Ok(read) => assert_eq!(read, value, "the Golomb tail {value} read back as {read}"),1042 Ok(read) => assert_eq!(read, value, "the Golomb tail {value} read back as {read}"),
@@ -1056,7 +1092,11 @@ mod tests {
1056 ];1092 ];
1057 assert_eq!(alphabets[0].1, 13, "the y_mode alphabet changed size");1093 assert_eq!(alphabets[0].1, 13, "the y_mode alphabet changed size");
1058 for group in &crate::cdf::Y_MODE {1094 for group in &crate::cdf::Y_MODE {
1059- assert_eq!(group.len() - 1, alphabets[0].1, "the y_mode rows disagree on width");1095+ assert_eq!(
1096+ group.len() - 1,
1097+ alphabets[0].1,
1098+ "the y_mode rows disagree on width"
1099+ );
1060 }1100 }
1061 1101
1062 let src = include_str!("decode.rs");1102 let src = include_str!("decode.rs");
@@ -1064,12 +1104,17 @@ mod tests {
1064 let mut sites = 0usize;1104 let mut sites = 0usize;
1065 for (i, line) in lines.iter().enumerate() {1105 for (i, line) in lines.iter().enumerate() {
1066 let t = line.trim();1106 let t = line.trim();
1067- let Some(rest) = t.strip_prefix("if mode >= ") else { continue };1107+ let Some(rest) = t.strip_prefix("if mode >= ") else {
1108+ continue;
1109+ };
1068 let Some(threshold) = rest.trim_end_matches(" {").parse::<usize>().ok() else {1110 let Some(threshold) = rest.trim_end_matches(" {").parse::<usize>().ok() else {
1069 continue;1111 continue;
1070 };1112 };
1071 // Only the guards carrying THIS refusal.1113 // Only the guards carrying THIS refusal.
1072- if !lines[i..(i + 4).min(lines.len())].join("\n").contains(REFUSAL) {1114+ if !lines[i..(i + 4).min(lines.len())]
1115+ .join("\n")
1116+ .contains(REFUSAL)
1117+ {
1073 continue;1118 continue;
1074 }1119 }
1075 sites += 1;1120 sites += 1;
@@ -1099,7 +1144,10 @@ mod tests {
1099 i + 11144 i + 1
1100 );1145 );
1101 }1146 }
1102- assert_eq!(sites, 3, "the intra-mode guard is at three sites, found {sites}");1147+ assert_eq!(
1148+ sites, 3,
1149+ "the intra-mode guard is at three sites, found {sites}"
1150+ );
1103 }1151 }
1104 1152
1105 /// lane-t900 r31, ENUMERATION for "a HORZ/VERT intra strip in a1153 /// lane-t900 r31, ENUMERATION for "a HORZ/VERT intra strip in a
@@ -1159,7 +1207,10 @@ mod tests {
1159 }1207 }
1160 }1208 }
1161 }1209 }
1162- assert!(with_128 > 0, "enumeration is vacuous -- no 128-sided footprint was tried");1210+ assert!(
1211+ with_128 > 0,
1212+ "enumeration is vacuous -- no 128-sided footprint was tried"
1213+ );
1163 assert_eq!(1214 assert_eq!(
1164 allowed_at_128, 0,1215 allowed_at_128, 0,
1165 "a footprint with a 128-pixel side passed av1_allow_palette's bound -- the \1216 "a footprint with a 128-pixel side passed av1_allow_palette's bound -- the \
@@ -1271,11 +1322,17 @@ mod tests {
1271 // (3) The two tables, read out of decode_rect_split's own text: every1322 // (3) The two tables, read out of decode_rect_split's own text: every
1272 // `(w, h) =>` / `(w, h) |` match arm head between the table's `let`1323 // `(w, h) =>` / `(w, h) |` match arm head between the table's `let`
1273 // and the guard that follows it.1324 // and the guard that follows it.
1274- let body_at = src.find("fn decode_rect_split(").expect("decode_rect_split is gone");1325+ let body_at = src
1326+ .find("fn decode_rect_split(")
1327+ .expect("decode_rect_split is gone");
1275 let body = &src[body_at..];1328 let body = &src[body_at..];
1276 let slice = |from: &str, to: &str| -> &str {1329 let slice = |from: &str, to: &str| -> &str {
1277- let a = body.find(from).unwrap_or_else(|| panic!("{from:?} is gone from decode_rect_split"));1330+ let a = body
1278- let b = body[a..].find(to).unwrap_or_else(|| panic!("{to:?} is gone from decode_rect_split"));1331+ .find(from)
1332+ .unwrap_or_else(|| panic!("{from:?} is gone from decode_rect_split"));
1333+ let b = body[a..]
1334+ .find(to)
1335+ .unwrap_or_else(|| panic!("{to:?} is gone from decode_rect_split"));
1279 &body[a..a + b]1336 &body[a..a + b]
1280 };1337 };
1281 let arm_shapes = |text: &str| -> BTreeSet<(usize, usize)> {1338 let arm_shapes = |text: &str| -> BTreeSet<(usize, usize)> {
@@ -1348,7 +1405,10 @@ mod tests {
1348 }1405 }
1349 }1406 }
1350 }1407 }
1351- assert_eq!(checked, 40, "the strip domain is not the ten rect shapes x four depths");1408+ assert_eq!(
1409+ checked, 40,
1410+ "the strip domain is not the ten rect shapes x four depths"
1411+ );
1352 }1412 }
1353 1413
1354 /// lane-t900 r33, ENUMERATION for "CfL, filter intra or a palette on a1414 /// lane-t900 r33, ENUMERATION for "CfL, filter intra or a palette on a
@@ -1458,9 +1518,15 @@ mod tests {
1458 }1518 }
1459 continue;1519 continue;
1460 }1520 }
1461- let Some(rest) = line.strip_prefix('(') else { continue };1521+ let Some(rest) = line.strip_prefix('(') else {
1462- let Some((pair, _)) = rest.split_once(") =>") else { continue };1522+ continue;
1463- let Some((a, b)) = pair.split_once(',') else { continue };1523+ };
1524+ let Some((pair, _)) = rest.split_once(") =>") else {
1525+ continue;
1526+ };
1527+ let Some((a, b)) = pair.split_once(',') else {
1528+ continue;
1529+ };
1464 let bw: usize = a.trim().parse().expect("arm bw");1530 let bw: usize = a.trim().parse().expect("arm bw");
1465 let bh: usize = b.trim().parse().expect("arm bh");1531 let bh: usize = b.trim().parse().expect("arm bh");
1466 assert!(1532 assert!(
@@ -1472,9 +1538,18 @@ mod tests {
1472 seen.push((bw, bh));1538 seen.push((bw, bh));
1473 arms += 1;1539 arms += 1;
1474 }1540 }
1475- assert!(arms >= 10, "only {arms} arms parsed -- the table's shape changed");1541+ assert!(
1476- assert!(saw_none_fallback, "filter_intra_size_class_rect lost its `_ => None` fallback");1542+ arms >= 10,
1477- assert!(saw_square_delegate, "filter_intra_size_class_rect lost its square-delegate arm");1543+ "only {arms} arms parsed -- the table's shape changed"
1544+ );
1545+ assert!(
1546+ saw_none_fallback,
1547+ "filter_intra_size_class_rect lost its `_ => None` fallback"
1548+ );
1549+ assert!(
1550+ saw_square_delegate,
1551+ "filter_intra_size_class_rect lost its square-delegate arm"
1552+ );
1478 // The guard arm delegates square strips to `filter_intra_size_class`;1553 // The guard arm delegates square strips to `filter_intra_size_class`;
1479 // drive the delegate directly, so a widening there turns THIS test red1554 // drive the delegate directly, so a widening there turns THIS test red
1480 // (the numeric walk never reaches the guard arm).1555 // (the numeric walk never reaches the guard arm).
@@ -1524,7 +1599,10 @@ mod tests {
1524 );1599 );
1525 }1600 }
1526 }1601 }
1527- assert_eq!(sites, 8, "the call-site audit found {sites} sites, not the 8 it enumerated");1602+ assert_eq!(
1603+ sites, 8,
1604+ "the call-site audit found {sites} sites, not the 8 it enumerated"
1605+ );
1528 }1606 }
1529 1607
1530 /// Every entry of [`PROVEN`] must still name a live refusal and a test1608 /// Every entry of [`PROVEN`] must still name a live refusal and a test
@@ -1543,7 +1621,9 @@ mod tests {
1543 "{reason:?} is listed as proven but is no longer a decode-path refusal -- drop it from PROVEN (the capability landed) or fix the string"1621 "{reason:?} is listed as proven but is no longer a decode-path refusal -- drop it from PROVEN (the capability landed) or fix the string"
1544 );1622 );
1545 assert!(1623 assert!(
1546- sources.iter().any(|src| src.contains(&format!("fn {gate}("))),1624+ sources
1625+ .iter()
1626+ .any(|src| src.contains(&format!("fn {gate}("))),
1547 "{reason:?} names the proving test {gate}, which exists in neither stream.rs \1627 "{reason:?} names the proving test {gate}, which exists in neither stream.rs \
1548 nor decode.rs"1628 nor decode.rs"
1549 );1629 );
crates/ec-av1/src/stream.rsmodified+179 -55
@@ -1668,24 +1668,31 @@ fn decode_frame(
1668 "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels",1668 "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels",
1669 ));1669 ));
1670 }1670 }
1671- // lane-av1txr-r2: `quantization_params.using_qmatrix` (spec 5.9.12) is1671+ // lane-av1-qmatrix: `quantization_params.using_qmatrix` + `qm_y`/`qm_u`/
1672- // parsed by `ec-av1-syntax` along with `qm_y`/`qm_u`/`qm_v` (frame.rs1672+ // `qm_v` (spec 5.9.12) select per-plane INVERSE QUANTISATION MATRICES --
1673- // :190-196, :1418-1426), but this decoder reads NONE of them: every1673+ // spec 7.12.3's per-position `dqv` scale, libaom `get_dqv` over
1674- // dequantisation below is `base_q_idx` plus the plane DC/AC deltas only1674+ // `iwt_matrix_ref`. The parsed 4-bit values ARE the table levels
1675- // (`Quantizer::new`), so a stream that applies quantisation matrices1675+ // (libaom `quant_params->qmatrix_level_y/u/v`); 15 is libaom's flat
1676- // dequantises every coefficient off the wrong table. The same1676+ // NULL-table sentinel, and a `using_qmatrix == 0` frame keeps all three
1677- // silent-garbage class as the non-4:2:0 guard above. Measured: a real1677+ // parked there, selecting no matrix. Frame-constant: recorded here (the
1678- // `aomenc --enable-qm=1` key frame (`using_qmatrix = 1`, `qm_y/u/v = 5`)1678+ // spot where this decoder refused such frames until this lane), read by
1679- // decodes with NO refusal and its planes byte-differ from ffmpeg, while1679+ // every dequantisation call site through [`crate::decode::block_iqmatrix`].
1680- // the `--enable-qm=0` control of the same source and recipe is1680+ // A real `aomenc --enable-qm=1` stream (`using_qmatrix = 1`, levels 5)
1681- // byte-exact. Refuse by name rather than return garbage. Gate:1681+ // decodes byte-exact vs ffmpeg; the witness is
1682- // `a_frame_using_quantisation_matrices_is_refused_by_name` below.1682+ // `a_real_aomenc_quantisation_matrix_stream_decodes_pixel_exact` below.
1683- if header.quantization.using_qmatrix {1683+ let q = &header.quantization;
1684- return Err(Error::unsupported(1684+ // When the bit is UNSET the parsed 4-bit fields read 0 but mean nothing
1685- "AV1 decode_stream",1685+ // (spec: they are not even coded): libaom parks the level at
1686- "a frame using quantisation matrices (using_qmatrix=1): dequantisation here is base_q_idx plus the plane DC/AC deltas only, so qm_y/qm_u/qm_v would be ignored and the frame would decode silently wrong pixels",1686+ // `NUM_QM_LEVELS - 1`, its flat NULL-table sentinel
1687- ));1687+ // (`setup_segmentation_dequant`: `use_qmatrix ? qmatrix_level_y : 15`),
1688- }1688+ // so a matrix-free frame selects no matrix.
1689+ let flat = crate::qm::NUM_QM_LEVELS as u8 - 1;
1690+ let levels = if q.using_qmatrix {
1691+ [q.qm_y, q.qm_u, q.qm_v]
1692+ } else {
1693+ [flat; 3]
1694+ };
1695+ crate::decode::set_qm_levels(levels, fctx);
1689 // lane-dpm1: a LOSSLESS frame (`qindex == 0` with zero deltas, spec 5.9.12)1696 // lane-dpm1: a LOSSLESS frame (`qindex == 0` with zero deltas, spec 5.9.12)
1690 // is a different tile syntax, and this decoder has none of it: libaom1697 // is a different tile syntax, and this decoder has none of it: libaom
1691 // forces TX_4X4 with the WALSH-HADAMARD transform over every block (so no1698 // forces TX_4X4 with the WALSH-HADAMARD transform over every block (so no
@@ -2185,28 +2192,22 @@ pub(crate) mod tests {
2185 assert_eq!(frames.len(), 1, "{NAME}: the 4:2:0 control decoded no frame");2192 assert_eq!(frames.len(), 1, "{NAME}: the 4:2:0 control decoded no frame");
2186 }2193 }
2187 2194
2188- /// lane-av1txr-r2: a frame using quantisation matrices is refused by name2195+ /// lane-av1-qmatrix: WITNESS for the lifted `using_qmatrix` refusal (the
2189- /// -- the THIRD member of the silent-garbage family the non-4:2:0 gate2196+ /// old gate `a_frame_using_quantisation_matrices_is_refused_by_name` is
2190- /// above opened (the verifier's census miss).2197+ /// its mirror image -- its own panic said "flip this gate to a witness"
2191- ///2198+ /// the moment the dequant landed).
2192- /// `using_qmatrix`/`qm_y`/`qm_u`/`qm_v` are parsed by `ec-av1-syntax`
2193- /// (frame.rs:190-196, :1418-1426) but have NO consumer anywhere on the
2194- /// decode path -- every dequantisation is `base_q_idx` plus the plane
2195- /// DC/AC deltas only. Measured on a real aomenc pair (same source and
2196- /// recipe, only `--enable-qm` flipped): the qm-OFF control decodes
2197- /// byte-exact vs ffmpeg, the qm-ON stream used to decode with NO refusal
2198- /// and byte-differ from ffmpeg (silent garbage; class
2199- /// `refusal-hides-a-defect` read the other way -- a silent wrong decode
2200- /// is worse than a named gap).
2201 ///2199 ///
2202- /// Both arms are real encoder output, so this is a reachability gate, not2200+ /// A REAL `aomenc --enable-qm=1` stream (same source and recipe as the
2203- /// a hand-built construction: the qm-on arm carries `using_qmatrix = 1`2201+ /// refusal's gate, plus its `--enable-qm=0` control) now DECODES, and
2204- /// (`qm_y/u/v = 5` on the measured fixture) and the refusal names the2202+ /// every frame of both arms is pixel-exact against ffmpeg's own decode
2205- /// field rather than desyncing.2203+ /// of the same OBU. The census proves the feature non-vacuously both
2206- #[test]2204+ /// ways (class `gate-blind-to-feature`): the parsed frame headers really
2207- fn a_frame_using_quantisation_matrices_is_refused_by_name() {2205+ /// carry `using_qmatrix = 1`, the dequantiser really SCALED transform
2208- const NAME: &str = "a_frame_using_quantisation_matrices_is_refused_by_name";2206+ /// units through a matrix on the qm-on arm (`qm_scaled_units() > 0`),
2209- const REFUSAL: &str = "a frame using quantisation matrices";2207+ /// and the qm-off control really scaled none (`== 0`).
2208+ #[test]
2209+ fn a_real_aomenc_quantisation_matrix_stream_decodes_pixel_exact() {
2210+ const NAME: &str = "a_real_aomenc_quantisation_matrix_stream_decodes_pixel_exact";
2210 if !have_ffmpeg() || !have_aomenc() {2211 if !have_ffmpeg() || !have_aomenc() {
2211 eprintln!(2212 eprintln!(
2212 "SKIP {NAME}: needs ffmpeg + an aomenc oracle at {}",2213 "SKIP {NAME}: needs ffmpeg + an aomenc oracle at {}",
@@ -2214,26 +2215,149 @@ pub(crate) mod tests {
2214 );2215 );
2215 return;2216 return;
2216 }2217 }
2218+ const W: usize = 128;
2219+ const H: usize = 96;
2220+ const FRAMES: usize = 3;
2221+ // Every frame header's (using_qmatrix, qm_y, qm_u, qm_v), parsed
2222+ // straight off the encoded bytes.
2223+ let qm_headers = |stream: &[u8]| -> Vec<(bool, u8, u8, u8)> {
2224+ let mut parser = Av1Parser::new();
2225+ let mut pos = 0usize;
2226+ let mut out = Vec::new();
2227+ while pos < stream.len() {
2228+ let obu = parser.parse_obu(&stream[pos..]).unwrap();
2229+ pos += obu.total_size;
2230+ if let ObuKind::Frame(header, _) = obu.kind {
2231+ let q = &header.quantization;
2232+ out.push((q.using_qmatrix, q.qm_y, q.qm_u, q.qm_v));
2233+ }
2234+ }
2235+ out
2236+ };
2237+ // `census_attempt` hands back only the stream LENGTH; this witness
2238+ // needs the BYTES to hand ffmpeg, so the same recipe (its base args
2239+ // verbatim, `testsrc2` 128x96 @ 25fps, 3 frames) runs inline.
2240+ let encode = |extra: &[&str]| -> Vec<u8> {
2241+ let y4m = Command::new("ffmpeg")
2242+ .args([
2243+ "-v",
2244+ "error",
2245+ "-f",
2246+ "lavfi",
2247+ "-i",
2248+ &format!("testsrc2=size={W}x{H}:rate=25"),
2249+ "-t",
2250+ "0.12",
2251+ "-pix_fmt",
2252+ "yuv420p",
2253+ "-strict",
2254+ "-1",
2255+ "-f",
2256+ "yuv4mpegpipe",
2257+ "-",
2258+ ])
2259+ .stdin(Stdio::null())
2260+ .stdout(Stdio::piped())
2261+ .stderr(Stdio::piped())
2262+ .output()
2263+ .expect("ffmpeg failed to run");
2264+ assert!(y4m.status.success(), "ffmpeg refused the {W}x{H} fixture");
2265+ let mut args: Vec<String> = [
2266+ "--codec=av1",
2267+ "--passes=1",
2268+ "--end-usage=q",
2269+ "--threads=1",
2270+ "--row-mt=0",
2271+ "--kf-max-dist=9999",
2272+ "--lag-in-frames=0",
2273+ ]
2274+ .iter()
2275+ .map(|s| (*s).to_owned())
2276+ .collect();
2277+ args.push(format!("--limit={FRAMES}"));
2278+ args.extend(extra.iter().map(|s| (*s).to_owned()));
2279+ args.extend(["--obu".to_owned(), "-o".to_owned(), "-".to_owned(), "-".to_owned()]);
2280+ let out = run_with_stdin(Command::new(aomenc_path()).args(&args), &y4m.stdout);
2281+ assert!(
2282+ out.status.success(),
2283+ "aomenc refused {args:?}: {}",
2284+ String::from_utf8_lossy(&out.stderr)
2285+ );
2286+ out.stdout
2287+ };
2288+ // Pixel-exact every decoded frame of `stream` against ffmpeg, plane
2289+ // by plane, first differing sample named.
2290+ let compare = |stream: &[u8], frames: usize, label: &str| {
2291+ let want = ffmpeg_decode_sequence(stream, W, H, frames);
2292+ let got =
2293+ decode_stream(stream).unwrap_or_else(|e| panic!("{NAME}: {label} decode: {e}"));
2294+ assert_eq!(got.len(), want.len(), "{NAME}: {label} frame count");
2295+ for (i, (g, w)) in got.iter().zip(want.iter()).enumerate() {
2296+ for (plane, (gp, wp)) in
2297+ [(&g.y, &w.y), (&g.u, &w.u), (&g.v, &w.v)].into_iter().enumerate()
2298+ {
2299+ assert_eq!(gp, wp,
2300+ "{NAME}: {label} frame {i} plane {plane} differs from ffmpeg (first mismatching sample: got {}, want {})",
2301+ gp.iter().zip(wp.iter()).find(|(a, b)| a != b).map_or(0, |(a, _)| *a),
2302+ gp.iter().zip(wp.iter()).find(|(a, b)| a != b).map_or(0, |(_, b)| *b));
2303+ }
2304+ }
2305+ };
2306+
2217 // CONTROL: the same source and recipe with quantisation matrices OFF2307 // CONTROL: the same source and recipe with quantisation matrices OFF
2218- // still decodes -- so the refusal below is about the qm bit, not about2308+ // -- unchanged by this lane, and it must not scale a single unit
2219- // the source or the encoder settings.2309+ // through a matrix.
2220- let (_, control) = census_attempt("testsrc2", 128, 96, 3, &["--enable-qm=0"]);2310+ crate::quant::reset_qm_scaled_units();
2221- let frames = control.unwrap_or_else(|e| {2311+ let control_stream = encode(&["--enable-qm=0"]);
2222- panic!("{NAME}: the --enable-qm=0 control must decode, got: {e}")2312+ let control_frames = decode_stream(&control_stream)
2223- });2313+ .unwrap_or_else(|e| panic!("{NAME}: the --enable-qm=0 control must decode, got: {e}"))
2224- assert!(frames >= 1, "{NAME}: the qm-off control decoded no frame");2314+ .len();
2225- // The qm-on stream is the new refusal.2315+ assert!(
2226- let (_, qm) = census_attempt("testsrc2", 128, 96, 3, &["--enable-qm=1"]);2316+ control_frames >= 1,
2227- let err = qm.err().unwrap_or_else(|| {2317+ "{NAME}: the qm-off control decoded no frame"
2228- panic!("{NAME}: the --enable-qm=1 stream now decodes -- flip this gate to a witness")2318+ );
2229- });2319+ for (using, y, u, v) in qm_headers(&control_stream) {
2320+ assert!(!using, "{NAME}: the control's frame header set using_qmatrix");
2321+ assert_eq!(
2322+ (y, u, v),
2323+ (0, 0, 0),
2324+ "{NAME}: the control's frame header carried qm levels {y}/{u}/{v}"
2325+ );
2326+ }
2327+ compare(&control_stream, control_frames, "qm-off control");
2328+ assert_eq!(
2329+ crate::quant::qm_scaled_units(),
2330+ 0,
2331+ "{NAME}: the qm-off control scaled units through a matrix"
2332+ );
2333+
2334+ // The qm-on stream: decodes now, pixel-exact, levels 5 per plane as
2335+ // aomenc writes them.
2336+ crate::quant::reset_qm_scaled_units();
2337+ let qm_stream = encode(&["--enable-qm=1"]);
2338+ let qm_frames = decode_stream(&qm_stream)
2339+ .unwrap_or_else(|e| panic!("{NAME}: the --enable-qm=1 stream must decode, got: {e}"))
2340+ .len();
2341+ assert!(qm_frames >= 1, "{NAME}: the qm-on stream decoded no frame");
2342+ let headers = qm_headers(&qm_stream);
2343+ assert!(
2344+ !headers.is_empty() && headers.iter().all(|h| h.0),
2345+ "{NAME}: not every qm-on frame header set using_qmatrix: {headers:?}"
2346+ );
2347+ assert!(
2348+ headers.iter().all(|h| (h.1, h.2, h.3) == (5, 5, 5)),
2349+ "{NAME}: expected aomenc's qm levels 5/5/5, got {headers:?}"
2350+ );
2351+ compare(&qm_stream, qm_frames, "qm-on");
2352+ let scaled = crate::quant::qm_scaled_units();
2230 assert!(2353 assert!(
2231- err.contains(REFUSAL),2354+ scaled > 0,
2232- "{NAME}: the qm-on stream stopped without the named refusal {REFUSAL:?} \2355+ "{NAME}: the --enable-qm=1 stream dequantized every unit flat -- \
2233- -- re-measure before trusting this gate. got: {err}"2356+ the matrices were never applied (class gate-blind-to-feature)"
2234 );2357 );
2235 eprintln!(2358 eprintln!(
2236- "{NAME}: qm-off control {frames} frames, qm-on refused by name ({REFUSAL})"2359+ "{NAME}: qm-off control {control_frames} frames exact (0 matrix-scaled units), \
2360+ qm-on {qm_frames} frames exact ({scaled} matrix-scaled units, levels 5/5/5)"
2237 );2361 );
2238 }2362 }
2239 2363
crates/ec-av1/src/transform.rsmodified+155 -45
@@ -146,7 +146,10 @@ impl Lane for i32 {
146 }146 }
147 147
148 unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) {148 unsafe fn had(a: Self, b: Self, r: usize) -> (Self, Self) {
149- (clamp_range(a.wrapping_add(b), r), clamp_range(a.wrapping_sub(b), r))149+ (
150+ clamp_range(a.wrapping_add(b), r),
151+ clamp_range(a.wrapping_sub(b), r),
152+ )
150 }153 }
151 }154 }
152 155
@@ -183,7 +186,7 @@ impl Lane for [i32; 8] {
183 #[cfg(target_arch = "x86_64")]186 #[cfg(target_arch = "x86_64")]
184 #[allow(unsafe_code)]187 #[allow(unsafe_code)]
185 mod simd {188 mod simd {
186- use super::{COS_BITS, Lane, inverse_dct_n};189+ use super::{inverse_dct_n, Lane, COS_BITS};
187 use std::arch::x86_64::*;190 use std::arch::x86_64::*;
188 191
189 /// Eight `i32` lanes in one AVX2 register.192 /// Eight `i32` lanes in one AVX2 register.
@@ -205,7 +208,10 @@ mod simd {
205 let sv = _mm256_set1_epi32(s as i32);208 let sv = _mm256_set1_epi32(s as i32);
206 let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1));209 let rnd = _mm256_set1_epi64x(1 << (COS_BITS - 1));
207 let (ae, be) = (a.0, b.0);210 let (ae, be) = (a.0, b.0);
208- let (ao, bo) = (_mm256_shuffle_epi32(a.0, 0xB1), _mm256_shuffle_epi32(b.0, 0xB1));211+ let (ao, bo) = (
212+ _mm256_shuffle_epi32(a.0, 0xB1),
213+ _mm256_shuffle_epi32(b.0, 0xB1),
214+ );
209 let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| {215 let half = |x: __m256i, y: __m256i, u: __m256i, v: __m256i| {
210 let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v));216 let p = _mm256_sub_epi64(_mm256_mul_epi32(x, u), _mm256_mul_epi32(y, v));
211 let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u));217 let q = _mm256_add_epi64(_mm256_mul_epi32(x, v), _mm256_mul_epi32(y, u));
@@ -666,6 +672,16 @@ pub enum TxType {
666 }672 }
667 673
668 impl TxType {674 impl TxType {
675+ /// `is_2d_transform` (`av1/common/quant_common.c:237`): both axes are a
676+ /// real transform, i.e. libaom's `tx_type < IDTX`. The quantisation
677+ /// matrices weight a 2D frequency tiling, so a 1D/identity transform
678+ /// (`V_DCT`, `H_DCT`, `IDTX`, the `V_ADST` family) reads the FLAT
679+ /// matrix -- [`crate::qm::iwt_matrix`]'s `None`.
680+ pub(crate) fn is_2d_transform(self) -> bool {
681+ use TxType1d::Identity;
682+ !matches!(self.axes(), (Identity, _) | (_, Identity))
683+ }
684+
669 /// The inverse of `Tx_Type_Intra_Inv_Set2`'s CDF symbol order.685 /// The inverse of `Tx_Type_Intra_Inv_Set2`'s CDF symbol order.
670 pub fn from_symbol(t: usize) -> Option<Self> {686 pub fn from_symbol(t: usize) -> Option<Self> {
671 match t {687 match t {
@@ -1171,7 +1187,11 @@ pub fn inverse_transform_2d_typed_wh(
1171 // row: every butterfly and Hadamard of zeros is zero, and1187 // row: every butterfly and Hadamard of zeros is zero, and
1172 // `Round2(0, n)` is zero at every `n`, so this is the full1188 // `Round2(0, n)` is zero at every `n`, so this is the full
1173 // network's own answer, not an approximation of it.1189 // network's own answer, not an approximation of it.
1174- let src = if i < 32 { &dequant[i * w..i * w + cols] } else { &[][..] };1190+ let src = if i < 32 {
1191+ &dequant[i * w..i * w + cols]
1192+ } else {
1193+ &[][..]
1194+ };
1175 if src.iter().all(|&c| c == 0) {1195 if src.iter().all(|&c| c == 0) {
1176 dst.fill(0);1196 dst.fill(0);
1177 continue;1197 continue;
@@ -1306,18 +1326,26 @@ pub fn dequant_and_inverse_typed(
1306 ac_delta: i32,1326 ac_delta: i32,
1307 tx_type: TxType,1327 tx_type: TxType,
1308 ) -> Vec<i32> {1328 ) -> Vec<i32> {
1309- let r =1329+ let r = dequant_and_inverse_typed_wh(
1310- dequant_and_inverse_typed_wh(levels, side, side, bit_depth, q_idx, dc_delta, ac_delta, tx_type);1330+ levels, side, side, bit_depth, q_idx, dc_delta, ac_delta, tx_type, None,
1331+ );
1311 // This square entry point keeps the dense contract (its callers are the1332 // This square entry point keeps the dense contract (its callers are the
1312 // encoder-side model and the gates, which index the result directly);1333 // encoder-side model and the gates, which index the result directly);
1313 // only the decoder's `_wh` path speaks the empty marker.1334 // only the decoder's `_wh` path speaks the empty marker.
1314- if r.is_empty() { vec![0i32; side * side] } else { r }1335+ if r.is_empty() {
1336+ vec![0i32; side * side]
1337+ } else {
1338+ r
1339+ }
1315 }1340 }
1316 1341
1317 /// [`dequant_and_inverse_typed`] widened to `(w, h)` (lane-recttx). `dc_delta`/1342 /// [`dequant_and_inverse_typed`] widened to `(w, h)` (lane-recttx). `dc_delta`/
1318 /// `ac_delta` are the per-plane quantizer-index offsets (lane-sbpart r11,1343 /// `ac_delta` are the per-plane quantizer-index offsets (lane-sbpart r11,
1319 /// spec 5.9.12/7.12.2, [`crate::quant::QuantDeltas`]) -- `0`/`0` for the1344 /// spec 5.9.12/7.12.2, [`crate::quant::QuantDeltas`]) -- `0`/`0` for the
1320-/// unaffected callers below.1345+/// unaffected callers below. `qm` is the block's inverse quantisation matrix
1346+/// (lane-av1-qmatrix, spec 5.9.12/7.12.3, [`crate::qm::Iqm`]); `None`
1347+/// dequantises flat, and every caller that models a matrix-free stream (the
1348+/// encoder, the transform gates) passes `None`.
1321 #[allow(clippy::too_many_arguments)]1349 #[allow(clippy::too_many_arguments)]
1322 pub fn dequant_and_inverse_typed_wh(1350 pub fn dequant_and_inverse_typed_wh(
1323 levels: &[i32],1351 levels: &[i32],
@@ -1328,6 +1356,7 @@ pub fn dequant_and_inverse_typed_wh(
1328 dc_delta: i32,1356 dc_delta: i32,
1329 ac_delta: i32,1357 ac_delta: i32,
1330 tx_type: TxType,1358 tx_type: TxType,
1359+ qm: Option<crate::qm::Iqm>,
1331 ) -> Vec<i32> {1360 ) -> Vec<i32> {
1332 // 60% of the transform units of a 4K inter stream carry no coefficient at1361 // 60% of the transform units of a 4K inter stream carry no coefficient at
1333 // all (measured on the perf segment: 3.46M of 5.7M calls). Dequantization1362 // all (measured on the perf segment: 3.46M of 5.7M calls). Dequantization
@@ -1345,7 +1374,9 @@ pub fn dequant_and_inverse_typed_wh(
1345 }1374 }
1346 DQ_SCRATCH.with(|cell| {1375 DQ_SCRATCH.with(|cell| {
1347 let mut dq = cell.borrow_mut();1376 let mut dq = cell.borrow_mut();
1348- crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta);1377+ crate::quant::dequant_wh_into(
1378+ levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta, qm,
1379+ );
1349 // lane-d792: the DEQUANTIZED grid, in row-major `(row/col)` pairs --1380 // lane-d792: the DEQUANTIZED grid, in row-major `(row/col)` pairs --
1350 // the twin of the oracle's `EC_DQCOEFF` rung (whose own layout is1381 // the twin of the oracle's `EC_DQCOEFF` rung (whose own layout is
1351 // column-major `input[c * h + r]`), so a residual mismatch whose1382 // column-major `input[c * h + r]`), so a residual mismatch whose
@@ -1436,7 +1467,10 @@ fn build_axis_basis(n: usize, kind: TxType1d) -> Vec<f64> {
1436 // enough that nothing clamps at the 24-bit range passed below.1467 // enough that nothing clamps at the 24-bit range passed below.
1437 t[u] = 1 << 12;1468 t[u] = 1 << 12;
1438 inverse_1d(&mut t, log2, 24, kind);1469 inverse_1d(&mut t, log2, 24, kind);
1439- let norm = (0..n).map(|i| f64::from(t[i]) * f64::from(t[i])).sum::<f64>().sqrt();1470+ let norm = (0..n)
1471+ .map(|i| f64::from(t[i]) * f64::from(t[i]))
1472+ .sum::<f64>()
1473+ .sqrt();
1440 for i in 0..n {1474 for i in 0..n {
1441 basis[u * n + i] = f64::from(t[i]) / norm;1475 basis[u * n + i] = f64::from(t[i]) / norm;
1442 }1476 }
@@ -1448,7 +1482,8 @@ fn build_axis_basis(n: usize, kind: TxType1d) -> Vec<f64> {
1448 fn axis_basis(n: usize, kind: TxType1d) -> &'static [f64] {1482 fn axis_basis(n: usize, kind: TxType1d) -> &'static [f64] {
1449 use std::sync::OnceLock;1483 use std::sync::OnceLock;
1450 static CACHE: OnceLock<[[OnceLock<Vec<f64>>; 5]; 3]> = OnceLock::new();1484 static CACHE: OnceLock<[[OnceLock<Vec<f64>>; 5]; 3]> = OnceLock::new();
1451- let cache = CACHE.get_or_init(|| std::array::from_fn(|_| std::array::from_fn(|_| OnceLock::new())));1485+ let cache =
1486+ CACHE.get_or_init(|| std::array::from_fn(|_| std::array::from_fn(|_| OnceLock::new())));
1452 let k = match kind {1487 let k = match kind {
1453 TxType1d::Dct => 0,1488 TxType1d::Dct => 0,
1454 TxType1d::Adst => 1,1489 TxType1d::Adst => 1,
@@ -1467,7 +1502,8 @@ fn axis_basis(n: usize, kind: TxType1d) -> &'static [f64] {
1467 fn axis_basis_t(n: usize, kind: TxType1d) -> &'static [f64] {1502 fn axis_basis_t(n: usize, kind: TxType1d) -> &'static [f64] {
1468 use std::sync::OnceLock;1503 use std::sync::OnceLock;
1469 static CACHE: OnceLock<[[OnceLock<Vec<f64>>; 5]; 3]> = OnceLock::new();1504 static CACHE: OnceLock<[[OnceLock<Vec<f64>>; 5]; 3]> = OnceLock::new();
1470- let cache = CACHE.get_or_init(|| std::array::from_fn(|_| std::array::from_fn(|_| OnceLock::new())));1505+ let cache =
1506+ CACHE.get_or_init(|| std::array::from_fn(|_| std::array::from_fn(|_| OnceLock::new())));
1471 let k = match kind {1507 let k = match kind {
1472 TxType1d::Dct => 0,1508 TxType1d::Dct => 0,
1473 TxType1d::Adst => 1,1509 TxType1d::Adst => 1,
@@ -1517,7 +1553,11 @@ pub fn forward_transform_2d_typed(residual: &[i32], side: usize, tx_type: TxType
1517 } else {1553 } else {
1518 Vec::new()1554 Vec::new()
1519 };1555 };
1520- let residual: &[i32] = if mirrored.is_empty() { residual } else { &mirrored };1556+ let residual: &[i32] = if mirrored.is_empty() {
1557+ residual
1558+ } else {
1559+ &mirrored
1560+ };
1521 let (row_kind, col_kind) = tx_type.axes();1561 let (row_kind, col_kind) = tx_type.axes();
1522 let (hb, vb) = (axis_basis_t(side, row_kind), axis_basis_t(side, col_kind));1562 let (hb, vb) = (axis_basis_t(side, row_kind), axis_basis_t(side, col_kind));
1523 let mut rows = vec![0.0f64; side * side];1563 let mut rows = vec![0.0f64; side * side];
@@ -1754,7 +1794,18 @@ fn forward_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) {
1754 }1794 }
1755 }1795 }
1756 for i in 0..2 {1796 for i in 0..2 {
1757- butterfly(t, 2 * i, 2 * i + 1, if i == 0 { 32 + 16 * i as i32 } else { -(32 + 16 * i as i32) }, i == 0, r);1797+ butterfly(
1798+ t,
1799+ 2 * i,
1800+ 2 * i + 1,
1801+ if i == 0 {
1802+ 32 + 16 * i as i32
1803+ } else {
1804+ -(32 + 16 * i as i32)
1805+ },
1806+ i == 0,
1807+ r,
1808+ );
1758 }1809 }
1759 if N == 6 {1810 if N == 6 {
1760 for i in 0..8 {1811 for i in 0..8 {
@@ -1799,9 +1850,11 @@ fn forward_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) {
1799 butterfly(1850 butterfly(
1800 t,1851 t,
1801 8 + i,1852 8 + i,
1802- 15 - i, -(12 + ((brev(2, 3 - i) as i32) << 4)),1853+ 15 - i,
1854+ -(12 + ((brev(2, 3 - i) as i32) << 4)),
1803 false,1855 false,
1804- r);1856+ r,
1857+ );
1805 }1858 }
1806 }1859 }
1807 if N == 6 {1860 if N == 6 {
@@ -1814,9 +1867,11 @@ fn forward_dct_n<T: Lane, const N: u32>(t: &mut [T; 64], r: usize) {
1814 butterfly(1867 butterfly(
1815 t,1868 t,
1816 16 + i,1869 16 + i,
1817- 31 - i, -(6 + ((brev(3, 7 - i) as i32) << 3)),1870+ 31 - i,
1871+ -(6 + ((brev(3, 7 - i) as i32) << 3)),
1818 false,1872 false,
1819- r);1873+ r,
1874+ );
1820 }1875 }
1821 }1876 }
1822 if N == 6 {1877 if N == 6 {
@@ -2072,8 +2127,14 @@ pub fn forward_and_quantize_scaled(
2072 deadzone: f64,2127 deadzone: f64,
2073 ) -> (Vec<i32>, Vec<f32>) {2128 ) -> (Vec<i32>, Vec<f32>) {
2074 let mut scaled = vec![0f32; side * side];2129 let mut scaled = vec![0f32; side * side];
2075- let levels =2130+ let levels = forward_and_quantize_into(
2076- forward_and_quantize_into(residual, side, bit_depth, q_idx, deadzone, Some(&mut scaled));2131+ residual,
2132+ side,
2133+ bit_depth,
2134+ q_idx,
2135+ deadzone,
2136+ Some(&mut scaled),
2137+ );
2077 (levels, scaled)2138 (levels, scaled)
2078 }2139 }
2079 2140
@@ -2154,8 +2215,24 @@ mod tests {
2154 }2215 }
2155 }2216 }
2156 }2217 }
2157- for (w, h) in [(4, 4), (8, 8), (16, 16), (32, 32), (64, 64), (8, 16), (16, 8), (4, 16), (32, 8)] {2218+ for (w, h) in [
2158- for tx in [TxType::DctDct, TxType::AdstAdst, TxType::Idtx, TxType::AdstDct, TxType::DctAdst] {2219+ (4, 4),
2220+ (8, 8),
2221+ (16, 16),
2222+ (32, 32),
2223+ (64, 64),
2224+ (8, 16),
2225+ (16, 8),
2226+ (4, 16),
2227+ (32, 8),
2228+ ] {
2229+ for tx in [
2230+ TxType::DctDct,
2231+ TxType::AdstAdst,
2232+ TxType::Idtx,
2233+ TxType::AdstDct,
2234+ TxType::DctAdst,
2235+ ] {
2159 if w.max(h) > 16 && tx != TxType::DctDct && tx != TxType::Idtx {2236 if w.max(h) > 16 && tx != TxType::DctDct && tx != TxType::Idtx {
2160 continue;2237 continue;
2161 }2238 }
@@ -2163,7 +2240,10 @@ mod tests {
2163 continue;2240 continue;
2164 }2241 }
2165 let out = inverse_transform_2d_typed_wh(&vec![0i32; w * h], w, h, 10, tx);2242 let out = inverse_transform_2d_typed_wh(&vec![0i32; w * h], w, h, 10, tx);
2166- assert!(out.iter().all(|&v| v == 0), "2D {w}x{h} {tx:?} left a non-zero");2243+ assert!(
2244+ out.iter().all(|&v| v == 0),
2245+ "2D {w}x{h} {tx:?} left a non-zero"
2246+ );
2167 }2247 }
2168 }2248 }
2169 }2249 }
@@ -2172,8 +2252,10 @@ mod tests {
2172 #[ignore]2252 #[ignore]
2173 fn txrd_gain_probe() {2253 fn txrd_gain_probe() {
2174 for side in [4usize, 8, 16, 32] {2254 for side in [4usize, 8, 16, 32] {
2175- let residual: Vec<i32> =2255+ let residual: Vec<i32> = noise(side * side, 7 + side as u64)
2176- noise(side * side, 7 + side as u64).iter().map(|&v| v / 4).collect();2256+ .iter()
2257+ .map(|&v| v / 4)
2258+ .collect();
2177 let all = [2259 let all = [
2178 TxType::DctDct,2260 TxType::DctDct,
2179 TxType::AdstDct,2261 TxType::AdstDct,
@@ -2191,8 +2273,11 @@ mod tests {
2191 }2273 }
2192 let levels = forward_and_quantize_typed(&residual, side, 8, 20, 0.5, tx);2274 let levels = forward_and_quantize_typed(&residual, side, 8, 20, 0.5, tx);
2193 let back = dequant_and_inverse_typed(&levels, side, 8, 20, 0, 0, tx);2275 let back = dequant_and_inverse_typed(&levels, side, 8, 20, 0, 0, tx);
2194- let num: f64 =2276+ let num: f64 = residual
2195- residual.iter().zip(&back).map(|(&a, &b)| f64::from(a) * f64::from(b)).sum();2277+ .iter()
2278+ .zip(&back)
2279+ .map(|(&a, &b)| f64::from(a) * f64::from(b))
2280+ .sum();
2196 let den: f64 = residual.iter().map(|&a| f64::from(a) * f64::from(a)).sum();2281 let den: f64 = residual.iter().map(|&a| f64::from(a) * f64::from(a)).sum();
2197 let nz = levels.iter().filter(|&&l| l != 0).count();2282 let nz = levels.iter().filter(|&&l| l != 0).count();
2198 let energy: f64 = levels.iter().map(|&l| f64::from(l.abs())).sum();2283 let energy: f64 = levels.iter().map(|&l| f64::from(l.abs())).sum();
@@ -2632,9 +2717,7 @@ mod tests {
2632 inverse_adst16(&mut t, 16);2717 inverse_adst16(&mut t, 16);
2633 assert_eq!(2718 assert_eq!(
2634 t,2719 t,
2635- [2720+ [32, 73, 28, 6, -45, -28, 111, -52, 135, -54, 85, -67, 305, 190, 102, 145]
2636- 32, 73, 28, 6, -45, -28, 111, -52, 135, -54, 85, -67, 305, 190, 102, 145
2637- ]
2638 );2721 );
2639 }2722 }
2640 2723
@@ -2654,9 +2737,7 @@ mod tests {
2654 inverse_identity(&mut t16, 16);2737 inverse_identity(&mut t16, 16);
2655 assert_eq!(2738 assert_eq!(
2656 t16,2739 t16,
2657- [2740+ [283, -141, 71, 20, -8, 170, -34, 23, 42, -113, 93, -14, 62, 3, -198, 124]
2658- 283, -141, 71, 20, -8, 170, -34, 23, 42, -113, 93, -14, 62, 3, -198, 124
2659- ]
2660 );2741 );
2661 2742
2662 let mut t32 = [0i32; 32];2743 let mut t32 = [0i32; 32];
@@ -2988,7 +3069,6 @@ mod tests {
2988 }3069 }
2989 }3070 }
2990 }3071 }
2991-
2992 }3072 }
2993 3073
2994 /// The lossless inverse transform: libaom `av1_iwht4x4_16_add_c`3074 /// The lossless inverse transform: libaom `av1_iwht4x4_16_add_c`
@@ -3011,8 +3091,12 @@ pub fn inverse_wht4x4(dq: &[i32]) -> Vec<i32> {
3011 // the DC-only units correctly and drifts by +-1 everywhere else.3091 // the DC-only units correctly and drifts by +-1 everywhere else.
3012 let mut out = [0i32; 16];3092 let mut out = [0i32; 16];
3013 for i in 0..4 {3093 for i in 0..4 {
3014- let (mut a1, mut c1, mut d1, mut b1) =3094+ let (mut a1, mut c1, mut d1, mut b1) = (
3015- (dq[i * 4] >> 2, dq[i * 4 + 1] >> 2, dq[i * 4 + 2] >> 2, dq[i * 4 + 3] >> 2);3095+ dq[i * 4] >> 2,
3096+ dq[i * 4 + 1] >> 2,
3097+ dq[i * 4 + 2] >> 2,
3098+ dq[i * 4 + 3] >> 2,
3099+ );
3016 a1 += c1;3100 a1 += c1;
3017 d1 -= b1;3101 d1 -= b1;
3018 let e1 = (a1 - d1) >> 1;3102 let e1 = (a1 - d1) >> 1;
@@ -3027,8 +3111,7 @@ pub fn inverse_wht4x4(dq: &[i32]) -> Vec<i32> {
3027 }3111 }
3028 let mut res = vec![0i32; 16];3112 let mut res = vec![0i32; 16];
3029 for i in 0..4 {3113 for i in 0..4 {
3030- let (mut a1, mut c1, mut d1, mut b1) =3114+ let (mut a1, mut c1, mut d1, mut b1) = (out[i], out[4 + i], out[8 + i], out[12 + i]);
3031- (out[i], out[4 + i], out[8 + i], out[12 + i]);
3032 a1 += c1;3115 a1 += c1;
3033 d1 -= b1;3116 d1 -= b1;
3034 let e1 = (a1 - d1) >> 1;3117 let e1 = (a1 - d1) >> 1;
@@ -3059,7 +3142,12 @@ pub fn dequant_and_inverse_wht4x4(
3059 }3142 }
3060 DQ_SCRATCH.with(|cell| {3143 DQ_SCRATCH.with(|cell| {
3061 let mut dq = cell.borrow_mut();3144 let mut dq = cell.borrow_mut();
3062- crate::quant::dequant_wh_into(levels, &mut dq, 4, 4, bit_depth, q_idx, dc_delta, ac_delta);3145+ // Lossless dequantizes flat: a lossless frame (index 0, zero deltas)
3146+ // can never select a quantisation matrix (libaom `av1_use_qmatrix`),
3147+ // so there is no `qm` input to thread here.
3148+ crate::quant::dequant_wh_into(
3149+ levels, &mut dq, 4, 4, bit_depth, q_idx, dc_delta, ac_delta, None,
3150+ );
3063 inverse_wht4x4(&dq[..16])3151 inverse_wht4x4(&dq[..16])
3064 })3152 })
3065 }3153 }
@@ -3088,8 +3176,12 @@ mod lossless_tx_tests {
3088 fn fwht(residual: &[i32]) -> Vec<i32> {3176 fn fwht(residual: &[i32]) -> Vec<i32> {
3089 let mut t = [0i32; 16];3177 let mut t = [0i32; 16];
3090 for i in 0..4 {3178 for i in 0..4 {
3091- let (a, b, c, d) =3179+ let (a, b, c, d) = undo(
3092- undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]);3180+ residual[i],
3181+ residual[4 + i],
3182+ residual[8 + i],
3183+ residual[12 + i],
3184+ );
3093 t[i] = a;3185 t[i] = a;
3094 t[4 + i] = c;3186 t[4 + i] = c;
3095 t[8 + i] = d;3187 t[8 + i] = d;
@@ -3117,7 +3209,11 @@ mod lossless_tx_tests {
3117 // The forward pass already carries libaom's `UNIT_QUANT_SHIFT`3209 // The forward pass already carries libaom's `UNIT_QUANT_SHIFT`
3118 // scale-up, so the coded level is `levels / 4` and qindex 0's3210 // scale-up, so the coded level is `levels / 4` and qindex 0's
3119 // dequantization (times 4) hands `levels` straight back.3211 // dequantization (times 4) hands `levels` straight back.
3120- assert_eq!(super::inverse_wht4x4(&levels), residual, "levels={levels:?}");3212+ assert_eq!(
3213+ super::inverse_wht4x4(&levels),
3214+ residual,
3215+ "levels={levels:?}"
3216+ );
3121 }3217 }
3122 }3218 }
3123 }3219 }
@@ -3140,9 +3236,23 @@ mod d792_tests {
3140 const W: usize = 16;3236 const W: usize = 16;
3141 // (libaom index, dequantized value).3237 // (libaom index, dequantized value).
3142 const NZ: [(usize, i32); 17] = [3238 const NZ: [(usize, i32); 17] = [
3143- (0, 1554), (1, -1768), (2, 624), (3, -104), (9, 104), (10, -520), (11, 312),3239+ (0, 1554),
3144- (16, -104), (17, 104), (18, -104), (24, -104), (25, 104), (27, -104), (32, 208),3240+ (1, -1768),
3145- (33, -104), (35, -104), (40, 104),3241+ (2, 624),
3242+ (3, -104),
3243+ (9, 104),
3244+ (10, -520),
3245+ (11, 312),
3246+ (16, -104),
3247+ (17, 104),
3248+ (18, -104),
3249+ (24, -104),
3250+ (25, 104),
3251+ (27, -104),
3252+ (32, 208),
3253+ (33, -104),
3254+ (35, -104),
3255+ (40, 104),
3146 ];3256 ];
3147 let mut grid = vec![0i32; W * H];3257 let mut grid = vec![0i32; W * H];
3148 for (i, v) in NZ {3258 for (i, v) in NZ {
lanes/av1qmatrix.report.mdadded+119 -0
@@ -0,0 +1,119 @@
1+# lane-av1-qmatrix: `using_qmatrix` dequantisation lift
2+
3+Base 73f3ac64, worktree `~/Documents/Code/Rust/edith_codecs-av1qm`, branch
4+`lane-av1-qmatrix`. Owner: the using_qmatrix dequant path only.
5+
6+## What landed
7+
8+`using_qmatrix` + `qm_y`/`qm_u`/`qm_v` (spec 5.9.12) now select per-plane
9+inverse quantisation matrices, ported bug-for-bug from libaom
10+(`av1/common/quant_common.c`, `av1/decoder/decodetxb.c`). The frame-header
11+refusal from lane-av1txr-r2 is retired; `stream.rs` records the frame's
12+levels instead.
13+
14+New module `crates/ec-av1/src/qm.rs`:
15+
16+- `IWT`: libaom `iwt_matrix_ref[NUM_QM_LEVELS-1][2][QM_TOTAL_SIZE]` verbatim
17+ (100 320 u8 = 15 levels x [luma, chroma] x 3344), extracted mechanically
18+ from the oracle source with the `/* Size ... */` comment markers cross-checking
19+ the chunk order. Level 15 absent (libaom stores NULL = flat for it).
20+- `iwt_matrix(level, chroma, w, h)` = `av1_get_iqmatrix` narrowed to the
21+ decode path: level >= 15 -> `None`; 64-axis clamps
22+ (`av1_get_adjusted_tx_size`) -> the smaller shape's matrix
23+ (64x64/64x32/32x64 -> 32x32, 64x16 -> 32x16, 16x64 -> 16x32).
24+- `dqv_at` = `get_dqv` (`decodetxb.c:54-60`): `dqv = ((m * dqv) + 16) >> 5`
25+ (`AOM_QM_BITS = 5`), applied to the DC and the AC quantizer alike.
26+
27+Wiring (mirror of libaom's `setup_segmentation_dequant` + `av1_use_qmatrix`):
28+
29+- Levels are per FRAME, read straight off the parsed 4-bit fields -- the
30+ parsed value IS the table level (libaom `qmatrix_level_y/u/v`,
31+ decodeframe.c:1902-1917); the `aom_get_qmlevel(base_q_idx, ...)` formula is
32+ encoder-side only. When `using_qmatrix == 0` the parsed fields read 0 but
33+ are not coded: levels are parked at 15 (flat), exactly libaom's sentinel.
34+ Recorded per frame at the old refusal site (`set_qm_levels`).
35+- Per block (`block_iqmatrix`, decode.rs): `None` (flat) when the frame is
36+ flat, when the transform is 1D/identity (`TxType::is_2d_transform`, libaom
37+ `is_2d_transform`: `V_DCT`/`H_DCT`/`IDTX` read the NULL slot), or when the
38+ block is lossless (segment-adjusted quantizer index 0 with all five plane
39+ deltas zero -- libaom `av1_use_qmatrix`'s `!xd->lossless[seg]`, using the
40+ same `block_q_idx` every other dequant call site reads).
41+- Plumbing: `qm: Option<Iqm>` threaded through
42+ `dequant_and_inverse_typed_wh` -> `quant::dequant_wh_into` and captured in
43+ `TxParams` (selected on the parse thread, so the recon worker scales
44+ identically). The encoder model, the lossless WHT path and the
45+ transform/quant tests pass `None` -- they model matrix-free streams.
46+
47+## The subtle half: the matrix index is COLUMN-major
48+
49+`get_dqv` indexes `iqmatrix[coeff_idx]` where `coeff_idx` is a position in
50+libaom's txb coefficient space, and that space is COLUMN-major (h-strided):
51+libaom's `mcol_scan_8x4` is the identity `0,1,2,...`, i.e. position `p`
52+strides DOWN the columns of the h-tall block. Our levels grid is row-major
53+(w-wide), so `dqv_at` re-strides: `idx = col * stride + row`, with
54+`stride = active height` (32 for the 64-clamps' square active area). My first
55+port used the row-major raster and was off by transposition at every
56+non-4x4 unit: the 4x4 matrices are symmetric, so twenty units matched and
57+masked it; the first rect unit (8x4 chroma) diverged, and the
58+instrumented-oracle coefficient diff (EC_DQCOEFF, 442 units, 27 diverging --
59+all rects plus 8x8+ squares) pinned the mapping. After the fix the whole
60+repro stream decodes byte-identical to the oracle/ffmpeg.
61+
62+## Non-vacuity
63+
64+`QM_SCALED_UNITS` (`quant.rs`, feature-gated like every counter): transform
65+units whose dequantisation actually scaled a nonzero level through a matrix.
66+Witness asserts > 0 on the qm=1 arm and == 0 on the qm-off control, plus the
67+parsed headers carrying `using_qmatrix = true` / `false` and levels 5/5/5.
68+
69+## Gates (this tree, quoted)
70+
71+- Witness (flipped from the old refusal gate, which self-described as
72+ "flip this gate to a witness"):
73+ `a_real_aomenc_quantisation_matrix_stream_decodes_pixel_exact`:
74+ qm-off control 3 frames pixel-exact vs ffmpeg (0 matrix-scaled units),
75+ qm-on 3 frames pixel-exact vs ffmpeg (420 matrix-scaled units, levels
76+ 5/5/5). PASS.
77+- Fail-pre-fix, measured: `decode_probe` built from a pristine
78+ `git archive` of 73f3ac64 refuses the same qm=1 stream:
79+ `REFUSED: unsupported: AV1 decode_stream (a frame using quantisation
80+ matrices (using_qmatrix=1): ...)`. The lane tree decodes it byte-exact.
81+- `qm::` unit tests: table anchors (level-0 luma/chroma 4x4 rows read off the
82+ C file), luma != chroma at the same level, level/shape selection over all
83+ 15 levels x 14 shapes x 2 kinds, 64-clamp reuse (pointer identity with the
84+ adjusted shape), `dqv_at` scaling/rounding/column-major transposition
85+ (the pos-1 vs pos-8 corner where row- and column-major disagree).
86+ 5 passed.
87+- Extra local probes, byte-exact vs the oracle/ffmpeg decode:
88+ `gradients` 256x192 cq40 cpu1 qm=1 (3 frames), and a genuine 10-bit
89+ `--bit-depth=10` qm=1 stream whose two frames carry levels 5/5/5 AND
90+ 7/7/7 (base_q_idx 29 / 116) -- second level table exercised at 10-bit.
91+- Regression arms, all green on this tree: `refusal_inventory` (+15),
92+ `a_non_420_subsampled_sequence_header_is_refused_by_name` (4:4:4 still
93+ refused by name), `a_real_libaom_monochrome_key_frame_decodes_pixel_exact`
94+ (mono witness), `a_reader_told_not_to_adapt` (cdf-disabled),
95+ `a_real_aomenc_mixed_lossless_segment_frame_is_refused_by_name`
96+ (segmentation-override), `the_reelb_*` hg fixtures,
97+ `a_real_aomenc_10bit_stream_decodes_pixel_exact`,
98+ `a_real_aomenc_10bit_inter_sequence_decodes_pixel_exact`,
99+ `a_10bit_key_frame_with_skipped_8x8_intra_leaves_that_split_their_transform_decodes_luma_exact`.
100+- `quant::` + `transform::` scoped tests: 29 passed (buffered-vs-per-coeff
101+ dequant, WHT lossless, tx-type round trips -- all through the `None` path).
102+- `cargo check -p ec-av1 --all-targets`: 0 warnings.
103+
104+## Class sweep
105+
106+Every residual-dequant site goes through `dequant_wh_into`; all 21 decode
107+call sites + 3 `TxParams` constructions now pass a selected matrix (or the
108+flat `None`), so no dequant can silently ignore a qm frame. Non-decode
109+callers kept on `None` (they model matrix-free streams): `encode.rs`'s two
110+reconstruction-model sites, `dequant_and_inverse_typed`/`dequant_and_inverse`
111+(test/model entry points), `dequant_coeff`/`dequant_coeff_wh` (spec 7.12.3
112+per-coefficient helpers, no decode caller), and the lossless WHT4x4 (a
113+lossless frame can never select a matrix). Chroma-from-luma, palette,
114+prediction and the loop filters never touch the quantizer.
115+
116+## Full suite
117+
118+Handed to Main for the server run per standing order (local runs kept to named
119+tests and one-fixture probes). Result to be appended by the verifier.