| OLD | NEW |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
| 4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
| 5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 // | 9 // |
| 10 // Paginated token buffer | 10 // Paginated token buffer |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 b->left_ = b->page_size_; | 80 b->left_ = b->page_size_; |
| 81 b->tokens_ = (token_t*)TOKEN_DATA(page); | 81 b->tokens_ = (token_t*)TOKEN_DATA(page); |
| 82 return 1; | 82 return 1; |
| 83 } | 83 } |
| 84 | 84 |
| 85 //------------------------------------------------------------------------------ | 85 //------------------------------------------------------------------------------ |
| 86 | 86 |
| 87 #define TOKEN_ID(t, b, ctx) \ | 87 #define TOKEN_ID(t, b, ctx) \ |
| 88 (NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t)))) | 88 (NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t)))) |
| 89 | 89 |
| 90 static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, | 90 static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b, uint32_t bit, |
| 91 uint32_t bit, uint32_t proba_idx) { | 91 uint32_t proba_idx, |
| 92 proba_t* const stats) { |
| 92 assert(proba_idx < FIXED_PROBA_BIT); | 93 assert(proba_idx < FIXED_PROBA_BIT); |
| 93 assert(bit <= 1); | 94 assert(bit <= 1); |
| 94 if (b->left_ > 0 || TBufferNewPage(b)) { | 95 if (b->left_ > 0 || TBufferNewPage(b)) { |
| 95 const int slot = --b->left_; | 96 const int slot = --b->left_; |
| 96 b->tokens_[slot] = (bit << 15) | proba_idx; | 97 b->tokens_[slot] = (bit << 15) | proba_idx; |
| 97 } | 98 } |
| 99 VP8RecordStats(bit, stats); |
| 98 return bit; | 100 return bit; |
| 99 } | 101 } |
| 100 | 102 |
| 101 static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b, | 103 static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b, |
| 102 uint32_t bit, uint32_t proba) { | 104 uint32_t bit, uint32_t proba) { |
| 103 assert(proba < 256); | 105 assert(proba < 256); |
| 104 assert(bit <= 1); | 106 assert(bit <= 1); |
| 105 if (b->left_ > 0 || TBufferNewPage(b)) { | 107 if (b->left_ > 0 || TBufferNewPage(b)) { |
| 106 const int slot = --b->left_; | 108 const int slot = --b->left_; |
| 107 b->tokens_[slot] = (bit << 15) | FIXED_PROBA_BIT | proba; | 109 b->tokens_[slot] = (bit << 15) | FIXED_PROBA_BIT | proba; |
| 108 } | 110 } |
| 109 } | 111 } |
| 110 | 112 |
| 111 int VP8RecordCoeffTokens(const int ctx, const int coeff_type, | 113 int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res, |
| 112 int first, int last, | |
| 113 const int16_t* const coeffs, | |
| 114 VP8TBuffer* const tokens) { | 114 VP8TBuffer* const tokens) { |
| 115 int n = first; | 115 const int16_t* const coeffs = res->coeffs; |
| 116 const int coeff_type = res->coeff_type; |
| 117 const int last = res->last; |
| 118 int n = res->first; |
| 116 uint32_t base_id = TOKEN_ID(coeff_type, n, ctx); | 119 uint32_t base_id = TOKEN_ID(coeff_type, n, ctx); |
| 117 if (!AddToken(tokens, last >= 0, base_id + 0)) { | 120 // should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1 |
| 121 proba_t* s = res->stats[n][ctx]; |
| 122 if (!AddToken(tokens, last >= 0, base_id + 0, s + 0)) { |
| 118 return 0; | 123 return 0; |
| 119 } | 124 } |
| 120 | 125 |
| 121 while (n < 16) { | 126 while (n < 16) { |
| 122 const int c = coeffs[n++]; | 127 const int c = coeffs[n++]; |
| 123 const int sign = c < 0; | 128 const int sign = c < 0; |
| 124 const uint32_t v = sign ? -c : c; | 129 const uint32_t v = sign ? -c : c; |
| 125 if (!AddToken(tokens, v != 0, base_id + 1)) { | 130 if (!AddToken(tokens, v != 0, base_id + 1, s + 1)) { |
| 126 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0 | 131 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0); // ctx=0 |
| 132 s = res->stats[VP8EncBands[n]][0]; |
| 127 continue; | 133 continue; |
| 128 } | 134 } |
| 129 if (!AddToken(tokens, v > 1, base_id + 2)) { | 135 if (!AddToken(tokens, v > 1, base_id + 2, s + 2)) { |
| 130 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1 | 136 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1); // ctx=1 |
| 137 s = res->stats[VP8EncBands[n]][1]; |
| 131 } else { | 138 } else { |
| 132 if (!AddToken(tokens, v > 4, base_id + 3)) { | 139 if (!AddToken(tokens, v > 4, base_id + 3, s + 3)) { |
| 133 if (AddToken(tokens, v != 2, base_id + 4)) | 140 if (AddToken(tokens, v != 2, base_id + 4, s + 4)) |
| 134 AddToken(tokens, v == 4, base_id + 5); | 141 AddToken(tokens, v == 4, base_id + 5, s + 5); |
| 135 } else if (!AddToken(tokens, v > 10, base_id + 6)) { | 142 } else if (!AddToken(tokens, v > 10, base_id + 6, s + 6)) { |
| 136 if (!AddToken(tokens, v > 6, base_id + 7)) { | 143 if (!AddToken(tokens, v > 6, base_id + 7, s + 7)) { |
| 137 AddConstantToken(tokens, v == 6, 159); | 144 AddConstantToken(tokens, v == 6, 159); |
| 138 } else { | 145 } else { |
| 139 AddConstantToken(tokens, v >= 9, 165); | 146 AddConstantToken(tokens, v >= 9, 165); |
| 140 AddConstantToken(tokens, !(v & 1), 145); | 147 AddConstantToken(tokens, !(v & 1), 145); |
| 141 } | 148 } |
| 142 } else { | 149 } else { |
| 143 int mask; | 150 int mask; |
| 144 const uint8_t* tab; | 151 const uint8_t* tab; |
| 145 uint32_t residue = v - 3; | 152 uint32_t residue = v - 3; |
| 146 if (residue < (8 << 1)) { // VP8Cat3 (3b) | 153 if (residue < (8 << 1)) { // VP8Cat3 (3b) |
| 147 AddToken(tokens, 0, base_id + 8); | 154 AddToken(tokens, 0, base_id + 8, s + 8); |
| 148 AddToken(tokens, 0, base_id + 9); | 155 AddToken(tokens, 0, base_id + 9, s + 9); |
| 149 residue -= (8 << 0); | 156 residue -= (8 << 0); |
| 150 mask = 1 << 2; | 157 mask = 1 << 2; |
| 151 tab = VP8Cat3; | 158 tab = VP8Cat3; |
| 152 } else if (residue < (8 << 2)) { // VP8Cat4 (4b) | 159 } else if (residue < (8 << 2)) { // VP8Cat4 (4b) |
| 153 AddToken(tokens, 0, base_id + 8); | 160 AddToken(tokens, 0, base_id + 8, s + 8); |
| 154 AddToken(tokens, 1, base_id + 9); | 161 AddToken(tokens, 1, base_id + 9, s + 9); |
| 155 residue -= (8 << 1); | 162 residue -= (8 << 1); |
| 156 mask = 1 << 3; | 163 mask = 1 << 3; |
| 157 tab = VP8Cat4; | 164 tab = VP8Cat4; |
| 158 } else if (residue < (8 << 3)) { // VP8Cat5 (5b) | 165 } else if (residue < (8 << 3)) { // VP8Cat5 (5b) |
| 159 AddToken(tokens, 1, base_id + 8); | 166 AddToken(tokens, 1, base_id + 8, s + 8); |
| 160 AddToken(tokens, 0, base_id + 10); | 167 AddToken(tokens, 0, base_id + 10, s + 9); |
| 161 residue -= (8 << 2); | 168 residue -= (8 << 2); |
| 162 mask = 1 << 4; | 169 mask = 1 << 4; |
| 163 tab = VP8Cat5; | 170 tab = VP8Cat5; |
| 164 } else { // VP8Cat6 (11b) | 171 } else { // VP8Cat6 (11b) |
| 165 AddToken(tokens, 1, base_id + 8); | 172 AddToken(tokens, 1, base_id + 8, s + 8); |
| 166 AddToken(tokens, 1, base_id + 10); | 173 AddToken(tokens, 1, base_id + 10, s + 9); |
| 167 residue -= (8 << 3); | 174 residue -= (8 << 3); |
| 168 mask = 1 << 10; | 175 mask = 1 << 10; |
| 169 tab = VP8Cat6; | 176 tab = VP8Cat6; |
| 170 } | 177 } |
| 171 while (mask) { | 178 while (mask) { |
| 172 AddConstantToken(tokens, !!(residue & mask), *tab++); | 179 AddConstantToken(tokens, !!(residue & mask), *tab++); |
| 173 mask >>= 1; | 180 mask >>= 1; |
| 174 } | 181 } |
| 175 } | 182 } |
| 176 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2 | 183 base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2); // ctx=2 |
| 184 s = res->stats[VP8EncBands[n]][2]; |
| 177 } | 185 } |
| 178 AddConstantToken(tokens, sign, 128); | 186 AddConstantToken(tokens, sign, 128); |
| 179 if (n == 16 || !AddToken(tokens, n <= last, base_id + 0)) { | 187 if (n == 16 || !AddToken(tokens, n <= last, base_id + 0, s + 0)) { |
| 180 return 1; // EOB | 188 return 1; // EOB |
| 181 } | 189 } |
| 182 } | 190 } |
| 183 return 1; | 191 return 1; |
| 184 } | 192 } |
| 185 | 193 |
| 186 #undef TOKEN_ID | 194 #undef TOKEN_ID |
| 187 | 195 |
| 188 //------------------------------------------------------------------------------ | 196 //------------------------------------------------------------------------------ |
| 189 // This function works, but isn't currently used. Saved for later. | 197 // This function works, but isn't currently used. Saved for later. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 284 |
| 277 void VP8TBufferInit(VP8TBuffer* const b) { | 285 void VP8TBufferInit(VP8TBuffer* const b) { |
| 278 (void)b; | 286 (void)b; |
| 279 } | 287 } |
| 280 void VP8TBufferClear(VP8TBuffer* const b) { | 288 void VP8TBufferClear(VP8TBuffer* const b) { |
| 281 (void)b; | 289 (void)b; |
| 282 } | 290 } |
| 283 | 291 |
| 284 #endif // !DISABLE_TOKEN_BUFFER | 292 #endif // !DISABLE_TOKEN_BUFFER |
| 285 | 293 |
| OLD | NEW |