From e775b8bc623ff837951469f5dd1eadb482e440db Mon Sep 17 00:00:00 2001 From: Jingning Han Date: Thu, 26 Oct 2017 16:56:22 -0700 Subject: [PATCH] Re-design base level context modeling Reduce the context model size required to code the base level syntax values from 42 to 32 per transform size. Change-Id: I08485fdb563bc5c3323b0e147d69b47c286b9bb5 --- av1/common/entropymode.c | 177 +++++++++++++++------------------------ av1/common/onyxc_int.h | 4 +- av1/common/txb_common.c | 5 +- av1/common/txb_common.h | 99 +++++++++++++++++----- av1/encoder/encodetxb.c | 15 +++- 5 files changed, 163 insertions(+), 137 deletions(-) diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c index 2d539ed61..94aa21579 100644 --- a/av1/common/entropymode.c +++ b/av1/common/entropymode.c @@ -32,116 +32,77 @@ const aom_prob default_dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS] = { }; const aom_prob default_coeff_base - [TX_SIZES][PLANE_TYPES][NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS] = { - { // TX_4X4 + [TX_SIZES][PLANE_TYPES][NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS] = { - // PLANE_Y - { 73, 128, 131, 204, 165, 226, 169, 236, 18, 128, 51, - 153, 97, 179, 123, 201, 145, 226, 20, 128, 59, 153, - 107, 181, 129, 201, 142, 226, 3, 128, 19, 99, 46, - 135, 92, 166, 129, 190, 157, 217, 128, 128 }, - - { 128, 128, 178, 218, 192, 236, 186, 243, 55, 128, 110, - 183, 151, 205, 168, 221, 180, 238, 65, 128, 116, 178, - 157, 206, 172, 222, 183, 238, 24, 128, 65, 127, 104, - 164, 154, 195, 187, 216, 205, 230, 128, 128 }, - }, - { - // PLANE_UV - { 47, 128, 100, 176, 140, 207, 150, 223, 11, 128, 35, - 133, 79, 165, 115, 186, 129, 210, 8, 128, 30, 114, - 80, 159, 116, 187, 146, 214, 2, 128, 9, 59, 28, - 86, 71, 131, 117, 165, 149, 188, 128, 128 }, - - { 83, 128, 152, 205, 168, 227, 192, 238, 42, 128, 92, - 169, 138, 193, 165, 209, 128, 206, 36, 128, 86, 159, - 141, 198, 181, 213, 102, 223, 18, 128, 50, 132, 90, - 144, 141, 169, 180, 191, 128, 217, 128, 128 }, - } }, - { - // TX_8X8 - { - // PLANE_Y - { 82, 128, 143, 203, 177, 225, 186, 237, 7, 128, 37, - 109, 78, 151, 110, 182, 139, 213, 25, 128, 51, 115, - 86, 146, 111, 175, 125, 205, 3, 128, 12, 55, 32, - 78, 63, 111, 96, 148, 123, 185, 146, 206 }, - - { 136, 128, 182, 220, 201, 236, 205, 243, 46, 128, 101, - 164, 147, 194, 170, 218, 177, 234, 62, 128, 104, 146, - 143, 183, 165, 207, 183, 228, 30, 128, 60, 95, 95, - 128, 135, 163, 166, 196, 175, 219, 192, 231 }, - }, - { - // PLANE_UV - { 47, 128, 112, 189, 164, 202, 163, 218, 8, 128, 32, - 110, 68, 151, 102, 179, 134, 195, 5, 128, 22, 76, - 54, 103, 80, 146, 101, 182, 1, 128, 5, 39, 17, - 53, 46, 93, 79, 127, 112, 161, 64, 195 }, - { 90, 128, 156, 210, 183, 225, 128, 236, 39, 128, 98, - 164, 146, 201, 209, 219, 171, 208, 32, 128, 68, 123, - 119, 169, 154, 184, 128, 213, 15, 128, 38, 111, 83, - 112, 120, 163, 180, 170, 154, 213, 128, 205 }, - }, - }, - - { - // TX_16X16 - { - // PLANE_Y - { 96, 128, 169, 218, 208, 233, 187, 244, 10, 128, 34, - 101, 82, 153, 113, 184, 137, 212, 6, 128, 34, 104, - 81, 145, 109, 176, 147, 202, 1, 128, 3, 43, 15, - 53, 43, 89, 79, 129, 108, 168, 110, 194 }, - - { 156, 128, 206, 232, 218, 240, 128, 251, 39, 128, 108, - 161, 156, 202, 187, 216, 179, 234, 40, 128, 103, 152, - 144, 185, 159, 208, 205, 227, 14, 128, 39, 84, 76, - 110, 121, 151, 157, 187, 201, 206, 64, 216 }, - }, - { - // PLANE_UV - { 42, 128, 139, 211, 180, 230, 199, 238, 3, 128, 32, - 96, 69, 145, 102, 186, 117, 212, 4, 128, 25, 72, - 55, 111, 81, 159, 116, 198, 1, 128, 4, 22, 16, - 34, 35, 68, 63, 116, 89, 165, 102, 199 }, - - { 135, 128, 193, 227, 182, 239, 128, 246, 42, 128, 115, - 156, 146, 203, 188, 216, 128, 229, 32, 128, 82, 127, - 120, 178, 165, 203, 213, 229, 11, 128, 32, 73, 79, - 111, 129, 158, 162, 187, 156, 209, 85, 222 }, - }, - }, - - { - // TX_32X32 - { - // PLANE_Y - { 97, 128, 163, 232, 191, 246, 219, 252, 3, 128, 41, - 108, 91, 147, 104, 183, 118, 225, 6, 128, 45, 91, - 83, 125, 92, 160, 99, 215, 1, 128, 11, 36, 28, - 46, 43, 59, 57, 86, 73, 145, 91, 210 }, - - { 127, 128, 201, 239, 247, 248, 128, 254, 40, 128, 103, - 152, 158, 199, 186, 225, 181, 242, 38, 128, 92, 112, - 146, 189, 162, 217, 112, 239, 17, 128, 30, 47, 63, - 89, 113, 146, 147, 187, 168, 217, 150, 233 }, - }, - { - // PLANE_UV - { 65, 128, 155, 223, 166, 235, 154, 244, 15, 128, 57, - 154, 110, 199, 159, 224, 149, 239, 9, 128, 57, 140, - 97, 185, 148, 218, 176, 236, 1, 128, 3, 43, 19, - 42, 64, 98, 117, 167, 154, 199, 128, 158 }, - - { 130, 128, 189, 231, 171, 247, 128, 246, 63, 128, 132, - 222, 186, 224, 199, 244, 128, 247, 55, 128, 113, 211, - 164, 230, 225, 243, 128, 239, 7, 128, 31, 102, 106, - 138, 147, 183, 171, 223, 171, 224, 128, 128 }, - }, - }, - }; + { { { 222, 195, 160, 87, 108, 167, 209, 179, 60, 35, 31, + 83, 120, 154, 208, 181, 140, 45, 28, 85, 117, 148, + 201, 168, 86, 28, 19, 47, 81, 123, 129, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 230, 211, 198, 122, 175, 219, 221, 202, 106, 58, 95, + 152, 178, 198, 221, 201, 160, 97, 96, 151, 171, 199, + 214, 192, 124, 52, 67, 123, 150, 182, 170, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } }, + { { 206, 185, 149, 41, 87, 142, 188, 157, 50, 12, 20, + 60, 101, 141, 191, 161, 98, 15, 20, 69, 106, 138, + 176, 134, 50, 4, 6, 23, 51, 101, 116, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 218, 204, 187, 100, 161, 205, 211, 189, 106, 47, 83, + 138, 168, 165, 206, 185, 133, 62, 86, 143, 165, 182, + 201, 168, 95, 28, 47, 101, 126, 176, 174, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } } }, + + { { { 221, 188, 156, 103, 104, 162, 203, 160, 29, 15, 18, + 56, 93, 133, 203, 162, 108, 21, 17, 61, 94, 128, + 194, 142, 63, 5, 6, 21, 48, 90, 114, 134, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 230, 207, 196, 123, 177, 222, 218, 192, 82, 50, 83, + 141, 169, 195, 219, 192, 136, 75, 83, 142, 165, 181, + 209, 175, 99, 33, 53, 103, 134, 171, 175, 180, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } }, + { { 183, 158, 145, 45, 83, 123, 167, 125, 34, 11, 14, + 43, 70, 120, 143, 107, 65, 14, 12, 48, 69, 111, + 152, 111, 59, 3, 6, 22, 41, 84, 99, 123, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 217, 197, 177, 99, 158, 213, 201, 173, 73, 39, 72, + 128, 158, 189, 193, 157, 96, 49, 62, 120, 138, 166, + 169, 148, 114, 32, 54, 100, 117, 163, 140, 154, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } } }, + + { { { 219, 189, 180, 134, 131, 173, 197, 157, 40, 13, 18, + 51, 88, 128, 200, 155, 91, 17, 14, 55, 84, 122, + 181, 125, 49, 3, 4, 13, 30, 68, 97, 126, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 228, 207, 206, 136, 186, 220, 207, 188, 81, 44, 84, + 136, 167, 189, 217, 187, 131, 65, 76, 136, 164, 191, + 198, 161, 80, 25, 49, 92, 119, 159, 167, 180, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } }, + { { 206, 191, 210, 67, 109, 142, 145, 129, 39, 10, 15, + 35, 55, 88, 147, 118, 64, 9, 10, 39, 60, 115, + 142, 88, 7, 3, 6, 25, 45, 94, 108, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 211, 207, 224, 109, 181, 199, 171, 194, 96, 25, 77, + 125, 102, 128, 205, 166, 110, 32, 60, 132, 171, 128, + 171, 130, 13, 33, 61, 116, 142, 228, 213, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } } }, + + { { { 220, 198, 201, 139, 137, 187, 197, 166, 43, 20, 28, + 65, 94, 124, 191, 147, 90, 17, 18, 63, 84, 116, + 175, 117, 46, 2, 4, 16, 27, 63, 91, 119, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 232, 218, 219, 140, 212, 229, 203, 197, 125, 40, 92, + 142, 168, 168, 210, 182, 126, 68, 86, 133, 163, 180, + 197, 160, 89, 21, 42, 93, 113, 161, 171, 169, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } }, + { { 218, 229, 245, 77, 120, 171, 183, 181, 128, 4, 15, + 41, 94, 171, 67, 101, 57, 4, 7, 19, 50, 64, + 146, 109, 15, 1, 6, 35, 61, 143, 148, 205, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 }, + { 217, 217, 213, 86, 199, 171, 146, 197, 128, 47, 92, + 116, 171, 128, 171, 158, 64, 16, 54, 57, 128, 128, + 186, 155, 9, 41, 48, 135, 141, 253, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128 } } } + }; static const aom_prob default_nz_map[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS] = { { { 40, 157, 108, 70, 34, 19, 168, 98, 60, 32, 20, 128, diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h index d5fb410e2..230745e94 100644 --- a/av1/common/onyxc_int.h +++ b/av1/common/onyxc_int.h @@ -183,10 +183,10 @@ typedef struct BufferPool { #if CONFIG_LV_MAP typedef struct { - int base_ctx_table[2 /*row*/][2 /*col*/][2 /*sig_map*/] + int base_ctx_table[2 /*row*/][2 /*col*/][3 /*sig_map*/] [BASE_CONTEXT_POSITION_NUM + 1]; } LV_MAP_CTX_TABLE; -typedef int BASE_CTX_TABLE[2 /*col*/][2 /*sig_map*/] +typedef int BASE_CTX_TABLE[2 /*col*/][3 /*sig_map*/] [BASE_CONTEXT_POSITION_NUM + 1]; #endif diff --git a/av1/common/txb_common.c b/av1/common/txb_common.c index 921e65904..b89b40b33 100644 --- a/av1/common/txb_common.c +++ b/av1/common/txb_common.c @@ -221,8 +221,11 @@ void av1_init_lv_map(AV1_COMMON *cm) { LV_MAP_CTX_TABLE *coeff_ctx_table = &cm->coeff_ctx_table; for (int row = 0; row < 2; ++row) { for (int col = 0; col < 2; ++col) { - for (int sig_mag = 0; sig_mag < 2; ++sig_mag) { + for (int sig_mag = 0; sig_mag < 3; ++sig_mag) { for (int count = 0; count < BASE_CONTEXT_POSITION_NUM + 1; ++count) { + if (row == 0 && col == 0 && count > 5) continue; + if ((row == 0 || col == 0) && count > 8) continue; + coeff_ctx_table->base_ctx_table[row][col][sig_mag][count] = get_base_ctx_from_count_mag(row, col, count, sig_mag); } diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h index 76ab7b5c2..cec81edca 100644 --- a/av1/common/txb_common.h +++ b/av1/common/txb_common.h @@ -83,7 +83,7 @@ static INLINE int get_level_count_mag( const int (*nb_offset)[2], const int nb_num) { const int stride = 1 << bwl; int count = 0; - *mag = 0; + for (int idx = 0; idx < nb_num; ++idx) { const int ref_row = row + nb_offset[idx][0]; const int ref_col = col + nb_offset[idx][1]; @@ -91,8 +91,9 @@ static INLINE int get_level_count_mag( continue; const int pos = (ref_row << bwl) + ref_col; count += levels[pos] > level; - if (nb_offset[idx][0] >= 0 && nb_offset[idx][1] >= 0) - *mag = AOMMAX(*mag, levels[pos]); + if (nb_offset[idx][0] == 0 && nb_offset[idx][1] == 1) mag[0] = levels[pos]; + if (nb_offset[idx][0] == 1 && nb_offset[idx][1] == 0) mag[1] = levels[pos]; + if (nb_offset[idx][0] == 1 && nb_offset[idx][1] == 1) mag[2] = levels[pos]; } return count; } @@ -103,7 +104,6 @@ static INLINE int get_level_count_mag_coeff( const int (*nb_offset)[2], const int nb_num) { const int stride = 1 << bwl; int count = 0; - *mag = 0; for (int idx = 0; idx < nb_num; ++idx) { const int ref_row = row + nb_offset[idx][0]; const int ref_col = col + nb_offset[idx][1]; @@ -112,8 +112,10 @@ static INLINE int get_level_count_mag_coeff( const int pos = (ref_row << bwl) + ref_col; tran_low_t abs_coeff = abs(tcoeffs[pos]); count += abs_coeff > level; - if (nb_offset[idx][0] >= 0 && nb_offset[idx][1] >= 0) - *mag = AOMMAX(*mag, abs_coeff); + + if (nb_offset[idx][0] == 0 && nb_offset[idx][1] == 1) mag[0] = abs_coeff; + if (nb_offset[idx][0] == 1 && nb_offset[idx][1] == 0) mag[1] = abs_coeff; + if (nb_offset[idx][0] == 1 && nb_offset[idx][1] == 1) mag[2] = abs_coeff; } return count; } @@ -122,21 +124,66 @@ static INLINE int get_base_ctx_from_count_mag(int row, int col, int count, int sig_mag) { const int ctx = (count + 1) >> 1; int ctx_idx = -1; + if (row == 0 && col == 0) { - ctx_idx = (ctx << 1) + sig_mag; - // TODO(angiebird): turn this on once the optimization is finalized - // assert(ctx_idx < 8); + if (sig_mag >= 2) return ctx_idx = 0; + if (sig_mag == 1) { + if (count >= 2) + ctx_idx = 1; + else + ctx_idx = 2; + + return ctx_idx; + } + + ctx_idx = 3 + ctx; + assert(ctx_idx <= 6); + + return ctx_idx; } else if (row == 0) { - ctx_idx = 8 + (ctx << 1) + sig_mag; - // TODO(angiebird): turn this on once the optimization is finalized - // assert(ctx_idx < 18); + if (sig_mag >= 2) return ctx_idx = 6; + if (sig_mag == 1) { + if (count >= 2) + ctx_idx = 7; + else + ctx_idx = 8; + return ctx_idx; + } + + ctx_idx = 9 + ctx; + + assert(ctx_idx <= 13); + + return ctx_idx; } else if (col == 0) { - ctx_idx = 8 + 10 + (ctx << 1) + sig_mag; + if (sig_mag >= 2) return ctx_idx = 14; + if (sig_mag == 1) { + if (count >= 2) + ctx_idx = 15; + else + ctx_idx = 16; + + return ctx_idx; + } + + ctx_idx = 17 + ctx; + + assert(ctx_idx <= 21); // TODO(angiebird): turn this on once the optimization is finalized // assert(ctx_idx < 28); } else { - ctx_idx = 8 + 10 + 10 + (ctx << 1) + sig_mag; - assert(ctx_idx < COEFF_BASE_CONTEXTS); + if (sig_mag >= 2) return ctx_idx = 22; + if (sig_mag == 1) { + if (count >= 2) + ctx_idx = 23; + else + ctx_idx = 24; + return ctx_idx; + } + + ctx_idx = 25 + ctx; + + assert(ctx_idx <= 31); } return ctx_idx; } @@ -148,11 +195,15 @@ static INLINE int get_base_ctx(const uint8_t *const levels, const int row = c >> bwl; const int col = c - (row << bwl); const int level_minus_1 = level - 1; - int mag; + int mag_count = 0; + int nb_mag[3] = { 0 }; const int count = - get_level_count_mag(&mag, levels, bwl, height, row, col, level_minus_1, + get_level_count_mag(nb_mag, levels, bwl, height, row, col, level_minus_1, base_ref_offset, BASE_CONTEXT_POSITION_NUM); - const int ctx_idx = get_base_ctx_from_count_mag(row, col, count, mag > level); + + for (int idx = 0; idx < 3; ++idx) mag_count += nb_mag[idx] > level; + const int ctx_idx = + get_base_ctx_from_count_mag(row, col, count, AOMMIN(2, mag_count)); return ctx_idx; } @@ -244,10 +295,12 @@ static INLINE int get_br_ctx(const uint8_t *const levels, const int row = c >> bwl; const int col = c - (row << bwl); const int level_minus_1 = NUM_BASE_LEVELS; - int mag; + int mag = 0; + int nb_mag[3] = { 0 }; const int count = - get_level_count_mag(&mag, levels, bwl, height, row, col, level_minus_1, + get_level_count_mag(nb_mag, levels, bwl, height, row, col, level_minus_1, br_ref_offset, BR_CONTEXT_POSITION_NUM); + for (int idx = 0; idx < 3; ++idx) mag = AOMMAX(mag, nb_mag[idx]); const int ctx = get_br_ctx_from_count_mag(row, col, count, mag); return ctx; } @@ -258,10 +311,12 @@ static INLINE int get_br_ctx_coeff(const tran_low_t *const tcoeffs, const int row = c >> bwl; const int col = c - (row << bwl); const int level_minus_1 = NUM_BASE_LEVELS; - int mag; - const int count = get_level_count_mag_coeff(&mag, tcoeffs, bwl, height, row, + int mag = 0; + int nb_mag[3] = { 0 }; + const int count = get_level_count_mag_coeff(nb_mag, tcoeffs, bwl, height, row, col, level_minus_1, br_ref_offset, BR_CONTEXT_POSITION_NUM); + for (int idx = 0; idx < 3; ++idx) mag = AOMMAX(mag, nb_mag[idx]); const int ctx = get_br_ctx_from_count_mag(row, col, count, mag); return ctx; } diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c index 9d14964b0..f5ba47c18 100644 --- a/av1/encoder/encodetxb.c +++ b/av1/encoder/encodetxb.c @@ -570,7 +570,8 @@ static INLINE void get_base_ctx_set(const tran_low_t *tcoeffs, const int row = c >> bwl; const int col = c - (row << bwl); const int stride = 1 << bwl; - int mag[NUM_BASE_LEVELS] = { 0 }; + int mag_count[NUM_BASE_LEVELS] = { 0 }; + int nb_mag[NUM_BASE_LEVELS][3] = { { 0 } }; int idx; tran_low_t abs_coeff; int i; @@ -587,13 +588,19 @@ static INLINE void get_base_ctx_set(const tran_low_t *tcoeffs, for (i = 0; i < NUM_BASE_LEVELS; ++i) { ctx_set[i] += abs_coeff > i; - if (base_ref_offset[idx][0] >= 0 && base_ref_offset[idx][1] >= 0) - mag[i] |= abs_coeff > (i + 1); + if (base_ref_offset[idx][0] == 0 && base_ref_offset[idx][1] == 1) + nb_mag[i][0] = abs_coeff; + if (base_ref_offset[idx][0] == 1 && base_ref_offset[idx][1] == 0) + nb_mag[i][1] = abs_coeff; + if (base_ref_offset[idx][0] == 1 && base_ref_offset[idx][1] == 1) + nb_mag[i][2] = abs_coeff; } } for (i = 0; i < NUM_BASE_LEVELS; ++i) { - ctx_set[i] = get_base_ctx_from_count_mag(row, col, ctx_set[i], mag[i]); + for (idx = 0; idx < 3; ++idx) mag_count[i] += nb_mag[i][idx] > i + 1; + ctx_set[i] = get_base_ctx_from_count_mag(row, col, ctx_set[i], + AOMMIN(2, mag_count[i])); } return; } -- GitLab