| OLD | NEW |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
| 4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
| 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
| 6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
| 7 // | 7 // |
| 8 // Author: Jyrki Alakuijala (jyrki@google.com) | 8 // Author: Jyrki Alakuijala (jyrki@google.com) |
| 9 // | 9 // |
| 10 #ifdef HAVE_CONFIG_H | 10 #ifdef HAVE_CONFIG_H |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits) { | 49 void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits) { |
| 50 p->palette_code_bits_ = palette_code_bits; | 50 p->palette_code_bits_ = palette_code_bits; |
| 51 HistogramClear(p); | 51 HistogramClear(p); |
| 52 } | 52 } |
| 53 | 53 |
| 54 VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { | 54 VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { |
| 55 int i; | 55 int i; |
| 56 VP8LHistogramSet* set; | 56 VP8LHistogramSet* set; |
| 57 VP8LHistogram* bulk; | 57 VP8LHistogram* bulk; |
| 58 const uint64_t total_size = (uint64_t)sizeof(*set) | 58 const uint64_t total_size = sizeof(*set) |
| 59 + size * sizeof(*set->histograms) | 59 + (uint64_t)size * sizeof(*set->histograms) |
| 60 + size * sizeof(**set->histograms); | 60 + (uint64_t)size * sizeof(**set->histograms); |
| 61 uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); | 61 uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); |
| 62 if (memory == NULL) return NULL; | 62 if (memory == NULL) return NULL; |
| 63 | 63 |
| 64 set = (VP8LHistogramSet*)memory; | 64 set = (VP8LHistogramSet*)memory; |
| 65 memory += sizeof(*set); | 65 memory += sizeof(*set); |
| 66 set->histograms = (VP8LHistogram**)memory; | 66 set->histograms = (VP8LHistogram**)memory; |
| 67 memory += size * sizeof(*set->histograms); | 67 memory += size * sizeof(*set->histograms); |
| 68 bulk = (VP8LHistogram*)memory; | 68 bulk = (VP8LHistogram*)memory; |
| 69 set->max_size = size; | 69 set->max_size = size; |
| 70 set->size = size; | 70 set->size = size; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 91 int code, extra_bits_count, extra_bits_value; | 91 int code, extra_bits_count, extra_bits_value; |
| 92 PrefixEncode(PixOrCopyLength(v), | 92 PrefixEncode(PixOrCopyLength(v), |
| 93 &code, &extra_bits_count, &extra_bits_value); | 93 &code, &extra_bits_count, &extra_bits_value); |
| 94 ++histo->literal_[256 + code]; | 94 ++histo->literal_[256 + code]; |
| 95 PrefixEncode(PixOrCopyDistance(v), | 95 PrefixEncode(PixOrCopyDistance(v), |
| 96 &code, &extra_bits_count, &extra_bits_value); | 96 &code, &extra_bits_count, &extra_bits_value); |
| 97 ++histo->distance_[code]; | 97 ++histo->distance_[code]; |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 | |
| 102 | |
| 103 static double BitsEntropy(const int* const array, int n) { | 101 static double BitsEntropy(const int* const array, int n) { |
| 104 double retval = 0.; | 102 double retval = 0.; |
| 105 int sum = 0; | 103 int sum = 0; |
| 106 int nonzeros = 0; | 104 int nonzeros = 0; |
| 107 int max_val = 0; | 105 int max_val = 0; |
| 108 int i; | 106 int i; |
| 109 double mix; | 107 double mix; |
| 110 for (i = 0; i < n; ++i) { | 108 for (i = 0; i < n; ++i) { |
| 111 if (array[i] != 0) { | 109 if (array[i] != 0) { |
| 112 sum += array[i]; | 110 sum += array[i]; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 142 mix = 0.627; | 140 mix = 0.627; |
| 143 } | 141 } |
| 144 | 142 |
| 145 { | 143 { |
| 146 double min_limit = 2 * sum - max_val; | 144 double min_limit = 2 * sum - max_val; |
| 147 min_limit = mix * min_limit + (1.0 - mix) * retval; | 145 min_limit = mix * min_limit + (1.0 - mix) * retval; |
| 148 return (retval < min_limit) ? min_limit : retval; | 146 return (retval < min_limit) ? min_limit : retval; |
| 149 } | 147 } |
| 150 } | 148 } |
| 151 | 149 |
| 152 double VP8LHistogramEstimateBitsBulk(const VP8LHistogram* const p) { | |
| 153 double retval = BitsEntropy(&p->literal_[0], VP8LHistogramNumCodes(p)) | |
| 154 + BitsEntropy(&p->red_[0], 256) | |
| 155 + BitsEntropy(&p->blue_[0], 256) | |
| 156 + BitsEntropy(&p->alpha_[0], 256) | |
| 157 + BitsEntropy(&p->distance_[0], NUM_DISTANCE_CODES); | |
| 158 // Compute the extra bits cost. | |
| 159 int i; | |
| 160 for (i = 2; i < NUM_LENGTH_CODES - 2; ++i) { | |
| 161 retval += | |
| 162 (i >> 1) * p->literal_[256 + i + 2]; | |
| 163 } | |
| 164 for (i = 2; i < NUM_DISTANCE_CODES - 2; ++i) { | |
| 165 retval += (i >> 1) * p->distance_[i + 2]; | |
| 166 } | |
| 167 return retval; | |
| 168 } | |
| 169 | |
| 170 | |
| 171 // Returns the cost encode the rle-encoded entropy code. | 150 // Returns the cost encode the rle-encoded entropy code. |
| 172 // The constants in this function are experimental. | 151 // The constants in this function are experimental. |
| 173 static double HuffmanCost(const int* const population, int length) { | 152 static double HuffmanCost(const int* const population, int length) { |
| 174 // Small bias because Huffman code length is typically not stored in | 153 // Small bias because Huffman code length is typically not stored in |
| 175 // full length. | 154 // full length. |
| 176 static const int kHuffmanCodeOfHuffmanCodeSize = CODE_LENGTH_CODES * 3; | 155 static const int kHuffmanCodeOfHuffmanCodeSize = CODE_LENGTH_CODES * 3; |
| 177 static const double kSmallBias = 9.1; | 156 static const double kSmallBias = 9.1; |
| 178 double retval = kHuffmanCodeOfHuffmanCodeSize - kSmallBias; | 157 double retval = kHuffmanCodeOfHuffmanCodeSize - kSmallBias; |
| 179 int streak = 0; | 158 int streak = 0; |
| 180 int i = 0; | 159 int i = 0; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 200 } | 179 } |
| 201 streak = 0; | 180 streak = 0; |
| 202 } | 181 } |
| 203 if (i == length - 1) { | 182 if (i == length - 1) { |
| 204 ++streak; | 183 ++streak; |
| 205 goto last_streak_hack; | 184 goto last_streak_hack; |
| 206 } | 185 } |
| 207 return retval; | 186 return retval; |
| 208 } | 187 } |
| 209 | 188 |
| 210 // Estimates the Huffman dictionary + other block overhead size. | 189 static double PopulationCost(const int* const population, int length) { |
| 211 static double HistogramEstimateBitsHeader(const VP8LHistogram* const p) { | 190 return BitsEntropy(population, length) + HuffmanCost(population, length); |
| 212 return HuffmanCost(&p->alpha_[0], 256) + | |
| 213 HuffmanCost(&p->red_[0], 256) + | |
| 214 HuffmanCost(&p->literal_[0], VP8LHistogramNumCodes(p)) + | |
| 215 HuffmanCost(&p->blue_[0], 256) + | |
| 216 HuffmanCost(&p->distance_[0], NUM_DISTANCE_CODES); | |
| 217 } | 191 } |
| 218 | 192 |
| 193 static double ExtraCost(const int* const population, int length) { |
| 194 int i; |
| 195 double cost = 0.; |
| 196 for (i = 2; i < length - 2; ++i) cost += (i >> 1) * population[i + 2]; |
| 197 return cost; |
| 198 } |
| 199 |
| 200 // Estimates the Entropy + Huffman + other block overhead size cost. |
| 219 double VP8LHistogramEstimateBits(const VP8LHistogram* const p) { | 201 double VP8LHistogramEstimateBits(const VP8LHistogram* const p) { |
| 220 return HistogramEstimateBitsHeader(p) + VP8LHistogramEstimateBitsBulk(p); | 202 return PopulationCost(p->literal_, VP8LHistogramNumCodes(p)) |
| 203 + PopulationCost(p->red_, 256) |
| 204 + PopulationCost(p->blue_, 256) |
| 205 + PopulationCost(p->alpha_, 256) |
| 206 + PopulationCost(p->distance_, NUM_DISTANCE_CODES) |
| 207 + ExtraCost(p->literal_ + 256, NUM_LENGTH_CODES) |
| 208 + ExtraCost(p->distance_, NUM_DISTANCE_CODES); |
| 221 } | 209 } |
| 222 | 210 |
| 211 double VP8LHistogramEstimateBitsBulk(const VP8LHistogram* const p) { |
| 212 return BitsEntropy(p->literal_, VP8LHistogramNumCodes(p)) |
| 213 + BitsEntropy(p->red_, 256) |
| 214 + BitsEntropy(p->blue_, 256) |
| 215 + BitsEntropy(p->alpha_, 256) |
| 216 + BitsEntropy(p->distance_, NUM_DISTANCE_CODES) |
| 217 + ExtraCost(p->literal_ + 256, NUM_LENGTH_CODES) |
| 218 + ExtraCost(p->distance_, NUM_DISTANCE_CODES); |
| 219 } |
| 220 |
| 221 // ----------------------------------------------------------------------------- |
| 222 // Various histogram combine/cost-eval functions |
| 223 |
| 224 // Adds 'in' histogram to 'out' |
| 225 static void HistogramAdd(const VP8LHistogram* const in, |
| 226 VP8LHistogram* const out) { |
| 227 int i; |
| 228 for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) { |
| 229 out->literal_[i] += in->literal_[i]; |
| 230 } |
| 231 for (i = 0; i < NUM_DISTANCE_CODES; ++i) { |
| 232 out->distance_[i] += in->distance_[i]; |
| 233 } |
| 234 for (i = 0; i < 256; ++i) { |
| 235 out->red_[i] += in->red_[i]; |
| 236 out->blue_[i] += in->blue_[i]; |
| 237 out->alpha_[i] += in->alpha_[i]; |
| 238 } |
| 239 } |
| 240 |
| 241 // Performs out = a + b, computing the cost C(a+b) - C(a) - C(b) while comparing |
| 242 // to the threshold value 'cost_threshold'. The score returned is |
| 243 // Score = C(a+b) - C(a) - C(b), where C(a) + C(b) is known and fixed. |
| 244 // Since the previous score passed is 'cost_threshold', we only need to compare |
| 245 // the partial cost against 'cost_threshold + C(a) + C(b)' to possibly bail-out |
| 246 // early. |
| 247 static double HistogramAddEval(const VP8LHistogram* const a, |
| 248 const VP8LHistogram* const b, |
| 249 VP8LHistogram* const out, |
| 250 double cost_threshold) { |
| 251 double cost = 0; |
| 252 const double sum_cost = a->bit_cost_ + b->bit_cost_; |
| 253 int i; |
| 254 |
| 255 cost_threshold += sum_cost; |
| 256 |
| 257 // palette_code_bits_ is part of the cost evaluation for literal_. |
| 258 // TODO(skal): remove/simplify this palette_code_bits_? |
| 259 out->palette_code_bits_ = |
| 260 (a->palette_code_bits_ > b->palette_code_bits_) ? a->palette_code_bits_ : |
| 261 b->palette_code_bits_; |
| 262 for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) { |
| 263 out->literal_[i] = a->literal_[i] + b->literal_[i]; |
| 264 } |
| 265 cost += PopulationCost(out->literal_, VP8LHistogramNumCodes(out)); |
| 266 cost += ExtraCost(out->literal_ + 256, NUM_LENGTH_CODES); |
| 267 if (cost > cost_threshold) return cost; |
| 268 |
| 269 for (i = 0; i < 256; ++i) out->red_[i] = a->red_[i] + b->red_[i]; |
| 270 cost += PopulationCost(out->red_, 256); |
| 271 if (cost > cost_threshold) return cost; |
| 272 |
| 273 for (i = 0; i < 256; ++i) out->blue_[i] = a->blue_[i] + b->blue_[i]; |
| 274 cost += PopulationCost(out->blue_, 256); |
| 275 if (cost > cost_threshold) return cost; |
| 276 |
| 277 for (i = 0; i < NUM_DISTANCE_CODES; ++i) { |
| 278 out->distance_[i] = a->distance_[i] + b->distance_[i]; |
| 279 } |
| 280 cost += PopulationCost(out->distance_, NUM_DISTANCE_CODES); |
| 281 cost += ExtraCost(out->distance_, NUM_DISTANCE_CODES); |
| 282 if (cost > cost_threshold) return cost; |
| 283 |
| 284 for (i = 0; i < 256; ++i) out->alpha_[i] = a->alpha_[i] + b->alpha_[i]; |
| 285 cost += PopulationCost(out->alpha_, 256); |
| 286 |
| 287 out->bit_cost_ = cost; |
| 288 return cost - sum_cost; |
| 289 } |
| 290 |
| 291 // Same as HistogramAddEval(), except that the resulting histogram |
| 292 // is not stored. Only the cost C(a+b) - C(a) is evaluated. We omit |
| 293 // the term C(b) which is constant over all the evaluations. |
| 294 static double HistogramAddThresh(const VP8LHistogram* const a, |
| 295 const VP8LHistogram* const b, |
| 296 double cost_threshold) { |
| 297 int tmp[PIX_OR_COPY_CODES_MAX]; // <= max storage we'll need |
| 298 int i; |
| 299 double cost = -a->bit_cost_; |
| 300 |
| 301 for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) { |
| 302 tmp[i] = a->literal_[i] + b->literal_[i]; |
| 303 } |
| 304 // note that the tests are ordered so that the usually largest |
| 305 // cost shares come first. |
| 306 cost += PopulationCost(tmp, VP8LHistogramNumCodes(a)); |
| 307 cost += ExtraCost(tmp + 256, NUM_LENGTH_CODES); |
| 308 if (cost > cost_threshold) return cost; |
| 309 |
| 310 for (i = 0; i < 256; ++i) tmp[i] = a->red_[i] + b->red_[i]; |
| 311 cost += PopulationCost(tmp, 256); |
| 312 if (cost > cost_threshold) return cost; |
| 313 |
| 314 for (i = 0; i < 256; ++i) tmp[i] = a->blue_[i] + b->blue_[i]; |
| 315 cost += PopulationCost(tmp, 256); |
| 316 if (cost > cost_threshold) return cost; |
| 317 |
| 318 for (i = 0; i < NUM_DISTANCE_CODES; ++i) { |
| 319 tmp[i] = a->distance_[i] + b->distance_[i]; |
| 320 } |
| 321 cost += PopulationCost(tmp, NUM_DISTANCE_CODES); |
| 322 cost += ExtraCost(tmp, NUM_DISTANCE_CODES); |
| 323 if (cost > cost_threshold) return cost; |
| 324 |
| 325 for (i = 0; i < 256; ++i) tmp[i] = a->alpha_[i] + b->alpha_[i]; |
| 326 cost += PopulationCost(tmp, 256); |
| 327 |
| 328 return cost; |
| 329 } |
| 330 |
| 331 // ----------------------------------------------------------------------------- |
| 332 |
| 223 static void HistogramBuildImage(int xsize, int histo_bits, | 333 static void HistogramBuildImage(int xsize, int histo_bits, |
| 224 const VP8LBackwardRefs* const backward_refs, | 334 const VP8LBackwardRefs* const backward_refs, |
| 225 VP8LHistogramSet* const image) { | 335 VP8LHistogramSet* const image) { |
| 226 int i; | 336 int i; |
| 227 int x = 0, y = 0; | 337 int x = 0, y = 0; |
| 228 const int histo_xsize = VP8LSubSampleSize(xsize, histo_bits); | 338 const int histo_xsize = VP8LSubSampleSize(xsize, histo_bits); |
| 229 VP8LHistogram** const histograms = image->histograms; | 339 VP8LHistogram** const histograms = image->histograms; |
| 230 assert(histo_bits > 0); | 340 assert(histo_bits > 0); |
| 231 for (i = 0; i < backward_refs->size; ++i) { | 341 for (i = 0; i < backward_refs->size; ++i) { |
| 232 const PixOrCopy* const v = &backward_refs->refs[i]; | 342 const PixOrCopy* const v = &backward_refs->refs[i]; |
| 233 const int ix = (y >> histo_bits) * histo_xsize + (x >> histo_bits); | 343 const int ix = (y >> histo_bits) * histo_xsize + (x >> histo_bits); |
| 234 VP8LHistogramAddSinglePixOrCopy(histograms[ix], v); | 344 VP8LHistogramAddSinglePixOrCopy(histograms[ix], v); |
| 235 x += PixOrCopyLength(v); | 345 x += PixOrCopyLength(v); |
| 236 while (x >= xsize) { | 346 while (x >= xsize) { |
| 237 x -= xsize; | 347 x -= xsize; |
| 238 ++y; | 348 ++y; |
| 239 } | 349 } |
| 240 } | 350 } |
| 241 } | 351 } |
| 242 | 352 |
| 243 static uint32_t MyRand(uint32_t *seed) { | 353 static uint32_t MyRand(uint32_t *seed) { |
| 244 *seed *= 16807U; | 354 *seed *= 16807U; |
| 245 if (*seed == 0) { | 355 if (*seed == 0) { |
| 246 *seed = 1; | 356 *seed = 1; |
| 247 } | 357 } |
| 248 return *seed; | 358 return *seed; |
| 249 } | 359 } |
| 250 | 360 |
| 251 static int HistogramCombine(const VP8LHistogramSet* const in, | 361 static int HistogramCombine(const VP8LHistogramSet* const in, |
| 252 VP8LHistogramSet* const out, int num_pairs) { | 362 VP8LHistogramSet* const out, int iter_mult, |
| 363 int num_pairs, int num_tries_no_success) { |
| 253 int ok = 0; | 364 int ok = 0; |
| 254 int i, iter; | 365 int i, iter; |
| 255 uint32_t seed = 0; | 366 uint32_t seed = 0; |
| 256 int tries_with_no_success = 0; | 367 int tries_with_no_success = 0; |
| 368 int out_size = in->size; |
| 369 const int outer_iters = in->size * iter_mult; |
| 257 const int min_cluster_size = 2; | 370 const int min_cluster_size = 2; |
| 258 int out_size = in->size; | |
| 259 const int outer_iters = in->size * 3; | |
| 260 VP8LHistogram* const histos = (VP8LHistogram*)malloc(2 * sizeof(*histos)); | 371 VP8LHistogram* const histos = (VP8LHistogram*)malloc(2 * sizeof(*histos)); |
| 261 VP8LHistogram* cur_combo = histos + 0; // trial merged histogram | 372 VP8LHistogram* cur_combo = histos + 0; // trial merged histogram |
| 262 VP8LHistogram* best_combo = histos + 1; // best merged histogram so far | 373 VP8LHistogram* best_combo = histos + 1; // best merged histogram so far |
| 263 if (histos == NULL) goto End; | 374 if (histos == NULL) goto End; |
| 264 | 375 |
| 265 // Copy histograms from in[] to out[]. | 376 // Copy histograms from in[] to out[]. |
| 266 assert(in->size <= out->size); | 377 assert(in->size <= out->size); |
| 267 for (i = 0; i < in->size; ++i) { | 378 for (i = 0; i < in->size; ++i) { |
| 268 in->histograms[i]->bit_cost_ = VP8LHistogramEstimateBits(in->histograms[i]); | 379 in->histograms[i]->bit_cost_ = VP8LHistogramEstimateBits(in->histograms[i]); |
| 269 *out->histograms[i] = *in->histograms[i]; | 380 *out->histograms[i] = *in->histograms[i]; |
| 270 } | 381 } |
| 271 | 382 |
| 272 // Collapse similar histograms in 'out'. | 383 // Collapse similar histograms in 'out'. |
| 273 for (iter = 0; iter < outer_iters && out_size >= min_cluster_size; ++iter) { | 384 for (iter = 0; iter < outer_iters && out_size >= min_cluster_size; ++iter) { |
| 274 // We pick the best pair to be combined out of 'inner_iters' pairs. | |
| 275 double best_cost_diff = 0.; | 385 double best_cost_diff = 0.; |
| 276 int best_idx1 = 0, best_idx2 = 1; | 386 int best_idx1 = -1, best_idx2 = 1; |
| 277 int j; | 387 int j; |
| 388 const int num_tries = (num_pairs < out_size) ? num_pairs : out_size; |
| 278 seed += iter; | 389 seed += iter; |
| 279 for (j = 0; j < num_pairs; ++j) { | 390 for (j = 0; j < num_tries; ++j) { |
| 280 double curr_cost_diff; | 391 double curr_cost_diff; |
| 281 // Choose two histograms at random and try to combine them. | 392 // Choose two histograms at random and try to combine them. |
| 282 const uint32_t idx1 = MyRand(&seed) % out_size; | 393 const uint32_t idx1 = MyRand(&seed) % out_size; |
| 283 const uint32_t tmp = ((j & 7) + 1) % (out_size - 1); | 394 const uint32_t tmp = (j & 7) + 1; |
| 284 const uint32_t diff = (tmp < 3) ? tmp : MyRand(&seed) % (out_size - 1); | 395 const uint32_t diff = (tmp < 3) ? tmp : MyRand(&seed) % (out_size - 1); |
| 285 const uint32_t idx2 = (idx1 + diff + 1) % out_size; | 396 const uint32_t idx2 = (idx1 + diff + 1) % out_size; |
| 286 if (idx1 == idx2) { | 397 if (idx1 == idx2) { |
| 287 continue; | 398 continue; |
| 288 } | 399 } |
| 289 *cur_combo = *out->histograms[idx1]; | |
| 290 VP8LHistogramAdd(cur_combo, out->histograms[idx2]); | |
| 291 cur_combo->bit_cost_ = VP8LHistogramEstimateBits(cur_combo); | |
| 292 // Calculate cost reduction on combining. | 400 // Calculate cost reduction on combining. |
| 293 curr_cost_diff = cur_combo->bit_cost_ | 401 curr_cost_diff = HistogramAddEval(out->histograms[idx1], |
| 294 - out->histograms[idx1]->bit_cost_ | 402 out->histograms[idx2], |
| 295 - out->histograms[idx2]->bit_cost_; | 403 cur_combo, best_cost_diff); |
| 296 if (best_cost_diff > curr_cost_diff) { // found a better pair? | 404 if (curr_cost_diff < best_cost_diff) { // found a better pair? |
| 297 { // swap cur/best combo histograms | 405 { // swap cur/best combo histograms |
| 298 VP8LHistogram* const tmp_histo = cur_combo; | 406 VP8LHistogram* const tmp_histo = cur_combo; |
| 299 cur_combo = best_combo; | 407 cur_combo = best_combo; |
| 300 best_combo = tmp_histo; | 408 best_combo = tmp_histo; |
| 301 } | 409 } |
| 302 best_cost_diff = curr_cost_diff; | 410 best_cost_diff = curr_cost_diff; |
| 303 best_idx1 = idx1; | 411 best_idx1 = idx1; |
| 304 best_idx2 = idx2; | 412 best_idx2 = idx2; |
| 305 } | 413 } |
| 306 } | 414 } |
| 307 | 415 |
| 308 if (best_cost_diff < 0.0) { | 416 if (best_idx1 >= 0) { |
| 309 *out->histograms[best_idx1] = *best_combo; | 417 *out->histograms[best_idx1] = *best_combo; |
| 310 // swap best_idx2 slot with last one (which is now unused) | 418 // swap best_idx2 slot with last one (which is now unused) |
| 311 --out_size; | 419 --out_size; |
| 312 if (best_idx2 != out_size) { | 420 if (best_idx2 != out_size) { |
| 313 out->histograms[best_idx2] = out->histograms[out_size]; | 421 out->histograms[best_idx2] = out->histograms[out_size]; |
| 314 out->histograms[out_size] = NULL; // just for sanity check. | 422 out->histograms[out_size] = NULL; // just for sanity check. |
| 315 } | 423 } |
| 316 tries_with_no_success = 0; | 424 tries_with_no_success = 0; |
| 317 } | 425 } |
| 318 if (++tries_with_no_success >= 50) { | 426 if (++tries_with_no_success >= num_tries_no_success) { |
| 319 break; | 427 break; |
| 320 } | 428 } |
| 321 } | 429 } |
| 322 out->size = out_size; | 430 out->size = out_size; |
| 323 ok = 1; | 431 ok = 1; |
| 324 | 432 |
| 325 End: | 433 End: |
| 326 free(histos); | 434 free(histos); |
| 327 return ok; | 435 return ok; |
| 328 } | 436 } |
| 329 | 437 |
| 330 // ----------------------------------------------------------------------------- | 438 // ----------------------------------------------------------------------------- |
| 331 // Histogram refinement | 439 // Histogram refinement |
| 332 | 440 |
| 333 // What is the bit cost of moving square_histogram from | 441 // What is the bit cost of moving square_histogram from cur_symbol to candidate. |
| 334 // cur_symbol to candidate_symbol. | |
| 335 // TODO(skal): we don't really need to copy the histogram and Add(). Instead | |
| 336 // we just need VP8LDualHistogramEstimateBits(A, B) estimation function. | |
| 337 static double HistogramDistance(const VP8LHistogram* const square_histogram, | 442 static double HistogramDistance(const VP8LHistogram* const square_histogram, |
| 338 const VP8LHistogram* const candidate) { | 443 const VP8LHistogram* const candidate, |
| 339 const double previous_bit_cost = candidate->bit_cost_; | 444 double cost_threshold) { |
| 340 double new_bit_cost; | 445 return HistogramAddThresh(candidate, square_histogram, cost_threshold); |
| 341 VP8LHistogram modified_histo; | |
| 342 modified_histo = *candidate; | |
| 343 VP8LHistogramAdd(&modified_histo, square_histogram); | |
| 344 new_bit_cost = VP8LHistogramEstimateBits(&modified_histo); | |
| 345 | |
| 346 return new_bit_cost - previous_bit_cost; | |
| 347 } | 446 } |
| 348 | 447 |
| 349 // Find the best 'out' histogram for each of the 'in' histograms. | 448 // Find the best 'out' histogram for each of the 'in' histograms. |
| 350 // Note: we assume that out[]->bit_cost_ is already up-to-date. | 449 // Note: we assume that out[]->bit_cost_ is already up-to-date. |
| 351 static void HistogramRemap(const VP8LHistogramSet* const in, | 450 static void HistogramRemap(const VP8LHistogramSet* const in, |
| 352 const VP8LHistogramSet* const out, | 451 const VP8LHistogramSet* const out, |
| 353 uint16_t* const symbols) { | 452 uint16_t* const symbols) { |
| 354 int i; | 453 int i; |
| 355 for (i = 0; i < in->size; ++i) { | 454 for (i = 0; i < in->size; ++i) { |
| 356 int best_out = 0; | 455 int best_out = 0; |
| 357 double best_bits = HistogramDistance(in->histograms[i], out->histograms[0]); | 456 double best_bits = |
| 457 HistogramDistance(in->histograms[i], out->histograms[0], 1.e38); |
| 358 int k; | 458 int k; |
| 359 for (k = 1; k < out->size; ++k) { | 459 for (k = 1; k < out->size; ++k) { |
| 360 const double cur_bits = | 460 const double cur_bits = |
| 361 HistogramDistance(in->histograms[i], out->histograms[k]); | 461 HistogramDistance(in->histograms[i], out->histograms[k], best_bits); |
| 362 if (cur_bits < best_bits) { | 462 if (cur_bits < best_bits) { |
| 363 best_bits = cur_bits; | 463 best_bits = cur_bits; |
| 364 best_out = k; | 464 best_out = k; |
| 365 } | 465 } |
| 366 } | 466 } |
| 367 symbols[i] = best_out; | 467 symbols[i] = best_out; |
| 368 } | 468 } |
| 369 | 469 |
| 370 // Recompute each out based on raw and symbols. | 470 // Recompute each out based on raw and symbols. |
| 371 for (i = 0; i < out->size; ++i) { | 471 for (i = 0; i < out->size; ++i) { |
| 372 HistogramClear(out->histograms[i]); | 472 HistogramClear(out->histograms[i]); |
| 373 } | 473 } |
| 374 for (i = 0; i < in->size; ++i) { | 474 for (i = 0; i < in->size; ++i) { |
| 375 VP8LHistogramAdd(out->histograms[symbols[i]], in->histograms[i]); | 475 HistogramAdd(in->histograms[i], out->histograms[symbols[i]]); |
| 376 } | 476 } |
| 377 } | 477 } |
| 378 | 478 |
| 379 int VP8LGetHistoImageSymbols(int xsize, int ysize, | 479 int VP8LGetHistoImageSymbols(int xsize, int ysize, |
| 380 const VP8LBackwardRefs* const refs, | 480 const VP8LBackwardRefs* const refs, |
| 381 int quality, int histo_bits, int cache_bits, | 481 int quality, int histo_bits, int cache_bits, |
| 382 VP8LHistogramSet* const image_in, | 482 VP8LHistogramSet* const image_in, |
| 383 uint16_t* const histogram_symbols) { | 483 uint16_t* const histogram_symbols) { |
| 384 int ok = 0; | 484 int ok = 0; |
| 385 const int histo_xsize = histo_bits ? VP8LSubSampleSize(xsize, histo_bits) : 1; | 485 const int histo_xsize = histo_bits ? VP8LSubSampleSize(xsize, histo_bits) : 1; |
| 386 const int histo_ysize = histo_bits ? VP8LSubSampleSize(ysize, histo_bits) : 1; | 486 const int histo_ysize = histo_bits ? VP8LSubSampleSize(ysize, histo_bits) : 1; |
| 387 const int num_histo_pairs = 10 + quality / 2; // For HistogramCombine(). | |
| 388 const int histo_image_raw_size = histo_xsize * histo_ysize; | 487 const int histo_image_raw_size = histo_xsize * histo_ysize; |
| 488 |
| 489 // Heuristic params for HistogramCombine(). |
| 490 const int num_tries_no_success = 8 + (quality >> 1); |
| 491 const int iter_mult = (quality < 27) ? 1 : 1 + ((quality - 27) >> 4); |
| 492 const int num_pairs = (quality < 25) ? 10 : (5 * quality) >> 3; |
| 493 |
| 389 VP8LHistogramSet* const image_out = | 494 VP8LHistogramSet* const image_out = |
| 390 VP8LAllocateHistogramSet(histo_image_raw_size, cache_bits); | 495 VP8LAllocateHistogramSet(histo_image_raw_size, cache_bits); |
| 391 if (image_out == NULL) return 0; | 496 if (image_out == NULL) return 0; |
| 392 | 497 |
| 393 // Build histogram image. | 498 // Build histogram image. |
| 394 HistogramBuildImage(xsize, histo_bits, refs, image_out); | 499 HistogramBuildImage(xsize, histo_bits, refs, image_out); |
| 395 // Collapse similar histograms. | 500 // Collapse similar histograms. |
| 396 if (!HistogramCombine(image_out, image_in, num_histo_pairs)) { | 501 if (!HistogramCombine(image_out, image_in, iter_mult, num_pairs, |
| 502 num_tries_no_success)) { |
| 397 goto Error; | 503 goto Error; |
| 398 } | 504 } |
| 399 // Find the optimal map from original histograms to the final ones. | 505 // Find the optimal map from original histograms to the final ones. |
| 400 HistogramRemap(image_out, image_in, histogram_symbols); | 506 HistogramRemap(image_out, image_in, histogram_symbols); |
| 401 ok = 1; | 507 ok = 1; |
| 402 | 508 |
| 403 Error: | 509 Error: |
| 404 free(image_out); | 510 free(image_out); |
| 405 return ok; | 511 return ok; |
| 406 } | 512 } |
| OLD | NEW |