From d1893f64e01541648e40fb81ba4d1eb8857e2417 Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Wed, 16 Dec 2015 11:17:25 -0800 Subject: [PATCH] Code DCT tokens with ANS Change-Id: I452f9675325a5f45bfbbe3e7e135009a125539f1 --- vp10/common/entropy.c | 283 ++++++++++++++++++++++++++++++++++++- vp10/common/entropy.h | 11 ++ vp10/decoder/decodeframe.c | 186 ++++++++++++++++++++---- vp10/decoder/decoder.c | 3 + vp10/decoder/decoder.h | 12 ++ vp10/decoder/detokenize.c | 195 ++++++++++++++++++++++++- vp10/decoder/detokenize.h | 20 ++- vp10/encoder/bitstream.c | 117 +++++++++++++-- vp10/encoder/cost.c | 17 +++ vp10/encoder/cost.h | 5 + vp10/encoder/rd.c | 9 ++ vp10/encoder/tokenize.c | 11 +- vp10/encoder/tokenize.h | 2 + 13 files changed, 819 insertions(+), 52 deletions(-) diff --git a/vp10/common/entropy.c b/vp10/common/entropy.c index 3da08a61b..f60bcf5d4 100644 --- a/vp10/common/entropy.c +++ b/vp10/common/entropy.c @@ -133,7 +133,7 @@ const uint8_t vp10_pt_energy_class[ENTROPY_TOKENS] = { 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5 }; -// Model obtained from a 2-sided zero-centerd distribuition derived +// Model obtained from a 2-sided zero-centered distribution derived // from a Pareto distribution. The cdf of the distribution is: // cdf(x) = 0.5 + 0.5 * sgn(x) * [1 - {alpha/(alpha + |x|)} ^ beta] // @@ -405,6 +405,287 @@ const vpx_prob vp10_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES] = { {255, 246, 247, 255, 239, 255, 253, 255}, }; +#if CONFIG_ANS +// Model obtained from a 2-sided zero-centerd distribuition derived +// from a Pareto distribution. The cdf of the distribution is: +// cdf(x) = 0.5 + 0.5 * sgn(x) * [1 - {alpha/(alpha + |x|)} ^ beta] +// +// For a given beta and a given probablity of the 1-node, the alpha +// is first solved, and then the {alpha, beta} pair is used to generate +// the probabilities for the rest of the nodes. +// +// beta = 8 +// Values for tokens ONE_TOKEN through CATEGORY6_TOKEN included here. +// ZERO_TOKEN and EOB_TOKEN are coded as flags outside this coder. +const vpx_prob vp10_pareto8_token_probs[COEFF_PROB_MODELS] + [ENTROPY_TOKENS - 2] = { + {1, 1, 1, 1, 2, 4, 8, 14, 26, 198}, + {2, 2, 2, 2, 4, 7, 14, 26, 42, 155}, + {3, 3, 3, 3, 6, 11, 20, 34, 51, 122}, + {4, 4, 4, 4, 7, 14, 25, 41, 56, 97}, + {5, 5, 5, 5, 9, 17, 30, 46, 58, 76}, + {6, 6, 6, 5, 11, 20, 34, 50, 57, 61}, + {7, 7, 7, 6, 12, 22, 37, 53, 56, 49}, + {8, 8, 7, 7, 14, 25, 40, 54, 53, 40}, + {9, 9, 8, 8, 15, 27, 43, 55, 50, 32}, + {10, 10, 9, 9, 16, 29, 45, 55, 47, 26}, + {11, 10, 10, 10, 18, 31, 47, 55, 43, 21}, + {12, 11, 11, 10, 19, 32, 48, 55, 40, 18}, + {13, 12, 12, 11, 20, 34, 49, 54, 37, 14}, + {14, 13, 12, 12, 21, 35, 50, 53, 34, 12}, + {15, 14, 13, 12, 22, 37, 51, 51, 31, 10}, + {16, 15, 14, 13, 23, 38, 51, 50, 28, 8}, + {17, 16, 15, 13, 24, 39, 51, 48, 26, 7}, + {18, 17, 15, 14, 25, 40, 52, 46, 23, 6}, + {19, 17, 16, 15, 26, 41, 51, 45, 21, 5}, + {20, 18, 17, 15, 27, 42, 51, 43, 19, 4}, + {21, 19, 17, 16, 28, 42, 51, 41, 18, 3}, + {22, 20, 18, 16, 28, 43, 51, 39, 16, 3}, + {23, 21, 19, 17, 29, 43, 50, 37, 14, 3}, + {24, 22, 19, 17, 30, 44, 49, 36, 13, 2}, + {25, 22, 20, 18, 30, 44, 49, 34, 12, 2}, + {26, 23, 20, 18, 31, 44, 48, 33, 11, 2}, + {27, 24, 21, 19, 31, 45, 47, 31, 10, 1}, + {28, 25, 22, 19, 32, 45, 46, 29, 9, 1}, + {29, 25, 22, 20, 32, 45, 46, 28, 8, 1}, + {30, 26, 23, 20, 33, 45, 45, 26, 7, 1}, + {31, 27, 23, 20, 33, 45, 44, 25, 7, 1}, + {32, 27, 24, 21, 33, 45, 43, 24, 6, 1}, + {33, 28, 24, 21, 34, 44, 42, 23, 6, 1}, + {34, 29, 25, 21, 34, 44, 41, 22, 5, 1}, + {35, 30, 25, 22, 34, 44, 40, 20, 5, 1}, + {36, 30, 26, 22, 35, 44, 39, 19, 4, 1}, + {37, 31, 26, 22, 35, 44, 38, 18, 4, 1}, + {38, 32, 27, 22, 35, 43, 37, 17, 4, 1}, + {39, 33, 27, 23, 35, 43, 36, 16, 3, 1}, + {40, 33, 27, 23, 35, 43, 35, 16, 3, 1}, + {41, 34, 28, 23, 35, 42, 34, 15, 3, 1}, + {42, 35, 28, 23, 36, 42, 33, 14, 2, 1}, + {43, 35, 29, 24, 35, 42, 32, 13, 2, 1}, + {44, 36, 29, 24, 36, 41, 31, 12, 2, 1}, + {45, 36, 29, 24, 36, 41, 30, 12, 2, 1}, + {46, 37, 30, 24, 35, 40, 30, 11, 2, 1}, + {47, 37, 30, 24, 36, 40, 29, 10, 2, 1}, + {48, 38, 30, 24, 36, 40, 28, 10, 1, 1}, + {49, 39, 31, 24, 36, 39, 27, 9, 1, 1}, + {50, 39, 31, 25, 35, 39, 26, 9, 1, 1}, + {51, 40, 31, 25, 36, 38, 25, 8, 1, 1}, + {52, 40, 31, 25, 35, 38, 25, 8, 1, 1}, + {53, 41, 32, 25, 35, 37, 24, 7, 1, 1}, + {54, 41, 32, 25, 35, 37, 23, 7, 1, 1}, + {55, 42, 32, 25, 35, 36, 22, 7, 1, 1}, + {56, 42, 33, 25, 35, 35, 22, 6, 1, 1}, + {57, 43, 33, 25, 34, 35, 21, 6, 1, 1}, + {58, 43, 33, 25, 35, 34, 20, 6, 1, 1}, + {59, 44, 33, 25, 34, 34, 20, 5, 1, 1}, + {60, 45, 33, 25, 34, 33, 19, 5, 1, 1}, + {61, 45, 33, 25, 34, 33, 18, 5, 1, 1}, + {62, 45, 34, 25, 34, 32, 18, 4, 1, 1}, + {63, 46, 34, 25, 33, 32, 17, 4, 1, 1}, + {64, 46, 34, 25, 33, 31, 17, 4, 1, 1}, + {65, 47, 34, 25, 33, 30, 16, 4, 1, 1}, + {66, 47, 34, 25, 33, 30, 15, 4, 1, 1}, + {67, 48, 34, 25, 33, 29, 15, 3, 1, 1}, + {68, 48, 35, 25, 32, 29, 14, 3, 1, 1}, + {69, 48, 35, 25, 32, 28, 14, 3, 1, 1}, + {70, 49, 35, 25, 32, 27, 13, 3, 1, 1}, + {71, 49, 35, 25, 31, 27, 13, 3, 1, 1}, + {72, 49, 35, 25, 31, 27, 12, 3, 1, 1}, + {73, 50, 35, 25, 31, 26, 12, 2, 1, 1}, + {74, 50, 35, 25, 31, 25, 12, 2, 1, 1}, + {75, 51, 35, 25, 30, 25, 11, 2, 1, 1}, + {76, 51, 35, 25, 30, 24, 11, 2, 1, 1}, + {77, 51, 35, 25, 30, 24, 10, 2, 1, 1}, + {78, 52, 35, 24, 29, 24, 10, 2, 1, 1}, + {79, 52, 35, 24, 29, 23, 10, 2, 1, 1}, + {80, 52, 35, 24, 29, 23, 9, 2, 1, 1}, + {81, 53, 35, 24, 28, 22, 9, 2, 1, 1}, + {82, 53, 35, 24, 28, 22, 9, 1, 1, 1}, + {83, 54, 35, 24, 28, 21, 8, 1, 1, 1}, + {84, 54, 35, 24, 27, 21, 8, 1, 1, 1}, + {85, 54, 35, 24, 27, 20, 8, 1, 1, 1}, + {86, 54, 35, 24, 27, 20, 7, 1, 1, 1}, + {87, 55, 35, 23, 27, 19, 7, 1, 1, 1}, + {88, 55, 35, 23, 26, 19, 7, 1, 1, 1}, + {89, 55, 35, 23, 26, 18, 7, 1, 1, 1}, + {90, 55, 35, 23, 26, 18, 6, 1, 1, 1}, + {91, 56, 35, 23, 25, 17, 6, 1, 1, 1}, + {92, 56, 35, 22, 25, 17, 6, 1, 1, 1}, + {93, 56, 35, 22, 24, 17, 6, 1, 1, 1}, + {94, 57, 35, 22, 24, 16, 5, 1, 1, 1}, + {95, 56, 35, 22, 24, 16, 5, 1, 1, 1}, + {96, 57, 35, 22, 23, 15, 5, 1, 1, 1}, + {97, 56, 35, 22, 23, 15, 5, 1, 1, 1}, + {98, 57, 34, 21, 23, 15, 5, 1, 1, 1}, + {99, 57, 35, 21, 23, 14, 4, 1, 1, 1}, + {100, 58, 34, 21, 22, 14, 4, 1, 1, 1}, + {101, 57, 34, 21, 22, 14, 4, 1, 1, 1}, + {102, 58, 34, 21, 21, 13, 4, 1, 1, 1}, + {103, 57, 34, 21, 21, 13, 4, 1, 1, 1}, + {104, 57, 34, 20, 21, 13, 4, 1, 1, 1}, + {105, 58, 34, 20, 20, 12, 4, 1, 1, 1}, + {106, 58, 34, 20, 20, 12, 3, 1, 1, 1}, + {107, 58, 33, 20, 20, 12, 3, 1, 1, 1}, + {108, 59, 33, 20, 19, 11, 3, 1, 1, 1}, + {109, 59, 33, 19, 19, 11, 3, 1, 1, 1}, + {110, 58, 33, 19, 19, 11, 3, 1, 1, 1}, + {111, 59, 33, 19, 18, 10, 3, 1, 1, 1}, + {112, 58, 33, 19, 18, 10, 3, 1, 1, 1}, + {113, 58, 32, 19, 18, 10, 3, 1, 1, 1}, + {114, 59, 32, 18, 18, 10, 2, 1, 1, 1}, + {115, 60, 32, 18, 17, 9, 2, 1, 1, 1}, + {116, 59, 32, 18, 17, 9, 2, 1, 1, 1}, + {117, 59, 32, 18, 16, 9, 2, 1, 1, 1}, + {118, 59, 31, 18, 16, 9, 2, 1, 1, 1}, + {119, 59, 32, 17, 16, 8, 2, 1, 1, 1}, + {120, 59, 31, 17, 16, 8, 2, 1, 1, 1}, + {121, 59, 31, 17, 15, 8, 2, 1, 1, 1}, + {122, 59, 30, 17, 15, 8, 2, 1, 1, 1}, + {123, 59, 30, 17, 15, 7, 2, 1, 1, 1}, + {124, 59, 30, 16, 15, 7, 2, 1, 1, 1}, + {125, 59, 30, 16, 14, 7, 2, 1, 1, 1}, + {126, 59, 30, 16, 14, 7, 1, 1, 1, 1}, + {127, 59, 30, 16, 14, 6, 1, 1, 1, 1}, + {128, 59, 30, 16, 13, 6, 1, 1, 1, 1}, + {129, 59, 30, 15, 13, 6, 1, 1, 1, 1}, + {130, 59, 29, 15, 13, 6, 1, 1, 1, 1}, + {131, 59, 29, 15, 12, 6, 1, 1, 1, 1}, + {132, 59, 28, 15, 12, 6, 1, 1, 1, 1}, + {133, 59, 28, 15, 12, 5, 1, 1, 1, 1}, + {134, 59, 28, 14, 12, 5, 1, 1, 1, 1}, + {135, 59, 28, 14, 11, 5, 1, 1, 1, 1}, + {136, 58, 28, 14, 11, 5, 1, 1, 1, 1}, + {137, 58, 27, 14, 11, 5, 1, 1, 1, 1}, + {138, 58, 27, 13, 11, 5, 1, 1, 1, 1}, + {139, 58, 27, 13, 11, 4, 1, 1, 1, 1}, + {140, 58, 27, 13, 10, 4, 1, 1, 1, 1}, + {141, 58, 26, 13, 10, 4, 1, 1, 1, 1}, + {142, 57, 26, 13, 10, 4, 1, 1, 1, 1}, + {143, 57, 26, 12, 10, 4, 1, 1, 1, 1}, + {144, 57, 26, 12, 9, 4, 1, 1, 1, 1}, + {145, 57, 25, 12, 9, 4, 1, 1, 1, 1}, + {146, 57, 25, 12, 9, 3, 1, 1, 1, 1}, + {147, 57, 25, 11, 9, 3, 1, 1, 1, 1}, + {148, 57, 25, 11, 8, 3, 1, 1, 1, 1}, + {149, 57, 24, 11, 8, 3, 1, 1, 1, 1}, + {150, 56, 24, 11, 8, 3, 1, 1, 1, 1}, + {151, 56, 23, 11, 8, 3, 1, 1, 1, 1}, + {152, 56, 23, 10, 8, 3, 1, 1, 1, 1}, + {153, 56, 23, 10, 7, 3, 1, 1, 1, 1}, + {154, 55, 23, 10, 7, 3, 1, 1, 1, 1}, + {155, 55, 22, 10, 7, 3, 1, 1, 1, 1}, + {156, 55, 22, 10, 7, 2, 1, 1, 1, 1}, + {157, 54, 22, 10, 7, 2, 1, 1, 1, 1}, + {158, 54, 22, 9, 7, 2, 1, 1, 1, 1}, + {159, 55, 21, 9, 6, 2, 1, 1, 1, 1}, + {160, 54, 21, 9, 6, 2, 1, 1, 1, 1}, + {161, 53, 21, 9, 6, 2, 1, 1, 1, 1}, + {162, 53, 20, 9, 6, 2, 1, 1, 1, 1}, + {163, 53, 20, 8, 6, 2, 1, 1, 1, 1}, + {164, 53, 20, 8, 5, 2, 1, 1, 1, 1}, + {165, 52, 20, 8, 5, 2, 1, 1, 1, 1}, + {166, 52, 19, 8, 5, 2, 1, 1, 1, 1}, + {167, 51, 19, 8, 5, 2, 1, 1, 1, 1}, + {168, 51, 19, 7, 5, 2, 1, 1, 1, 1}, + {169, 51, 19, 7, 5, 1, 1, 1, 1, 1}, + {170, 51, 18, 7, 5, 1, 1, 1, 1, 1}, + {171, 51, 18, 7, 4, 1, 1, 1, 1, 1}, + {172, 50, 18, 7, 4, 1, 1, 1, 1, 1}, + {173, 50, 17, 7, 4, 1, 1, 1, 1, 1}, + {174, 49, 17, 7, 4, 1, 1, 1, 1, 1}, + {175, 49, 17, 6, 4, 1, 1, 1, 1, 1}, + {176, 49, 16, 6, 4, 1, 1, 1, 1, 1}, + {177, 48, 16, 6, 4, 1, 1, 1, 1, 1}, + {178, 47, 16, 6, 4, 1, 1, 1, 1, 1}, + {179, 47, 16, 6, 3, 1, 1, 1, 1, 1}, + {180, 47, 15, 6, 3, 1, 1, 1, 1, 1}, + {181, 47, 15, 5, 3, 1, 1, 1, 1, 1}, + {182, 46, 15, 5, 3, 1, 1, 1, 1, 1}, + {183, 46, 14, 5, 3, 1, 1, 1, 1, 1}, + {184, 45, 14, 5, 3, 1, 1, 1, 1, 1}, + {185, 44, 14, 5, 3, 1, 1, 1, 1, 1}, + {186, 44, 13, 5, 3, 1, 1, 1, 1, 1}, + {187, 43, 13, 5, 3, 1, 1, 1, 1, 1}, + {188, 44, 13, 4, 2, 1, 1, 1, 1, 1}, + {189, 43, 13, 4, 2, 1, 1, 1, 1, 1}, + {190, 43, 12, 4, 2, 1, 1, 1, 1, 1}, + {191, 42, 12, 4, 2, 1, 1, 1, 1, 1}, + {192, 41, 12, 4, 2, 1, 1, 1, 1, 1}, + {193, 41, 11, 4, 2, 1, 1, 1, 1, 1}, + {194, 40, 11, 4, 2, 1, 1, 1, 1, 1}, + {195, 39, 11, 4, 2, 1, 1, 1, 1, 1}, + {196, 39, 11, 3, 2, 1, 1, 1, 1, 1}, + {197, 39, 10, 3, 2, 1, 1, 1, 1, 1}, + {198, 38, 10, 3, 2, 1, 1, 1, 1, 1}, + {199, 37, 10, 3, 2, 1, 1, 1, 1, 1}, + {200, 37, 10, 3, 1, 1, 1, 1, 1, 1}, + {201, 37, 9, 3, 1, 1, 1, 1, 1, 1}, + {202, 36, 9, 3, 1, 1, 1, 1, 1, 1}, + {203, 35, 9, 3, 1, 1, 1, 1, 1, 1}, + {204, 35, 8, 3, 1, 1, 1, 1, 1, 1}, + {205, 35, 8, 2, 1, 1, 1, 1, 1, 1}, + {206, 34, 8, 2, 1, 1, 1, 1, 1, 1}, + {207, 33, 8, 2, 1, 1, 1, 1, 1, 1}, + {208, 32, 8, 2, 1, 1, 1, 1, 1, 1}, + {209, 32, 7, 2, 1, 1, 1, 1, 1, 1}, + {210, 31, 7, 2, 1, 1, 1, 1, 1, 1}, + {211, 30, 7, 2, 1, 1, 1, 1, 1, 1}, + {212, 30, 6, 2, 1, 1, 1, 1, 1, 1}, + {213, 29, 6, 2, 1, 1, 1, 1, 1, 1}, + {214, 28, 6, 2, 1, 1, 1, 1, 1, 1}, + {215, 27, 6, 2, 1, 1, 1, 1, 1, 1}, + {216, 27, 6, 1, 1, 1, 1, 1, 1, 1}, + {217, 27, 5, 1, 1, 1, 1, 1, 1, 1}, + {218, 26, 5, 1, 1, 1, 1, 1, 1, 1}, + {219, 25, 5, 1, 1, 1, 1, 1, 1, 1}, + {220, 24, 5, 1, 1, 1, 1, 1, 1, 1}, + {221, 24, 4, 1, 1, 1, 1, 1, 1, 1}, + {222, 23, 4, 1, 1, 1, 1, 1, 1, 1}, + {223, 22, 4, 1, 1, 1, 1, 1, 1, 1}, + {224, 21, 4, 1, 1, 1, 1, 1, 1, 1}, + {225, 20, 4, 1, 1, 1, 1, 1, 1, 1}, + {226, 20, 3, 1, 1, 1, 1, 1, 1, 1}, + {227, 19, 3, 1, 1, 1, 1, 1, 1, 1}, + {228, 18, 3, 1, 1, 1, 1, 1, 1, 1}, + {229, 17, 3, 1, 1, 1, 1, 1, 1, 1}, + {230, 16, 3, 1, 1, 1, 1, 1, 1, 1}, + {231, 16, 2, 1, 1, 1, 1, 1, 1, 1}, + {232, 15, 2, 1, 1, 1, 1, 1, 1, 1}, + {233, 14, 2, 1, 1, 1, 1, 1, 1, 1}, + {234, 13, 2, 1, 1, 1, 1, 1, 1, 1}, + {235, 12, 2, 1, 1, 1, 1, 1, 1, 1}, + {236, 11, 2, 1, 1, 1, 1, 1, 1, 1}, + {237, 11, 1, 1, 1, 1, 1, 1, 1, 1}, + {238, 10, 1, 1, 1, 1, 1, 1, 1, 1}, + {239, 9, 1, 1, 1, 1, 1, 1, 1, 1}, + {240, 8, 1, 1, 1, 1, 1, 1, 1, 1}, + {241, 7, 1, 1, 1, 1, 1, 1, 1, 1}, + {242, 6, 1, 1, 1, 1, 1, 1, 1, 1}, + {243, 5, 1, 1, 1, 1, 1, 1, 1, 1}, + {244, 4, 1, 1, 1, 1, 1, 1, 1, 1}, + {245, 3, 1, 1, 1, 1, 1, 1, 1, 1}, + {246, 2, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {247, 1, 1, 1, 1, 1, 1, 1, 1, 1}, +}; + +void vp10_build_pareto8_dec_tab( + const vpx_prob token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2], + rans_dec_lut dec_tab[COEFF_PROB_MODELS]) { + int p; + for (p = 0; p < COEFF_PROB_MODELS; ++p) { + rans_build_dec_tab(token_probs[p], dec_tab[p]); + } +} +#endif // CONFIG_ANS + static const vp10_coeff_probs_model default_coef_probs_4x4[PLANE_TYPES] = { { // Y plane { // Intra diff --git a/vp10/common/entropy.h b/vp10/common/entropy.h index c1de3b265..4da0bfbff 100644 --- a/vp10/common/entropy.h +++ b/vp10/common/entropy.h @@ -14,6 +14,9 @@ #include "vpx/vpx_integer.h" #include "vpx_dsp/prob.h" +#if CONFIG_ANS +#include "vp10/common/ans.h" +#endif // CONFIG_ANS #include "vp10/common/common.h" #include "vp10/common/enums.h" @@ -163,6 +166,14 @@ static INLINE const uint8_t *get_band_translate(TX_SIZE tx_size) { #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES) extern const vpx_tree_index vp10_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)]; extern const vpx_prob vp10_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES]; +#if CONFIG_ANS +extern const vpx_prob + vp10_pareto8_token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2]; + +void vp10_build_pareto8_dec_tab( + const vpx_prob token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2], + rans_dec_lut dec_tab[COEFF_PROB_MODELS]); +#endif // CONFIG_ANS typedef vpx_prob vp10_coeff_probs_model[REF_TYPES][COEF_BANDS] [COEFF_CONTEXTS][UNCONSTRAINED_NODES]; diff --git a/vp10/decoder/decodeframe.c b/vp10/decoder/decodeframe.c index 44b5bc406..4f2af60db 100644 --- a/vp10/decoder/decodeframe.c +++ b/vp10/decoder/decodeframe.c @@ -350,7 +350,12 @@ static void inverse_transform_block_intra(MACROBLOCKD* xd, int plane, } static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd, +#if CONFIG_ANS + const rans_dec_lut *const token_tab, + struct AnsDecoder *const r, +#else vpx_reader *r, +#endif // CONFIG_ANS MB_MODE_INFO *const mbmi, int plane, int row, int col, @@ -373,7 +378,11 @@ static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd, if (!mbmi->skip) { TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size); const scan_order *sc = get_scan(tx_size, tx_type, 0); - const int eob = vp10_decode_block_tokens(xd, plane, sc, col, row, tx_size, + const int eob = vp10_decode_block_tokens(xd, +#if CONFIG_ANS + token_tab, +#endif // CONFIG_ANS + plane, sc, col, row, tx_size, r, mbmi->segment_id); inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst, pd->dst.stride, eob); @@ -437,7 +446,13 @@ static void decode_reconstruct_tx(MACROBLOCKD *const xd, vpx_reader *r, } #endif // CONFIG_VAR_TX -static int reconstruct_inter_block(MACROBLOCKD *const xd, vpx_reader *r, +static int reconstruct_inter_block(MACROBLOCKD *const xd, +#if CONFIG_ANS + const rans_dec_lut *const token_tab, + struct AnsDecoder *const r, +#else + vpx_reader *r, +#endif MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) { struct macroblockd_plane *const pd = &xd->plane[plane]; @@ -445,8 +460,12 @@ static int reconstruct_inter_block(MACROBLOCKD *const xd, vpx_reader *r, int block_idx = (row << 1) + col; TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size); const scan_order *sc = get_scan(tx_size, tx_type, 1); - const int eob = vp10_decode_block_tokens(xd, plane, sc, col, row, tx_size, r, - mbmi->segment_id); + const int eob = vp10_decode_block_tokens(xd, +#if CONFIG_ANS + token_tab, +#endif + plane, sc, col, row, tx_size, r, + mbmi->segment_id); inverse_transform_block_inter(xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col], @@ -1513,7 +1532,11 @@ static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd, int supertx_enabled, #endif // CONFIG_SUPERTX int mi_row, int mi_col, - vpx_reader *r, BLOCK_SIZE bsize, + vpx_reader *r, +#if CONFIG_ANS + struct AnsDecoder *const tok, +#endif // CONFIG_ANS + BLOCK_SIZE bsize, int bwl, int bhl) { VP10_COMMON *const cm = &pbi->common; const int less8x8 = bsize < BLOCK_8X8; @@ -1573,7 +1596,13 @@ static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd, for (row = 0; row < max_blocks_high; row += step) for (col = 0; col < max_blocks_wide; col += step) - predict_and_reconstruct_intra_block(xd, r, mbmi, plane, + predict_and_reconstruct_intra_block(xd, +#if CONFIG_ANS + pbi->token_tab, tok, +#else + r, +#endif + mbmi, plane, row, col, tx_size); } } else { @@ -1621,7 +1650,13 @@ static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd, for (row = 0; row < max_blocks_high; row += step) for (col = 0; col < max_blocks_wide; col += step) - eobtotal += reconstruct_inter_block(xd, r, mbmi, plane, row, col, + eobtotal += reconstruct_inter_block(xd, +#if CONFIG_ANS + pbi->token_tab, tok, +#else + r, +#endif + mbmi, plane, row, col, tx_size); #endif } @@ -1704,7 +1739,11 @@ static void decode_partition(VP10Decoder *const pbi, MACROBLOCKD *const xd, int supertx_enabled, #endif int mi_row, int mi_col, - vpx_reader* r, BLOCK_SIZE bsize, int n4x4_l2) { + vpx_reader* r, +#if CONFIG_ANS + struct AnsDecoder *const tok, +#endif // CONFIG_ANS + BLOCK_SIZE bsize, int n4x4_l2) { VP10_COMMON *const cm = &pbi->common; const int n8x8_l2 = n4x4_l2 - 1; const int num_8x8_wh = 1 << n8x8_l2; @@ -1777,7 +1816,11 @@ static void decode_partition(VP10Decoder *const pbi, MACROBLOCKD *const xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col, r, subsize, 1, 1); + mi_row, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, 1, 1); } else { switch (partition) { case PARTITION_NONE: @@ -1785,55 +1828,91 @@ static void decode_partition(VP10Decoder *const pbi, MACROBLOCKD *const xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col, r, subsize, n4x4_l2, n4x4_l2); + mi_row, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n4x4_l2, n4x4_l2); break; case PARTITION_HORZ: decode_block(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col, r, subsize, n4x4_l2, n8x8_l2); + mi_row, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n4x4_l2, n8x8_l2); if (has_rows) decode_block(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row + hbs, mi_col, r, subsize, n4x4_l2, n8x8_l2); + mi_row + hbs, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n4x4_l2, n8x8_l2); break; case PARTITION_VERT: decode_block(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col, r, subsize, n8x8_l2, n4x4_l2); + mi_row, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n8x8_l2, n4x4_l2); if (has_cols) decode_block(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col + hbs, r, subsize, n8x8_l2, n4x4_l2); + mi_row, mi_col + hbs, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n8x8_l2, n4x4_l2); break; case PARTITION_SPLIT: decode_partition(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col, r, subsize, n8x8_l2); + mi_row, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n8x8_l2); decode_partition(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row, mi_col + hbs, r, subsize, n8x8_l2); + mi_row, mi_col + hbs, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n8x8_l2); decode_partition(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row + hbs, mi_col, r, subsize, n8x8_l2); + mi_row + hbs, mi_col, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n8x8_l2); decode_partition(pbi, xd, #if CONFIG_SUPERTX supertx_enabled, #endif // CONFIG_SUPERTX - mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2); + mi_row + hbs, mi_col + hbs, r, +#if CONFIG_ANS + tok, +#endif // CONFIG_ANS + subsize, n8x8_l2); break; default: assert(0 && "Invalid partition type"); @@ -1878,7 +1957,13 @@ static void decode_partition(VP10Decoder *const pbi, MACROBLOCKD *const xd, for (row = 0; row < max_blocks_high; row += step) for (col = 0; col < max_blocks_wide; col += step) - eobtotal += reconstruct_inter_block(xd, r, mbmi, i, row, col, + eobtotal += reconstruct_inter_block(xd, +#if CONFIG_ANS + pbi->token_tab, tok, +#else + r, +#endif + mbmi, i, row, col, tx_size); } if (!(subsize < BLOCK_8X8) && eobtotal == 0) @@ -1898,13 +1983,34 @@ static void decode_partition(VP10Decoder *const pbi, MACROBLOCKD *const xd, dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh); } +static void setup_bool_decoder(const uint8_t *data, + const uint8_t *data_end, + const size_t read_size, + struct vpx_internal_error_info *error_info, + vpx_reader *r, + vpx_decrypt_cb decrypt_cb, + void *decrypt_state) { + // Validate the calculated partition length. If the buffer + // described by the partition can't be fully read, then restrict + // it to the portion that can be (for EC mode) or throw an error. + if (!read_is_valid(data, read_size, data_end)) + vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, + "Truncated packet or corrupt tile length"); + + if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state)) + vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR, + "Failed to allocate bool decoder %d", 1); +} + static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end, - size_t read_size, + const size_t read_size, struct vpx_internal_error_info *error_info, - vpx_reader *r, + struct AnsDecoder *const ans, vpx_decrypt_cb decrypt_cb, void *decrypt_state) { + (void) decrypt_cb; + (void) decrypt_state; // Validate the calculated partition length. If the buffer // described by the partition can't be fully read, then restrict // it to the portion that can be (for EC mode) or throw an error. @@ -1912,9 +2018,9 @@ static void setup_token_decoder(const uint8_t *data, vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, "Truncated packet or corrupt tile length"); - if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state)) + if (read_size > INT_MAX || ans_read_init(ans, data, (int)read_size)) vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR, - "Failed to allocate bool decoder %d", 1); + "Failed to allocate token decoder %d", 1); } static void read_coef_probs_common(vp10_coeff_probs_model *coef_probs, @@ -2394,6 +2500,7 @@ static const uint8_t *decode_tiles(VP10Decoder *pbi, for (tile_row = 0; tile_row < tile_rows; ++tile_row) { for (tile_col = 0; tile_col < tile_cols; ++tile_col) { const TileBuffer *const buf = &tile_buffers[tile_row][tile_col]; + tile_data = pbi->tile_data + tile_cols * tile_row + tile_col; tile_data->cm = cm; tile_data->xd = pbi->mb; @@ -2403,9 +2510,21 @@ static const uint8_t *decode_tiles(VP10Decoder *pbi, &cm->counts : NULL; vp10_zero(tile_data->dqcoeff); vp10_tile_init(&tile_data->xd.tile, tile_data->cm, tile_row, tile_col); +#if !CONFIG_ANS + setup_bool_decoder(buf->data, data_end, buf->size, &cm->error, + &tile_data->bit_reader, pbi->decrypt_cb, + pbi->decrypt_state); +#else + if (buf->size < 3 || !read_is_valid(buf->data, buf->size, data_end)) + vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, + "Truncated packet or corrupt tile length"); + setup_bool_decoder(buf->data, data_end, buf->size, &cm->error, + &tile_data->bit_reader, pbi->decrypt_cb, + pbi->decrypt_state); setup_token_decoder(buf->data, data_end, buf->size, &cm->error, - &tile_data->bit_reader, pbi->decrypt_cb, + &tile_data->token_ans, pbi->decrypt_cb, pbi->decrypt_state); +#endif vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff); tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0]; tile_data->xd.plane[1].color_index_map = tile_data->color_index_map[1]; @@ -2434,6 +2553,9 @@ static const uint8_t *decode_tiles(VP10Decoder *pbi, 0, #endif mi_row, mi_col, &tile_data->bit_reader, +#if CONFIG_ANS + &tile_data->token_ans, +#endif // CONFIG_ANS BLOCK_64X64, 4); } pbi->mb.corrupted |= tile_data->xd.corrupted; @@ -2491,7 +2613,11 @@ static const uint8_t *decode_tiles(VP10Decoder *pbi, if (cm->frame_parallel_decode) vp10_frameworker_broadcast(pbi->cur_buf, INT_MAX); +#if CONFIG_ANS + return data_end; +#else return vpx_reader_find_end(&tile_data->bit_reader); +#endif } static int tile_worker_hook(TileWorkerData *const tile_data, @@ -2521,6 +2647,9 @@ static int tile_worker_hook(TileWorkerData *const tile_data, 0, #endif mi_row, mi_col, &tile_data->bit_reader, +#if CONFIG_ANS + &tile_data->token_ans, +#endif // CONFIG_ANS BLOCK_64X64, 4); } } @@ -2551,6 +2680,9 @@ static const uint8_t *decode_tiles_mt(VP10Decoder *pbi, assert(tile_cols <= (1 << 6)); assert(tile_rows == 1); (void)tile_rows; +#if CONFIG_ANS + abort(); // FIXME: Tile parsing broken +#endif // TODO(jzern): See if we can remove the restriction of passing in max // threads to the decoder. @@ -2650,9 +2782,9 @@ static const uint8_t *decode_tiles_mt(VP10Decoder *pbi, vp10_zero(tile_data->dqcoeff); vp10_tile_init(tile, cm, 0, buf->col); vp10_tile_init(&tile_data->xd.tile, cm, 0, buf->col); - setup_token_decoder(buf->data, data_end, buf->size, &cm->error, - &tile_data->bit_reader, pbi->decrypt_cb, - pbi->decrypt_state); + setup_bool_decoder(buf->data, data_end, buf->size, &cm->error, + &tile_data->bit_reader, pbi->decrypt_cb, + pbi->decrypt_state); vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff); tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0]; tile_data->xd.plane[1].color_index_map = tile_data->color_index_map[1]; diff --git a/vp10/decoder/decoder.c b/vp10/decoder/decoder.c index 2dbadb3b2..329e54cc1 100644 --- a/vp10/decoder/decoder.c +++ b/vp10/decoder/decoder.c @@ -115,6 +115,9 @@ VP10Decoder *vp10_decoder_create(BufferPool *const pool) { cm->setup_mi = vp10_dec_setup_mi; vp10_loop_filter_init(cm); +#if CONFIG_ANS + vp10_build_pareto8_dec_tab(vp10_pareto8_token_probs, pbi->token_tab); +#endif // CONFIG_ANS cm->error.setjmp = 0; diff --git a/vp10/decoder/decoder.h b/vp10/decoder/decoder.h index 72a631020..e590d8be1 100644 --- a/vp10/decoder/decoder.h +++ b/vp10/decoder/decoder.h @@ -18,6 +18,9 @@ #include "vpx_scale/yv12config.h" #include "vpx_util/vpx_thread.h" +#if CONFIG_ANS +#include "vp10/common/ans.h" +#endif #include "vp10/common/thread_common.h" #include "vp10/common/onyxc_int.h" #include "vp10/common/ppflags.h" @@ -31,6 +34,9 @@ extern "C" { typedef struct TileData { VP10_COMMON *cm; vpx_reader bit_reader; +#if CONFIG_ANS + struct AnsDecoder token_ans; +#endif // CONFIG_ANS DECLARE_ALIGNED(16, MACROBLOCKD, xd); /* dqcoeff are shared by all the planes. So planes must be decoded serially */ DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]); @@ -40,6 +46,9 @@ typedef struct TileData { typedef struct TileWorkerData { struct VP10Decoder *pbi; vpx_reader bit_reader; +#if CONFIG_ANS + struct AnsDecoder token_ans; +#endif // CONFIG_ANS FRAME_COUNTS counts; DECLARE_ALIGNED(16, MACROBLOCKD, xd); /* dqcoeff are shared by all the planes. So planes must be decoded serially */ @@ -80,6 +89,9 @@ typedef struct VP10Decoder { int inv_tile_order; int need_resync; // wait for key/intra-only frame. int hold_ref_buf; // hold the reference buffer. +#if CONFIG_ANS + rans_dec_lut token_tab[COEFF_PROB_MODELS]; +#endif // CONFIG_ANS } VP10Decoder; int vp10_receive_compressed_data(struct VP10Decoder *pbi, diff --git a/vp10/decoder/detokenize.c b/vp10/decoder/detokenize.c index 011c45acb..c5dec87bb 100644 --- a/vp10/decoder/detokenize.c +++ b/vp10/decoder/detokenize.c @@ -11,6 +11,7 @@ #include "vpx_mem/vpx_mem.h" #include "vpx_ports/mem.h" +#include "vp10/common/ans.h" #include "vp10/common/blockd.h" #include "vp10/common/common.h" #include "vp10/common/entropy.h" @@ -38,6 +39,7 @@ ++coef_counts[band][ctx][token]; \ } while (0) +#if !CONFIG_ANS static INLINE int read_coeff(const vpx_prob *probs, int n, vpx_reader *r) { int i, val = 0; for (i = 0; i < n; ++i) @@ -207,6 +209,175 @@ static int decode_coefs(const MACROBLOCKD *xd, return c; } +#else // !CONFIG_ANS +static INLINE int read_coeff(const vpx_prob *const probs, int n, + struct AnsDecoder *const ans) { + int i, val = 0; + for (i = 0; i < n; ++i) + val = (val << 1) | uabs_read(ans, probs[i]); + return val; +} + +static int decode_coefs_ans(const MACROBLOCKD *const xd, + const rans_dec_lut *const token_tab, + PLANE_TYPE type, + tran_low_t *dqcoeff, TX_SIZE tx_size, + const int16_t *dq, + int ctx, const int16_t *scan, const int16_t *nb, + struct AnsDecoder *const ans) { + FRAME_COUNTS *counts = xd->counts; + const int max_eob = 16 << (tx_size << 1); + const FRAME_CONTEXT *const fc = xd->fc; + const int ref = is_inter_block(&xd->mi[0]->mbmi); + int band, c = 0; + const vpx_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] = + fc->coef_probs[tx_size][type][ref]; + const vpx_prob *prob; + unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1]; + unsigned int (*eob_branch_count)[COEFF_CONTEXTS]; + uint8_t token_cache[32 * 32]; + const uint8_t *band_translate = get_band_translate(tx_size); + const int dq_shift = (tx_size == TX_32X32); + int v, token; + int16_t dqv = dq[0]; + const uint8_t *cat1_prob; + const uint8_t *cat2_prob; + const uint8_t *cat3_prob; + const uint8_t *cat4_prob; + const uint8_t *cat5_prob; + const uint8_t *cat6_prob; + + if (counts) { + coef_counts = counts->coef[tx_size][type][ref]; + eob_branch_count = counts->eob_branch[tx_size][type][ref]; + } + +#if CONFIG_VP9_HIGHBITDEPTH + if (xd->bd > VPX_BITS_8) { + if (xd->bd == VPX_BITS_10) { + cat1_prob = vp10_cat1_prob_high10; + cat2_prob = vp10_cat2_prob_high10; + cat3_prob = vp10_cat3_prob_high10; + cat4_prob = vp10_cat4_prob_high10; + cat5_prob = vp10_cat5_prob_high10; + cat6_prob = vp10_cat6_prob_high10; + } else { + cat1_prob = vp10_cat1_prob_high12; + cat2_prob = vp10_cat2_prob_high12; + cat3_prob = vp10_cat3_prob_high12; + cat4_prob = vp10_cat4_prob_high12; + cat5_prob = vp10_cat5_prob_high12; + cat6_prob = vp10_cat6_prob_high12; + } + } else { + cat1_prob = vp10_cat1_prob; + cat2_prob = vp10_cat2_prob; + cat3_prob = vp10_cat3_prob; + cat4_prob = vp10_cat4_prob; + cat5_prob = vp10_cat5_prob; + cat6_prob = vp10_cat6_prob; + } +#else + cat1_prob = vp10_cat1_prob; + cat2_prob = vp10_cat2_prob; + cat3_prob = vp10_cat3_prob; + cat4_prob = vp10_cat4_prob; + cat5_prob = vp10_cat5_prob; + cat6_prob = vp10_cat6_prob; +#endif + + while (c < max_eob) { + int val = -1; + band = *band_translate++; + prob = coef_probs[band][ctx]; + if (counts) + ++eob_branch_count[band][ctx]; + if (!uabs_read(ans, prob[EOB_CONTEXT_NODE])) { + INCREMENT_COUNT(EOB_MODEL_TOKEN); + break; + } + + while (!uabs_read(ans, prob[ZERO_CONTEXT_NODE])) { + INCREMENT_COUNT(ZERO_TOKEN); + dqv = dq[1]; + token_cache[scan[c]] = 0; + ++c; + if (c >= max_eob) + return c; // zero tokens at the end (no eob token) + ctx = get_coef_context(nb, token_cache, c); + band = *band_translate++; + prob = coef_probs[band][ctx]; + } + + token = ONE_TOKEN + rans_read(ans, token_tab[prob[PIVOT_NODE] - 1]); + INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN)); + switch (token) { + case ONE_TOKEN: + case TWO_TOKEN: + case THREE_TOKEN: + case FOUR_TOKEN: + val = token; + break; + case CATEGORY1_TOKEN: + val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, ans); + break; + case CATEGORY2_TOKEN: + val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, ans); + break; + case CATEGORY3_TOKEN: + val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, ans); + break; + case CATEGORY4_TOKEN: + val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, ans); + break; + case CATEGORY5_TOKEN: + val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, ans); + break; + case CATEGORY6_TOKEN: + { + const int skip_bits = TX_SIZES - 1 - tx_size; + const uint8_t *cat6p = cat6_prob + skip_bits; +#if CONFIG_VP9_HIGHBITDEPTH + switch (xd->bd) { + case VPX_BITS_8: + val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, ans); + break; + case VPX_BITS_10: + val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, ans); + break; + case VPX_BITS_12: + val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, ans); + break; + default: + assert(0); + return -1; + } +#else + val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, ans); +#endif + } + break; + } + v = (val * dqv) >> dq_shift; +#if CONFIG_COEFFICIENT_RANGE_CHECKING +#if CONFIG_VP9_HIGHBITDEPTH + dqcoeff[scan[c]] = highbd_check_range((uabs_read_bit(ans) ? -v : v), + xd->bd); +#else + dqcoeff[scan[c]] = check_range(uabs_read_bit(ans) ? -v : v); +#endif // CONFIG_VP9_HIGHBITDEPTH +#else + dqcoeff[scan[c]] = uabs_read_bit(ans) ? -v : v; +#endif // CONFIG_COEFFICIENT_RANGE_CHECKING + token_cache[scan[c]] = vp10_pt_energy_class[token]; + ++c; + ctx = get_coef_context(nb, token_cache, c); + dqv = dq[1]; + } + + return c; +} +#endif // !CONFIG_ANS // TODO(slavarnway): Decode version of vp10_set_context. Modify vp10_set_context // after testing is complete, then delete this version. @@ -280,18 +451,32 @@ void vp10_decode_palette_tokens(MACROBLOCKD *const xd, int plane, } } -int vp10_decode_block_tokens(MACROBLOCKD *xd, - int plane, const scan_order *sc, - int x, int y, - TX_SIZE tx_size, vpx_reader *r, - int seg_id) { +int vp10_decode_block_tokens(MACROBLOCKD *const xd, +#if CONFIG_ANS + const rans_dec_lut *const token_tab, +#endif // CONFIG_ANS + int plane, const scan_order *sc, + int x, int y, + TX_SIZE tx_size, +#if CONFIG_ANS + struct AnsDecoder *const r, +#else + vpx_reader *r, +#endif // CONFIG_ANS + int seg_id) { struct macroblockd_plane *const pd = &xd->plane[plane]; const int16_t *const dequant = pd->seg_dequant[seg_id]; const int ctx = get_entropy_context(tx_size, pd->above_context + x, pd->left_context + y); +#if !CONFIG_ANS const int eob = decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, dequant, ctx, sc->scan, sc->neighbors, r); +#else + const int eob = decode_coefs_ans(xd, token_tab, pd->plane_type, + pd->dqcoeff, tx_size, + dequant, ctx, sc->scan, sc->neighbors, r); +#endif // !CONFIG_ANS dec_set_contexts(xd, pd, tx_size, eob > 0, x, y); return eob; } diff --git a/vp10/decoder/detokenize.h b/vp10/decoder/detokenize.h index d2677f612..f87c6f028 100644 --- a/vp10/decoder/detokenize.h +++ b/vp10/decoder/detokenize.h @@ -12,8 +12,8 @@ #ifndef VP10_DECODER_DETOKENIZE_H_ #define VP10_DECODER_DETOKENIZE_H_ -#include "vpx_dsp/bitreader.h" #include "vp10/decoder/decoder.h" +#include "vp10/common/ans.h" #include "vp10/common/scan.h" #ifdef __cplusplus @@ -22,11 +22,19 @@ extern "C" { void vp10_decode_palette_tokens(MACROBLOCKD *const xd, int plane, vpx_reader *r); -int vp10_decode_block_tokens(MACROBLOCKD *xd, - int plane, const scan_order *sc, - int x, int y, - TX_SIZE tx_size, vpx_reader *r, - int seg_id); +int vp10_decode_block_tokens(MACROBLOCKD *const xd, +#if CONFIG_ANS + const rans_dec_lut *const token_tab, +#endif // CONFIG_ANS + int plane, const scan_order *sc, + int x, int y, + TX_SIZE tx_size, +#if CONFIG_ANS + struct AnsDecoder *const r, +#else + vpx_reader *r, +#endif // CONFIG_ANS + int seg_id); #ifdef __cplusplus } // extern "C" diff --git a/vp10/encoder/bitstream.c b/vp10/encoder/bitstream.c index 4c0c6af22..8aed44e8e 100644 --- a/vp10/encoder/bitstream.c +++ b/vp10/encoder/bitstream.c @@ -402,6 +402,7 @@ static void update_supertx_probs(VP10_COMMON *cm, vpx_writer *w) { } #endif // CONFIG_SUPERTX +#if !CONFIG_ANS static void pack_mb_tokens(vpx_writer *w, TOKENEXTRA **tp, const TOKENEXTRA *const stop, vpx_bit_depth_t bit_depth, const TX_SIZE tx) { @@ -486,6 +487,71 @@ static void pack_mb_tokens(vpx_writer *w, *tp = p; } +#else +// This function serializes the tokens backwards both in token order and +// bit order in each token. +static void pack_mb_tokens_ans(struct AnsCoder *const ans, + const TOKENEXTRA *const start, + const TOKENEXTRA *const stop, + vpx_bit_depth_t bit_depth) { + const TOKENEXTRA *p; + TX_SIZE tx_size = TX_SIZES; + + for (p = stop - 1; p >= start; --p) { + const int t = p->token; + if (t == EOSB_TOKEN) { + tx_size = (TX_SIZE)p->extra; + } else { +#if CONFIG_VP9_HIGHBITDEPTH + const vp10_extra_bit *const b = + (bit_depth == VPX_BITS_12) ? &vp10_extra_bits_high12[t] : + (bit_depth == VPX_BITS_10) ? &vp10_extra_bits_high10[t] : + &vp10_extra_bits[t]; +#else + const vp10_extra_bit *const b = &vp10_extra_bits[t]; + (void) bit_depth; +#endif // CONFIG_VP9_HIGHBITDEPTH + + if (t != EOB_TOKEN && t != ZERO_TOKEN) { + // Write extra bits first + const int e = p->extra; + const int l = b->len; + const int skip_bits = (t == CATEGORY6_TOKEN) ? TX_SIZES - 1 - tx_size : 0; + assert(tx_size < TX_SIZES); + uabs_write(ans, e & 1, 128); + if (l) { + const int v = e >> 1; + int n; + for (n = 0; n < l - skip_bits; ++n) { + const int bb = (v >> n) & 1; + uabs_write(ans, bb, b->prob[l - 1 - n]); + } + for (; n < l; ++n) { + assert(((v >> n) & 1) == 0); + } + } + + { + struct rans_sym s; + int j; + const vpx_prob *token_probs = + vp10_pareto8_token_probs[p->context_tree[PIVOT_NODE] - 1]; + s.cum_prob = 0; + for (j = ONE_TOKEN; j < t; ++j) { + s.cum_prob += token_probs[j - ONE_TOKEN]; + } + s.prob = token_probs[t - ONE_TOKEN]; + rans_write(ans, &s); + } + } + if (t != EOB_TOKEN) + uabs_write(ans, t != ZERO_TOKEN, p->context_tree[1]); + if (!p->skip_eob_node) + uabs_write(ans, t != EOB_TOKEN, p->context_tree[0]); + } + } +} +#endif // !CONFIG_ANS #if CONFIG_VAR_TX static void pack_txb_tokens(vpx_writer *w, @@ -973,6 +1039,11 @@ static void write_modes_b(VP10_COMP *cpi, const TileInfo *const tile, MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; MODE_INFO *m; int plane; +#if CONFIG_ANS + (void) tok; + (void) tok_end; + (void) plane; +#endif // !CONFIG_ANS xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col); m = xd->mi[0]; @@ -1008,6 +1079,7 @@ static void write_modes_b(VP10_COMP *cpi, const TileInfo *const tile, if (supertx_enabled) return; #endif // CONFIG_SUPERTX +#if !CONFIG_ANS if (!m->mbmi.skip) { assert(*tok < tok_end); for (plane = 0; plane < MAX_MB_PLANE; ++plane) { @@ -1054,6 +1126,7 @@ static void write_modes_b(VP10_COMP *cpi, const TileInfo *const tile, (*tok)++; } } +#endif } static void write_partition(const VP10_COMMON *const cm, @@ -1692,7 +1765,10 @@ static int get_refresh_mask(VP10_COMP *cpi) { static size_t encode_tiles(VP10_COMP *cpi, uint8_t *data_ptr, unsigned int *max_tile_sz) { VP10_COMMON *const cm = &cpi->common; - vpx_writer residual_bc; + vpx_writer mode_bc; +#if CONFIG_ANS + struct AnsCoder token_ans; +#endif int tile_row, tile_col; TOKENEXTRA *tok_end; size_t total_size = 0; @@ -1710,32 +1786,49 @@ static size_t encode_tiles(VP10_COMP *cpi, uint8_t *data_ptr, for (tile_row = 0; tile_row < tile_rows; tile_row++) { for (tile_col = 0; tile_col < tile_cols; tile_col++) { int tile_idx = tile_row * tile_cols + tile_col; + int put_tile_size = tile_col < tile_cols - 1 || tile_row < tile_rows - 1; + uint8_t *const mode_data_start = + data_ptr + total_size + (put_tile_size ? 4 : 0); + int token_section_size; TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col]; tok_end = cpi->tile_tok[tile_row][tile_col] + cpi->tok_count[tile_row][tile_col]; - if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) - vpx_start_encode(&residual_bc, data_ptr + total_size + 4); - else - vpx_start_encode(&residual_bc, data_ptr + total_size); + vpx_start_encode(&mode_bc, mode_data_start); +#if !CONFIG_ANS + (void) token_section_size; write_modes(cpi, &cpi->tile_data[tile_idx].tile_info, - &residual_bc, &tok, tok_end); + &mode_bc, &tok, tok_end); assert(tok == tok_end); - vpx_stop_encode(&residual_bc); - if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) { + vpx_stop_encode(&mode_bc); + if (put_tile_size) { unsigned int tile_sz; // size of this tile - assert(residual_bc.pos > 0); - tile_sz = residual_bc.pos - 1; + assert(mode_bc.pos > 0); + tile_sz = mode_bc.pos - 1; mem_put_le32(data_ptr + total_size, tile_sz); max_tile = max_tile > tile_sz ? max_tile : tile_sz; total_size += 4; } - - total_size += residual_bc.pos; + total_size += mode_bc.pos; +#else + write_modes(cpi, &cpi->tile_data[tile_idx].tile_info, &mode_bc, + NULL, NULL); + vpx_stop_encode(&mode_bc); + ans_write_init(&token_ans, mode_data_start + mode_bc.pos); + pack_mb_tokens_ans(&token_ans, tok, tok_end, cm->bit_depth); + token_section_size = ans_write_end(&token_ans); + if (put_tile_size) { + // size of this tile + mem_put_be32(data_ptr + total_size, + 4 + mode_bc.pos + token_section_size); + total_size += 4; + } + total_size += mode_bc.pos + token_section_size; +#endif // !CONFIG_ANS } } *max_tile_sz = max_tile; diff --git a/vp10/encoder/cost.c b/vp10/encoder/cost.c index aab826322..ded51d3a1 100644 --- a/vp10/encoder/cost.c +++ b/vp10/encoder/cost.c @@ -10,6 +10,7 @@ #include #include "vp10/encoder/cost.h" +#include "vp10/common/entropy.h" const unsigned int vp10_prob_cost[256] = { 2047, 2047, 1791, 1641, 1535, 1452, 1385, 1328, 1279, 1235, 1196, 1161, @@ -51,6 +52,22 @@ static void cost(int *costs, vpx_tree tree, const vpx_prob *probs, } } +#if CONFIG_ANS +void vp10_cost_tokens_ans(int *costs, const vpx_prob *tree_probs, + const vpx_prob *token_probs, int skip_eob) { + int c_tree = 0; // Cost of the "tree" nodes EOB and ZERO. + int i; + costs[EOB_TOKEN] = vp10_cost_bit(tree_probs[0], 0); + if (!skip_eob) + c_tree = vp10_cost_bit(tree_probs[0], 1); + costs[ZERO_TOKEN] = c_tree + vp10_cost_bit(tree_probs[1], 0); + c_tree += vp10_cost_bit(tree_probs[1], 1); + for (i = ONE_TOKEN; i <= CATEGORY6_TOKEN; ++i) { + costs[i] = c_tree + vp10_cost_bit(token_probs[i - ONE_TOKEN], 0); + } +} +#endif // CONFIG_ANS + void vp10_cost_tokens(int *costs, const vpx_prob *probs, vpx_tree tree) { cost(costs, tree, probs, 0, 0); } diff --git a/vp10/encoder/cost.h b/vp10/encoder/cost.h index b9619c6b1..551e4e536 100644 --- a/vp10/encoder/cost.h +++ b/vp10/encoder/cost.h @@ -48,6 +48,11 @@ static INLINE int treed_cost(vpx_tree tree, const vpx_prob *probs, void vp10_cost_tokens(int *costs, const vpx_prob *probs, vpx_tree tree); void vp10_cost_tokens_skip(int *costs, const vpx_prob *probs, vpx_tree tree); +#if CONFIG_ANS +void vp10_cost_tokens_ans(int *costs, const vpx_prob *tree_probs, + const vpx_prob *token_probs, int skip_eob); +#endif + #ifdef __cplusplus } // extern "C" #endif diff --git a/vp10/encoder/rd.c b/vp10/encoder/rd.c index a1fd00d53..991c713e9 100644 --- a/vp10/encoder/rd.c +++ b/vp10/encoder/rd.c @@ -136,12 +136,21 @@ static void fill_token_costs(vp10_coeff_cost *c, for (j = 0; j < REF_TYPES; ++j) for (k = 0; k < COEF_BANDS; ++k) for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { +#if CONFIG_ANS + const vpx_prob *const tree_probs = p[t][i][j][k][l]; + vpx_prob pivot = tree_probs[PIVOT_NODE]; + vp10_cost_tokens_ans((int *)c[t][i][j][k][0][l], tree_probs, + vp10_pareto8_token_probs[pivot - 1], 0); + vp10_cost_tokens_ans((int *)c[t][i][j][k][1][l], tree_probs, + vp10_pareto8_token_probs[pivot - 1], 1); +#else vpx_prob probs[ENTROPY_NODES]; vp10_model_to_full_probs(p[t][i][j][k][l], probs); vp10_cost_tokens((int *)c[t][i][j][k][0][l], probs, vp10_coef_tree); vp10_cost_tokens_skip((int *)c[t][i][j][k][1][l], probs, vp10_coef_tree); +#endif // CONFIG_ANS assert(c[t][i][j][k][0][l][EOB_TOKEN] == c[t][i][j][k][1][l][EOB_TOKEN]); } diff --git a/vp10/encoder/tokenize.c b/vp10/encoder/tokenize.c index 64211a9c9..d1b4785ec 100644 --- a/vp10/encoder/tokenize.c +++ b/vp10/encoder/tokenize.c @@ -431,11 +431,12 @@ const vp10_extra_bit vp10_extra_bits_high12[ENTROPY_TOKENS] = { }; #endif +#if !CONFIG_ANS const struct vp10_token vp10_coef_encodings[ENTROPY_TOKENS] = { {2, 2}, {6, 3}, {28, 5}, {58, 6}, {59, 6}, {60, 6}, {61, 6}, {124, 7}, {125, 7}, {126, 7}, {127, 7}, {0, 1} }; - +#endif // !CONFIG_ANS struct tokenize_b_args { VP10_COMP *cpi; @@ -783,6 +784,14 @@ void vp10_tokenize_sb(VP10_COMP *cpi, ThreadData *td, TOKENEXTRA **t, vp10_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b, &arg); (*t)->token = EOSB_TOKEN; +#if CONFIG_ANS + // TODO(aconverse): clip the number of bits in tokenize_b + // Smuggle TX_SIZE in the unused extrabits field so the ANS encoder + // knows the maximum number of extrabits to write at the end of the block + // (where it starts). + (*t)->extra = (EXTRABIT)(plane ? get_uv_tx_size(mbmi, &xd->plane[plane]) + : mbmi->tx_size); +#endif // CONFIG_ANS (*t)++; } } else { diff --git a/vp10/encoder/tokenize.h b/vp10/encoder/tokenize.h index c68e6f220..c03ec0239 100644 --- a/vp10/encoder/tokenize.h +++ b/vp10/encoder/tokenize.h @@ -43,7 +43,9 @@ typedef struct { extern const vpx_tree_index vp10_coef_tree[]; extern const vpx_tree_index vp10_coef_con_tree[]; +#if !CONFIG_ANS extern const struct vp10_token vp10_coef_encodings[]; +#endif // !CONFIG_ANS int vp10_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane); int vp10_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane); -- 2.40.0