From 95aacc4fb0ed4477a8c8b7cbf3f8c89229b03dc7 Mon Sep 17 00:00:00 2001 From: Jason Garrett-Glaser Date: Sat, 15 May 2010 14:48:58 -0700 Subject: [PATCH] Overhaul CABAC: faster, less cache usage Horribly munge up the CABAC tables to allow deduplication of some data. Saves 256 bytes of L1d cache in non-RD, 512 bytes in RD. Add asm versions of bypass and terminal; save L1i cache by re-using putbyte code. Further optimize encode_decision. All 3 primary CABAC functions fit in under 256 bytes of code total on x86_64. --- common/cabac.c | 185 +++++++++++++++++------------------------ common/cabac.h | 22 +++-- common/x86/cabac-a.asm | 76 +++++++++++++---- common/x86/x86inc.asm | 2 +- encoder/rdo.c | 2 + tools/checkasm.c | 46 ++++++++-- 6 files changed, 195 insertions(+), 138 deletions(-) diff --git a/common/cabac.c b/common/cabac.c index f50aef6f..11988a12 100644 --- a/common/cabac.c +++ b/common/cabac.c @@ -664,75 +664,44 @@ static const int8_t x264_cabac_context_init_PB[3][460][2] = } }; -/* FIXME could avoid this duplication by reversing the order of states - * with MPS=0, but that would uglify the other tables */ -const uint8_t x264_cabac_range_lps[128][4] = +const uint8_t x264_cabac_range_lps[64][4] = { - { 2, 2, 2, 2 }, - { 6, 7, 8, 9 }, { 6, 7, 9, 10 }, { 6, 8, 9, 11 }, - { 7, 8, 10, 11 }, { 7, 9, 10, 12 }, { 7, 9, 11, 12 }, - { 8, 9, 11, 13 }, { 8, 10, 12, 14 }, { 9, 11, 12, 14 }, - { 9, 11, 13, 15 }, { 10, 12, 14, 16 }, { 10, 12, 15, 17 }, - { 11, 13, 15, 18 }, { 11, 14, 16, 19 }, { 12, 14, 17, 20 }, - { 12, 15, 18, 21 }, { 13, 16, 19, 22 }, { 14, 17, 20, 23 }, - { 14, 18, 21, 24 }, { 15, 19, 22, 25 }, { 16, 20, 23, 27 }, - { 17, 21, 25, 28 }, { 18, 22, 26, 30 }, { 19, 23, 27, 31 }, - { 20, 24, 29, 33 }, { 21, 26, 30, 35 }, { 22, 27, 32, 37 }, - { 23, 28, 33, 39 }, { 24, 30, 35, 41 }, { 26, 31, 37, 43 }, - { 27, 33, 39, 45 }, { 29, 35, 41, 48 }, { 30, 37, 43, 50 }, - { 32, 39, 46, 53 }, { 33, 41, 48, 56 }, { 35, 43, 51, 59 }, - { 37, 45, 54, 62 }, { 39, 48, 56, 65 }, { 41, 50, 59, 69 }, - { 43, 53, 63, 72 }, { 46, 56, 66, 76 }, { 48, 59, 69, 80 }, - { 51, 62, 73, 85 }, { 53, 65, 77, 89 }, { 56, 69, 81, 94 }, - { 59, 72, 86, 99 }, { 62, 76, 90, 104 }, { 66, 80, 95, 110 }, - { 69, 85, 100, 116 }, { 73, 89, 105, 122 }, { 77, 94, 111, 128 }, - { 81, 99, 117, 135 }, { 85, 104, 123, 142 }, { 90, 110, 130, 150 }, - { 95, 116, 137, 158 }, { 100, 122, 144, 166 }, { 105, 128, 152, 175 }, - { 111, 135, 160, 185 }, { 116, 142, 169, 195 }, { 123, 150, 178, 205 }, - { 128, 158, 187, 216 }, { 128, 167, 197, 227 }, { 128, 176, 208, 240 }, - - { 128, 176, 208, 240 }, { 128, 167, 197, 227 }, { 128, 158, 187, 216 }, - { 123, 150, 178, 205 }, { 116, 142, 169, 195 }, { 111, 135, 160, 185 }, - { 105, 128, 152, 175 }, { 100, 122, 144, 166 }, { 95, 116, 137, 158 }, - { 90, 110, 130, 150 }, { 85, 104, 123, 142 }, { 81, 99, 117, 135 }, - { 77, 94, 111, 128 }, { 73, 89, 105, 122 }, { 69, 85, 100, 116 }, - { 66, 80, 95, 110 }, { 62, 76, 90, 104 }, { 59, 72, 86, 99 }, - { 56, 69, 81, 94 }, { 53, 65, 77, 89 }, { 51, 62, 73, 85 }, - { 48, 59, 69, 80 }, { 46, 56, 66, 76 }, { 43, 53, 63, 72 }, - { 41, 50, 59, 69 }, { 39, 48, 56, 65 }, { 37, 45, 54, 62 }, - { 35, 43, 51, 59 }, { 33, 41, 48, 56 }, { 32, 39, 46, 53 }, - { 30, 37, 43, 50 }, { 29, 35, 41, 48 }, { 27, 33, 39, 45 }, - { 26, 31, 37, 43 }, { 24, 30, 35, 41 }, { 23, 28, 33, 39 }, - { 22, 27, 32, 37 }, { 21, 26, 30, 35 }, { 20, 24, 29, 33 }, - { 19, 23, 27, 31 }, { 18, 22, 26, 30 }, { 17, 21, 25, 28 }, - { 16, 20, 23, 27 }, { 15, 19, 22, 25 }, { 14, 18, 21, 24 }, - { 14, 17, 20, 23 }, { 13, 16, 19, 22 }, { 12, 15, 18, 21 }, - { 12, 14, 17, 20 }, { 11, 14, 16, 19 }, { 11, 13, 15, 18 }, - { 10, 12, 15, 17 }, { 10, 12, 14, 16 }, { 9, 11, 13, 15 }, - { 9, 11, 12, 14 }, { 8, 10, 12, 14 }, { 8, 9, 11, 13 }, - { 7, 9, 11, 12 }, { 7, 9, 10, 12 }, { 7, 8, 10, 11 }, - { 6, 8, 9, 11 }, { 6, 7, 9, 10 }, { 6, 7, 8, 9 }, - { 2, 2, 2, 2 }, + { 2, 2, 2, 2}, { 6, 7, 8, 9}, { 6, 7, 9, 10}, { 6, 8, 9, 11}, + { 7, 8, 10, 11}, { 7, 9, 10, 12}, { 7, 9, 11, 12}, { 8, 9, 11, 13}, + { 8, 10, 12, 14}, { 9, 11, 12, 14}, { 9, 11, 13, 15}, { 10, 12, 14, 16}, + { 10, 12, 15, 17}, { 11, 13, 15, 18}, { 11, 14, 16, 19}, { 12, 14, 17, 20}, + { 12, 15, 18, 21}, { 13, 16, 19, 22}, { 14, 17, 20, 23}, { 14, 18, 21, 24}, + { 15, 19, 22, 25}, { 16, 20, 23, 27}, { 17, 21, 25, 28}, { 18, 22, 26, 30}, + { 19, 23, 27, 31}, { 20, 24, 29, 33}, { 21, 26, 30, 35}, { 22, 27, 32, 37}, + { 23, 28, 33, 39}, { 24, 30, 35, 41}, { 26, 31, 37, 43}, { 27, 33, 39, 45}, + { 29, 35, 41, 48}, { 30, 37, 43, 50}, { 32, 39, 46, 53}, { 33, 41, 48, 56}, + { 35, 43, 51, 59}, { 37, 45, 54, 62}, { 39, 48, 56, 65}, { 41, 50, 59, 69}, + { 43, 53, 63, 72}, { 46, 56, 66, 76}, { 48, 59, 69, 80}, { 51, 62, 73, 85}, + { 53, 65, 77, 89}, { 56, 69, 81, 94}, { 59, 72, 86, 99}, { 62, 76, 90, 104}, + { 66, 80, 95, 110}, { 69, 85, 100, 116}, { 73, 89, 105, 122}, { 77, 94, 111, 128}, + { 81, 99, 117, 135}, { 85, 104, 123, 142}, { 90, 110, 130, 150}, { 95, 116, 137, 158}, + {100, 122, 144, 166}, {105, 128, 152, 175}, {111, 135, 160, 185}, {116, 142, 169, 195}, + {123, 150, 178, 205}, {128, 158, 187, 216}, {128, 167, 197, 227}, {128, 176, 208, 240} }; const uint8_t x264_cabac_transition[128][2] = { - { 0, 0}, { 1, 25}, { 1, 25}, { 2, 26}, { 3, 26}, { 4, 26}, { 5, 27}, { 6, 27}, - { 7, 27}, { 8, 28}, { 9, 28}, { 10, 28}, { 11, 29}, { 12, 29}, { 13, 30}, { 14, 30}, - { 15, 30}, { 16, 31}, { 17, 31}, { 18, 32}, { 19, 33}, { 20, 33}, { 21, 33}, { 22, 34}, - { 23, 34}, { 24, 35}, { 25, 36}, { 26, 36}, { 27, 37}, { 28, 37}, { 29, 38}, { 30, 39}, - { 31, 39}, { 32, 40}, { 33, 41}, { 34, 41}, { 35, 42}, { 36, 42}, { 37, 44}, { 38, 44}, - { 39, 45}, { 40, 45}, { 41, 47}, { 42, 47}, { 43, 48}, { 44, 48}, { 45, 50}, { 46, 50}, - { 47, 51}, { 48, 52}, { 49, 52}, { 50, 54}, { 51, 54}, { 52, 55}, { 53, 56}, { 54, 57}, - { 55, 58}, { 56, 59}, { 57, 59}, { 58, 61}, { 59, 61}, { 60, 62}, { 61, 63}, { 62, 64}, - { 63, 65}, { 64, 66}, { 65, 67}, { 66, 68}, { 66, 69}, { 68, 70}, { 68, 71}, { 69, 72}, - { 70, 73}, { 71, 74}, { 72, 75}, { 73, 76}, { 73, 77}, { 75, 78}, { 75, 79}, { 76, 80}, - { 77, 81}, { 77, 82}, { 79, 83}, { 79, 84}, { 80, 85}, { 80, 86}, { 82, 87}, { 82, 88}, - { 83, 89}, { 83, 90}, { 85, 91}, { 85, 92}, { 86, 93}, { 86, 94}, { 87, 95}, { 88, 96}, - { 88, 97}, { 89, 98}, { 90, 99}, { 90,100}, { 91,101}, { 91,102}, { 92,103}, { 93,104}, - { 93,105}, { 94,106}, { 94,107}, { 94,108}, { 95,109}, { 96,110}, { 96,111}, { 97,112}, - { 97,113}, { 97,114}, { 98,115}, { 98,116}, { 99,117}, { 99,118}, { 99,119}, {100,120}, - {100,121}, {100,122}, {101,123}, {101,124}, {101,125}, {102,126}, {102,126}, {127,127}, + { 0, 0}, { 1, 1}, { 2, 50}, { 51, 3}, { 2, 50}, { 51, 3}, { 4, 52}, { 53, 5}, + { 6, 52}, { 53, 7}, { 8, 52}, { 53, 9}, { 10, 54}, { 55, 11}, { 12, 54}, { 55, 13}, + { 14, 54}, { 55, 15}, { 16, 56}, { 57, 17}, { 18, 56}, { 57, 19}, { 20, 56}, { 57, 21}, + { 22, 58}, { 59, 23}, { 24, 58}, { 59, 25}, { 26, 60}, { 61, 27}, { 28, 60}, { 61, 29}, + { 30, 60}, { 61, 31}, { 32, 62}, { 63, 33}, { 34, 62}, { 63, 35}, { 36, 64}, { 65, 37}, + { 38, 66}, { 67, 39}, { 40, 66}, { 67, 41}, { 42, 66}, { 67, 43}, { 44, 68}, { 69, 45}, + { 46, 68}, { 69, 47}, { 48, 70}, { 71, 49}, { 50, 72}, { 73, 51}, { 52, 72}, { 73, 53}, + { 54, 74}, { 75, 55}, { 56, 74}, { 75, 57}, { 58, 76}, { 77, 59}, { 60, 78}, { 79, 61}, + { 62, 78}, { 79, 63}, { 64, 80}, { 81, 65}, { 66, 82}, { 83, 67}, { 68, 82}, { 83, 69}, + { 70, 84}, { 85, 71}, { 72, 84}, { 85, 73}, { 74, 88}, { 89, 75}, { 76, 88}, { 89, 77}, + { 78, 90}, { 91, 79}, { 80, 90}, { 91, 81}, { 82, 94}, { 95, 83}, { 84, 94}, { 95, 85}, + { 86, 96}, { 97, 87}, { 88, 96}, { 97, 89}, { 90, 100}, {101, 91}, { 92, 100}, {101, 93}, + { 94, 102}, {103, 95}, { 96, 104}, {105, 97}, { 98, 104}, {105, 99}, {100, 108}, {109, 101}, + {102, 108}, {109, 103}, {104, 110}, {111, 105}, {106, 112}, {113, 107}, {108, 114}, {115, 109}, + {110, 116}, {117, 111}, {112, 118}, {119, 113}, {114, 118}, {119, 115}, {116, 122}, {123, 117}, + {118, 122}, {123, 119}, {120, 124}, {125, 121}, {122, 126}, {127, 123}, {124, 127}, {126, 125} }; const uint8_t x264_cabac_renorm_shift[64]= { @@ -743,41 +712,40 @@ const uint8_t x264_cabac_renorm_shift[64]= { }; /* -ln2(probability) */ -#define F(a,b) {FIX8(a),FIX8(b)} -const uint16_t x264_cabac_entropy[128][2] = +const uint16_t x264_cabac_entropy[128] = { - F(0.0273,5.7370), F(0.0288,5.6618), F(0.0303,5.5866), F(0.0320,5.5114), - F(0.0337,5.4362), F(0.0355,5.3610), F(0.0375,5.2859), F(0.0395,5.2106), - F(0.0416,5.1354), F(0.0439,5.0602), F(0.0463,4.9851), F(0.0488,4.9099), - F(0.0515,4.8347), F(0.0543,4.7595), F(0.0572,4.6843), F(0.0604,4.6091), - F(0.0637,4.5339), F(0.0671,4.4588), F(0.0708,4.3836), F(0.0747,4.3083), - F(0.0788,4.2332), F(0.0832,4.1580), F(0.0878,4.0828), F(0.0926,4.0076), - F(0.0977,3.9324), F(0.1032,3.8572), F(0.1089,3.7820), F(0.1149,3.7068), - F(0.1214,3.6316), F(0.1282,3.5565), F(0.1353,3.4813), F(0.1429,3.4061), - F(0.1510,3.3309), F(0.1596,3.2557), F(0.1686,3.1805), F(0.1782,3.1053), - F(0.1884,3.0301), F(0.1992,2.9549), F(0.2107,2.8797), F(0.2229,2.8046), - F(0.2358,2.7294), F(0.2496,2.6542), F(0.2642,2.5790), F(0.2798,2.5038), - F(0.2964,2.4286), F(0.3142,2.3534), F(0.3331,2.2782), F(0.3532,2.2030), - F(0.3748,2.1278), F(0.3979,2.0527), F(0.4226,1.9775), F(0.4491,1.9023), - F(0.4776,1.8271), F(0.5082,1.7519), F(0.5412,1.6767), F(0.5768,1.6015), - F(0.6152,1.5263), F(0.6568,1.4511), F(0.7020,1.3759), F(0.7513,1.3008), - F(0.8050,1.2256), F(0.8638,1.1504), F(0.9285,1.0752), F(1.0000,1.0000), - F(1.0000,1.0000), F(1.0752,0.9285), F(1.1504,0.8638), F(1.2256,0.8050), - F(1.3008,0.7513), F(1.3759,0.7020), F(1.4511,0.6568), F(1.5263,0.6152), - F(1.6015,0.5768), F(1.6767,0.5412), F(1.7519,0.5082), F(1.8271,0.4776), - F(1.9023,0.4491), F(1.9775,0.4226), F(2.0527,0.3979), F(2.1278,0.3748), - F(2.2030,0.3532), F(2.2782,0.3331), F(2.3534,0.3142), F(2.4286,0.2964), - F(2.5038,0.2798), F(2.5790,0.2642), F(2.6542,0.2496), F(2.7294,0.2358), - F(2.8046,0.2229), F(2.8797,0.2107), F(2.9549,0.1992), F(3.0301,0.1884), - F(3.1053,0.1782), F(3.1805,0.1686), F(3.2557,0.1596), F(3.3309,0.1510), - F(3.4061,0.1429), F(3.4813,0.1353), F(3.5565,0.1282), F(3.6316,0.1214), - F(3.7068,0.1149), F(3.7820,0.1089), F(3.8572,0.1032), F(3.9324,0.0977), - F(4.0076,0.0926), F(4.0828,0.0878), F(4.1580,0.0832), F(4.2332,0.0788), - F(4.3083,0.0747), F(4.3836,0.0708), F(4.4588,0.0671), F(4.5339,0.0637), - F(4.6091,0.0604), F(4.6843,0.0572), F(4.7595,0.0543), F(4.8347,0.0515), - F(4.9099,0.0488), F(4.9851,0.0463), F(5.0602,0.0439), F(5.1354,0.0416), - F(5.2106,0.0395), F(5.2859,0.0375), F(5.3610,0.0355), F(5.4362,0.0337), - F(5.5114,0.0320), F(5.5866,0.0303), F(5.6618,0.0288), F(5.7370,0.0273), + FIX8(0.0273), FIX8(5.7370), FIX8(0.0288), FIX8(5.6618), + FIX8(0.0303), FIX8(5.5866), FIX8(0.0320), FIX8(5.5114), + FIX8(0.0337), FIX8(5.4362), FIX8(0.0355), FIX8(5.3610), + FIX8(0.0375), FIX8(5.2859), FIX8(0.0395), FIX8(5.2106), + FIX8(0.0416), FIX8(5.1354), FIX8(0.0439), FIX8(5.0602), + FIX8(0.0463), FIX8(4.9851), FIX8(0.0488), FIX8(4.9099), + FIX8(0.0515), FIX8(4.8347), FIX8(0.0543), FIX8(4.7595), + FIX8(0.0572), FIX8(4.6843), FIX8(0.0604), FIX8(4.6091), + FIX8(0.0637), FIX8(4.5339), FIX8(0.0671), FIX8(4.4588), + FIX8(0.0708), FIX8(4.3836), FIX8(0.0747), FIX8(4.3083), + FIX8(0.0788), FIX8(4.2332), FIX8(0.0832), FIX8(4.1580), + FIX8(0.0878), FIX8(4.0828), FIX8(0.0926), FIX8(4.0076), + FIX8(0.0977), FIX8(3.9324), FIX8(0.1032), FIX8(3.8572), + FIX8(0.1089), FIX8(3.7820), FIX8(0.1149), FIX8(3.7068), + FIX8(0.1214), FIX8(3.6316), FIX8(0.1282), FIX8(3.5565), + FIX8(0.1353), FIX8(3.4813), FIX8(0.1429), FIX8(3.4061), + FIX8(0.1510), FIX8(3.3309), FIX8(0.1596), FIX8(3.2557), + FIX8(0.1686), FIX8(3.1805), FIX8(0.1782), FIX8(3.1053), + FIX8(0.1884), FIX8(3.0301), FIX8(0.1992), FIX8(2.9549), + FIX8(0.2107), FIX8(2.8797), FIX8(0.2229), FIX8(2.8046), + FIX8(0.2358), FIX8(2.7294), FIX8(0.2496), FIX8(2.6542), + FIX8(0.2642), FIX8(2.5790), FIX8(0.2798), FIX8(2.5038), + FIX8(0.2964), FIX8(2.4286), FIX8(0.3142), FIX8(2.3534), + FIX8(0.3331), FIX8(2.2782), FIX8(0.3532), FIX8(2.2030), + FIX8(0.3748), FIX8(2.1278), FIX8(0.3979), FIX8(2.0527), + FIX8(0.4226), FIX8(1.9775), FIX8(0.4491), FIX8(1.9023), + FIX8(0.4776), FIX8(1.8271), FIX8(0.5082), FIX8(1.7519), + FIX8(0.5412), FIX8(1.6767), FIX8(0.5768), FIX8(1.6015), + FIX8(0.6152), FIX8(1.5263), FIX8(0.6568), FIX8(1.4511), + FIX8(0.7020), FIX8(1.3759), FIX8(0.7513), FIX8(1.3008), + FIX8(0.8050), FIX8(1.2256), FIX8(0.8638), FIX8(1.1504), + FIX8(0.9285), FIX8(1.0752), FIX8(1.0000), FIX8(1.0000) }; @@ -794,14 +762,17 @@ void x264_cabac_context_init( x264_cabac_t *cb, int i_slice_type, int i_qp, int cabac_context_init = &x264_cabac_context_init_PB[i_model]; for( int i = 0; i < 460; i++ ) - cb->state[i] = x264_clip3( (((*cabac_context_init)[i][0] * i_qp) >> 4) + (*cabac_context_init)[i][1], 1, 126 ); + { + int state = x264_clip3( (((*cabac_context_init)[i][0] * i_qp) >> 4) + (*cabac_context_init)[i][1], 1, 126 ); + cb->state[i] = (X264_MIN( state, 127-state ) << 1) | (state >> 6); + } } void x264_cabac_encode_init( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end ) { cb->i_low = 0; cb->i_range = 0x01FE; - cb->i_queue = -1; // the first bit will be shifted away and not written + cb->i_queue = -9; // the first bit will be shifted away and not written cb->i_bytes_outstanding = 0; cb->p_start = p_data; cb->p = p_data; @@ -810,10 +781,10 @@ void x264_cabac_encode_init( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end ) static inline void x264_cabac_putbyte( x264_cabac_t *cb ) { - if( cb->i_queue >= 8 ) + if( cb->i_queue >= 0 ) { - int out = cb->i_low >> (cb->i_queue+2); - cb->i_low &= (4<i_queue)-1; + int out = cb->i_low >> (cb->i_queue+10); + cb->i_low &= (0x400<i_queue)-1; cb->i_queue -= 8; if( (out & 0xff) == 0xff ) @@ -855,9 +826,9 @@ static inline void x264_cabac_encode_renorm( x264_cabac_t *cb ) void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b ) { int i_state = cb->state[i_ctx]; - int i_range_lps = x264_cabac_range_lps[i_state][(cb->i_range>>6)-4]; + int i_range_lps = x264_cabac_range_lps[i_state>>1][(cb->i_range>>6)-4]; cb->i_range -= i_range_lps; - if( b != (i_state >> 6) ) + if( b != (i_state & 1) ) { cb->i_low += cb->i_range; cb->i_range = i_range_lps; @@ -866,7 +837,7 @@ void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b ) x264_cabac_encode_renorm( cb ); } -void x264_cabac_encode_bypass( x264_cabac_t *cb, int b ) +void x264_cabac_encode_bypass_c( x264_cabac_t *cb, int b ) { cb->i_low <<= 1; cb->i_low += -b & cb->i_range; @@ -892,7 +863,7 @@ void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val ) } while( k > 0 ); } -void x264_cabac_encode_terminal( x264_cabac_t *cb ) +void x264_cabac_encode_terminal_c( x264_cabac_t *cb ) { cb->i_range -= 2; x264_cabac_encode_renorm( cb ); diff --git a/common/cabac.h b/common/cabac.h index ef68fe65..9fc30073 100644 --- a/common/cabac.h +++ b/common/cabac.h @@ -31,7 +31,7 @@ typedef struct int i_range; /* bit stream */ - int i_queue; + int i_queue; //stored with an offset of -8 for faster asm int i_bytes_outstanding; uint8_t *p_start; @@ -46,7 +46,7 @@ typedef struct } x264_cabac_t; extern const uint8_t x264_cabac_transition[128][2]; -extern const uint16_t x264_cabac_entropy[128][2]; +extern const uint16_t x264_cabac_entropy[128]; /* init the contexts given i_slice_type, the quantif and the model */ void x264_cabac_context_init( x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model ); @@ -55,15 +55,21 @@ void x264_cabac_context_init( x264_cabac_t *cb, int i_slice_type, int i_qp, int void x264_cabac_encode_init ( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end ); void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b ); void x264_cabac_encode_decision_asm( x264_cabac_t *cb, int i_ctx, int b ); -void x264_cabac_encode_bypass( x264_cabac_t *cb, int b ); +void x264_cabac_encode_bypass_c( x264_cabac_t *cb, int b ); +void x264_cabac_encode_bypass_asm( x264_cabac_t *cb, int b ); +void x264_cabac_encode_terminal_c( x264_cabac_t *cb ); +void x264_cabac_encode_terminal_asm( x264_cabac_t *cb ); void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val ); -void x264_cabac_encode_terminal( x264_cabac_t *cb ); void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb ); #ifdef HAVE_MMX #define x264_cabac_encode_decision x264_cabac_encode_decision_asm +#define x264_cabac_encode_bypass x264_cabac_encode_bypass_asm +#define x264_cabac_encode_terminal x264_cabac_encode_terminal_asm #else #define x264_cabac_encode_decision x264_cabac_encode_decision_c +#define x264_cabac_encode_bypass x264_cabac_encode_bypass_c +#define x264_cabac_encode_terminal x264_cabac_encode_terminal_c #endif #define x264_cabac_encode_decision_noup x264_cabac_encode_decision @@ -78,25 +84,25 @@ static ALWAYS_INLINE void x264_cabac_size_decision( x264_cabac_t *cb, long i_ctx { int i_state = cb->state[i_ctx]; cb->state[i_ctx] = x264_cabac_transition[i_state][b]; - cb->f8_bits_encoded += x264_cabac_entropy[i_state][b]; + cb->f8_bits_encoded += x264_cabac_entropy[i_state^b]; } static ALWAYS_INLINE int x264_cabac_size_decision2( uint8_t *state, long b ) { int i_state = *state; *state = x264_cabac_transition[i_state][b]; - return x264_cabac_entropy[i_state][b]; + return x264_cabac_entropy[i_state^b]; } static ALWAYS_INLINE void x264_cabac_size_decision_noup( x264_cabac_t *cb, long i_ctx, long b ) { int i_state = cb->state[i_ctx]; - cb->f8_bits_encoded += x264_cabac_entropy[i_state][b]; + cb->f8_bits_encoded += x264_cabac_entropy[i_state^b]; } static ALWAYS_INLINE int x264_cabac_size_decision_noup2( uint8_t *state, long b ) { - return x264_cabac_entropy[*state][b]; + return x264_cabac_entropy[*state^b]; } #endif diff --git a/common/x86/cabac-a.asm b/common/x86/cabac-a.asm index 2af98c77..8621c5b7 100644 --- a/common/x86/cabac-a.asm +++ b/common/x86/cabac-a.asm @@ -32,13 +32,13 @@ cextern cabac_renorm_shift ; t3 must be ecx, since it's used for shift. %ifdef WIN64 - DECLARE_REG_TMP 3,1,2,0,4,5,6,10 + DECLARE_REG_TMP 3,1,2,0,4,5,6,10,2 %define pointer resq %elifdef ARCH_X86_64 - DECLARE_REG_TMP 0,1,2,3,4,5,6,10 + DECLARE_REG_TMP 0,1,2,3,4,5,6,10,6 %define pointer resq %else - DECLARE_REG_TMP 0,4,2,1,3,5,6,2 + DECLARE_REG_TMP 0,4,2,1,3,5,6,2,2 %define pointer resd %endif @@ -72,13 +72,15 @@ cglobal cabac_encode_decision_asm, 0,7 movifnidn t0, r0mp movifnidn t1d, r1m mov t5d, [t0+cb.range] - movzx t6d, byte [t0+cb.state+t1] + movzx t4d, byte [t0+cb.state+t1] mov t3d, t5d + mov t6d, t4d shr t5d, 6 + shr t4d, 1 movifnidn t2d, r2m - LOAD_GLOBAL t5d, cabac_range_lps-4, t5, t6*4 + LOAD_GLOBAL t5d, cabac_range_lps-4, t5, t4*4 LOAD_GLOBAL t4d, cabac_transition, t2, t6*2 - shr t6d, 6 + and t6d, 1 sub t3d, t5d cmp t6d, t2d mov t6d, [t0+cb.low] @@ -94,20 +96,66 @@ cglobal cabac_encode_decision_asm, 0,7 shl t6d, t3b add t3d, [t0+cb.queue] mov [t0+cb.range], t4d - cmp t3d, 8 - jl .update_queue_low -;cabac_putbyte + jge cabac_putbyte +.update_queue_low: + mov [t0+cb.low], t6d + mov [t0+cb.queue], t3d + RET + +cglobal cabac_encode_bypass_asm, 0,3 + movifnidn t0, r0mp + movifnidn t3d, r1m + neg t3d + mov t8d, [t0+cb.low] + and t3d, [t0+cb.range] + lea t8d, [t8*2+t3] + mov t3d, [t0+cb.queue] + inc t3d +%ifdef UNIX64 ; .putbyte compiles to nothing but a jmp + jge cabac_putbyte +%else + jge .putbyte +%endif + mov [t0+cb.low], t8d + mov [t0+cb.queue], t3d + RET +.putbyte: + PROLOGUE 0,7 + movifnidn t6d, t8d + jmp cabac_putbyte + +cglobal cabac_encode_terminal_asm, 0,3 + movifnidn t0, r0mp + sub dword [t0+cb.range], 2 +; shortcut: the renormalization shift in terminal +; can only be 0 or 1 and is zero over 99% of the time. + test dword [t0+cb.range], 0x100 + je .renorm + REP_RET +.renorm: + shl dword [t0+cb.low], 1 + shl dword [t0+cb.range], 1 + inc dword [t0+cb.queue] + jge .putbyte + REP_RET +.putbyte: + PROLOGUE 0,7 + mov t3d, [t0+cb.queue] + mov t6d, [t0+cb.low] + jmp cabac_putbyte + +cabac_putbyte: ; alive: t0=cb t3=queue t6=low %ifdef WIN64 DECLARE_REG_TMP 3,4,1,0,2,5,6,10 %endif mov t1d, -1 - add t3d, 2 + add t3d, 10 mov t2d, t6d shl t1d, t3b shr t2d, t3b ; out not t1d - sub t3d, 10 + sub t3d, 18 and t6d, t1d mov t5d, [t0+cb.bytes_outstanding] cmp t2b, 0xff ; FIXME is a 32bit op faster? @@ -125,8 +173,4 @@ cglobal cabac_encode_decision_asm, 0,7 .postpone: inc t5d mov [t0+cb.bytes_outstanding], t5d -.update_queue_low: - mov [t0+cb.low], t6d - mov [t0+cb.queue], t3d - RET - + jmp mangle(x264_cabac_encode_decision_asm.update_queue_low) diff --git a/common/x86/x86inc.asm b/common/x86/x86inc.asm index 9d23640a..f006f377 100644 --- a/common/x86/x86inc.asm +++ b/common/x86/x86inc.asm @@ -171,7 +171,7 @@ DECLARE_REG_SIZE bp, bpl %endrep %endmacro -DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7 +DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9 %ifdef ARCH_X86_64 %define gprsize 8 diff --git a/encoder/rdo.c b/encoder/rdo.c index 4d83b6a7..574a4848 100644 --- a/encoder/rdo.c +++ b/encoder/rdo.c @@ -50,6 +50,8 @@ static uint16_t cabac_size_5ones[128]; * fractional bits, but only finite precision. */ #undef x264_cabac_encode_decision #undef x264_cabac_encode_decision_noup +#undef x264_cabac_encode_bypass +#undef x264_cabac_encode_terminal #define x264_cabac_encode_decision(c,x,v) x264_cabac_size_decision(c,x,v) #define x264_cabac_encode_decision_noup(c,x,v) x264_cabac_size_decision_noup(c,x,v) #define x264_cabac_encode_terminal(c) ((c)->f8_bits_encoded += 7) diff --git a/tools/checkasm.c b/tools/checkasm.c index 2008d2f7..9bc15c81 100644 --- a/tools/checkasm.c +++ b/tools/checkasm.c @@ -1556,32 +1556,66 @@ static int check_intra( int cpu_ref, int cpu_new ) } #define DECL_CABAC(cpu) \ -static void run_cabac_##cpu( uint8_t *dst )\ +static void run_cabac_decision_##cpu( uint8_t *dst )\ {\ x264_cabac_t cb;\ x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\ x264_cabac_encode_init( &cb, dst, dst+0xff0 );\ for( int i = 0; i < 0x1000; i++ )\ x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\ +}\ +static void run_cabac_bypass_##cpu( uint8_t *dst )\ +{\ + x264_cabac_t cb;\ + x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\ + x264_cabac_encode_init( &cb, dst, dst+0xff0 );\ + for( int i = 0; i < 0x1000; i++ )\ + x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\ +}\ +static void run_cabac_terminal_##cpu( uint8_t *dst )\ +{\ + x264_cabac_t cb;\ + x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\ + x264_cabac_encode_init( &cb, dst, dst+0xff0 );\ + for( int i = 0; i < 0x1000; i++ )\ + x264_cabac_encode_terminal_##cpu( &cb );\ } DECL_CABAC(c) #ifdef HAVE_MMX DECL_CABAC(asm) #else -#define run_cabac_asm run_cabac_c +#define run_cabac_decision_asm run_cabac_decision_c +#define run_cabac_bypass_asm run_cabac_bypass_c +#define run_cabac_terminal_asm run_cabac_terminal_c #endif static int check_cabac( int cpu_ref, int cpu_new ) { int ret = 0, ok, used_asm = 1; - if( cpu_ref || run_cabac_c == run_cabac_asm) + if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm ) return 0; + set_func_name( "cabac_encode_decision" ); memcpy( buf4, buf3, 0x1000 ); - call_c( run_cabac_c, buf3 ); - call_a( run_cabac_asm, buf4 ); + call_c( run_cabac_decision_c, buf3 ); + call_a( run_cabac_decision_asm, buf4 ); + ok = !memcmp( buf3, buf4, 0x1000 ); + report( "cabac decision:" ); + + set_func_name( "cabac_encode_bypass" ); + memcpy( buf4, buf3, 0x1000 ); + call_c( run_cabac_bypass_c, buf3 ); + call_a( run_cabac_bypass_asm, buf4 ); ok = !memcmp( buf3, buf4, 0x1000 ); - report( "cabac :" ); + report( "cabac bypass:" ); + + set_func_name( "cabac_encode_terminal" ); + memcpy( buf4, buf3, 0x1000 ); + call_c( run_cabac_terminal_c, buf3 ); + call_a( run_cabac_terminal_asm, buf4 ); + ok = !memcmp( buf3, buf4, 0x1000 ); + report( "cabac terminal:" ); + return ret; } -- 2.47.3