| 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 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 |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // main entry for the lossless encoder. |
| 11 | // |
| 12 | // Author: Vikas Arora (vikaas.arora@gmail.com) |
| 13 | // |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include "src/dsp/lossless.h" |
| 19 | #include "src/dsp/lossless_common.h" |
| 20 | #include "src/enc/backward_references_enc.h" |
| 21 | #include "src/enc/histogram_enc.h" |
| 22 | #include "src/enc/vp8i_enc.h" |
| 23 | #include "src/enc/vp8li_enc.h" |
| 24 | #include "src/utils/bit_writer_utils.h" |
| 25 | #include "src/utils/huffman_encode_utils.h" |
| 26 | #include "src/utils/utils.h" |
| 27 | #include "src/webp/encode.h" |
| 28 | #include "src/webp/format_constants.h" |
| 29 | |
| 30 | // Maximum number of histogram images (sub-blocks). |
| 31 | #define MAX_HUFF_IMAGE_SIZE 2600 |
| 32 | |
| 33 | // Palette reordering for smaller sum of deltas (and for smaller storage). |
| 34 | |
| 35 | static int PaletteCompareColorsForQsort(const void* p1, const void* p2) { |
| 36 | const uint32_t a = WebPMemToUint32((uint8_t*)p1); |
| 37 | const uint32_t b = WebPMemToUint32((uint8_t*)p2); |
| 38 | assert(a != b); |
| 39 | return (a < b) ? -1 : 1; |
| 40 | } |
| 41 | |
| 42 | static WEBP_INLINE uint32_t PaletteComponentDistance(uint32_t v) { |
| 43 | return (v <= 128) ? v : (256 - v); |
| 44 | } |
| 45 | |
| 46 | // Computes a value that is related to the entropy created by the |
| 47 | // palette entry diff. |
| 48 | // |
| 49 | // Note that the last & 0xff is a no-operation in the next statement, but |
| 50 | // removed by most compilers and is here only for regularity of the code. |
| 51 | static WEBP_INLINE uint32_t PaletteColorDistance(uint32_t col1, uint32_t col2) { |
| 52 | const uint32_t diff = VP8LSubPixels(col1, col2); |
| 53 | const int kMoreWeightForRGBThanForAlpha = 9; |
| 54 | uint32_t score; |
| 55 | score = PaletteComponentDistance((diff >> 0) & 0xff); |
| 56 | score += PaletteComponentDistance((diff >> 8) & 0xff); |
| 57 | score += PaletteComponentDistance((diff >> 16) & 0xff); |
| 58 | score *= kMoreWeightForRGBThanForAlpha; |
| 59 | score += PaletteComponentDistance((diff >> 24) & 0xff); |
| 60 | return score; |
| 61 | } |
| 62 | |
| 63 | static WEBP_INLINE void SwapColor(uint32_t* const col1, uint32_t* const col2) { |
| 64 | const uint32_t tmp = *col1; |
| 65 | *col1 = *col2; |
| 66 | *col2 = tmp; |
| 67 | } |
| 68 | |
| 69 | static WEBP_INLINE int SearchColorNoIdx(const uint32_t sorted[], uint32_t color, |
| 70 | int num_colors) { |
| 71 | int low = 0, hi = num_colors; |
| 72 | if (sorted[low] == color) return low; // loop invariant: sorted[low] != color |
| 73 | while (1) { |
| 74 | const int mid = (low + hi) >> 1; |
| 75 | if (sorted[mid] == color) { |
| 76 | return mid; |
| 77 | } else if (sorted[mid] < color) { |
| 78 | low = mid; |
| 79 | } else { |
| 80 | hi = mid; |
| 81 | } |
| 82 | } |
| 83 | assert(0); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | // The palette has been sorted by alpha. This function checks if the other |
| 88 | // components of the palette have a monotonic development with regards to |
| 89 | // position in the palette. If all have monotonic development, there is |
| 90 | // no benefit to re-organize them greedily. A monotonic development |
| 91 | // would be spotted in green-only situations (like lossy alpha) or gray-scale |
| 92 | // images. |
| 93 | static int PaletteHasNonMonotonousDeltas(const uint32_t* const palette, |
| 94 | int num_colors) { |
| 95 | uint32_t predict = 0x000000; |
| 96 | int i; |
| 97 | uint8_t sign_found = 0x00; |
| 98 | for (i = 0; i < num_colors; ++i) { |
| 99 | const uint32_t diff = VP8LSubPixels(palette[i], predict); |
| 100 | const uint8_t rd = (diff >> 16) & 0xff; |
| 101 | const uint8_t gd = (diff >> 8) & 0xff; |
| 102 | const uint8_t bd = (diff >> 0) & 0xff; |
| 103 | if (rd != 0x00) { |
| 104 | sign_found |= (rd < 0x80) ? 1 : 2; |
| 105 | } |
| 106 | if (gd != 0x00) { |
| 107 | sign_found |= (gd < 0x80) ? 8 : 16; |
| 108 | } |
| 109 | if (bd != 0x00) { |
| 110 | sign_found |= (bd < 0x80) ? 64 : 128; |
| 111 | } |
| 112 | predict = palette[i]; |
| 113 | } |
| 114 | return (sign_found & (sign_found << 1)) != 0; // two consequent signs. |
| 115 | } |
| 116 | |
| 117 | static void PaletteSortMinimizeDeltas(const uint32_t* const palette_sorted, |
| 118 | int num_colors, uint32_t* const palette) { |
| 119 | uint32_t predict = 0x00000000; |
| 120 | int i, k; |
| 121 | memcpy(palette, palette_sorted, num_colors * sizeof(*palette)); |
| 122 | if (!PaletteHasNonMonotonousDeltas(palette_sorted, num_colors)) return; |
| 123 | // Find greedily always the closest color of the predicted color to minimize |
| 124 | // deltas in the palette. This reduces storage needs since the |
| 125 | // palette is stored with delta encoding. |
| 126 | for (i = 0; i < num_colors; ++i) { |
| 127 | int best_ix = i; |
| 128 | uint32_t best_score = ~0U; |
| 129 | for (k = i; k < num_colors; ++k) { |
| 130 | const uint32_t cur_score = PaletteColorDistance(palette[k], predict); |
| 131 | if (best_score > cur_score) { |
| 132 | best_score = cur_score; |
| 133 | best_ix = k; |
| 134 | } |
| 135 | } |
| 136 | SwapColor(&palette[best_ix], &palette[i]); |
| 137 | predict = palette[i]; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Sort palette in increasing order and prepare an inverse mapping array. |
| 142 | static void PrepareMapToPalette(const uint32_t palette[], uint32_t num_colors, |
| 143 | uint32_t sorted[], uint32_t idx_map[]) { |
| 144 | uint32_t i; |
| 145 | memcpy(sorted, palette, num_colors * sizeof(*sorted)); |
| 146 | qsort(sorted, num_colors, sizeof(*sorted), PaletteCompareColorsForQsort); |
| 147 | for (i = 0; i < num_colors; ++i) { |
| 148 | idx_map[SearchColorNoIdx(sorted, palette[i], num_colors)] = i; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // ----------------------------------------------------------------------------- |
| 153 | // Modified Zeng method from "A Survey on Palette Reordering |
| 154 | // Methods for Improving the Compression of Color-Indexed Images" by Armando J. |
| 155 | // Pinho and Antonio J. R. Neves. |
| 156 | |
| 157 | // Finds the biggest cooccurrence in the matrix. |
| 158 | static void CoOccurrenceFindMax(const uint32_t* const cooccurrence, |
| 159 | uint32_t num_colors, uint8_t* const c1, |
| 160 | uint8_t* const c2) { |
| 161 | // Find the index that is most frequently located adjacent to other |
| 162 | // (different) indexes. |
| 163 | uint32_t best_sum = 0u; |
| 164 | uint32_t i, j, best_cooccurrence; |
| 165 | *c1 = 0u; |
| 166 | for (i = 0; i < num_colors; ++i) { |
| 167 | uint32_t sum = 0; |
| 168 | for (j = 0; j < num_colors; ++j) sum += cooccurrence[i * num_colors + j]; |
| 169 | if (sum > best_sum) { |
| 170 | best_sum = sum; |
| 171 | *c1 = i; |
| 172 | } |
| 173 | } |
| 174 | // Find the index that is most frequently found adjacent to *c1. |
| 175 | *c2 = 0u; |
| 176 | best_cooccurrence = 0u; |
| 177 | for (i = 0; i < num_colors; ++i) { |
| 178 | if (cooccurrence[*c1 * num_colors + i] > best_cooccurrence) { |
| 179 | best_cooccurrence = cooccurrence[*c1 * num_colors + i]; |
| 180 | *c2 = i; |
| 181 | } |
| 182 | } |
| 183 | assert(*c1 != *c2); |
| 184 | } |
| 185 | |
| 186 | // Builds the cooccurrence matrix |
| 187 | static int CoOccurrenceBuild(const WebPPicture* const pic, |
| 188 | const uint32_t* const palette, uint32_t num_colors, |
| 189 | uint32_t* cooccurrence) { |
| 190 | uint32_t *lines, *line_top, *line_current, *line_tmp; |
| 191 | int x, y; |
| 192 | const uint32_t* src = pic->argb; |
| 193 | uint32_t prev_pix = ~src[0]; |
| 194 | uint32_t prev_idx = 0u; |
| 195 | uint32_t idx_map[MAX_PALETTE_SIZE] = {0}; |
| 196 | uint32_t palette_sorted[MAX_PALETTE_SIZE]; |
| 197 | lines = (uint32_t*)WebPSafeMalloc(2 * pic->width, sizeof(*lines)); |
| 198 | if (lines == NULL) { |
| 199 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 200 | } |
| 201 | line_top = &lines[0]; |
| 202 | line_current = &lines[pic->width]; |
| 203 | PrepareMapToPalette(palette, num_colors, palette_sorted, idx_map); |
| 204 | for (y = 0; y < pic->height; ++y) { |
| 205 | for (x = 0; x < pic->width; ++x) { |
| 206 | const uint32_t pix = src[x]; |
| 207 | if (pix != prev_pix) { |
| 208 | prev_idx = idx_map[SearchColorNoIdx(palette_sorted, pix, num_colors)]; |
| 209 | prev_pix = pix; |
| 210 | } |
| 211 | line_current[x] = prev_idx; |
| 212 | // 4-connectivity is what works best as mentioned in "On the relation |
| 213 | // between Memon's and the modified Zeng's palette reordering methods". |
| 214 | if (x > 0 && prev_idx != line_current[x - 1]) { |
| 215 | const uint32_t left_idx = line_current[x - 1]; |
| 216 | ++cooccurrence[prev_idx * num_colors + left_idx]; |
| 217 | ++cooccurrence[left_idx * num_colors + prev_idx]; |
| 218 | } |
| 219 | if (y > 0 && prev_idx != line_top[x]) { |
| 220 | const uint32_t top_idx = line_top[x]; |
| 221 | ++cooccurrence[prev_idx * num_colors + top_idx]; |
| 222 | ++cooccurrence[top_idx * num_colors + prev_idx]; |
| 223 | } |
| 224 | } |
| 225 | line_tmp = line_top; |
| 226 | line_top = line_current; |
| 227 | line_current = line_tmp; |
| 228 | src += pic->argb_stride; |
| 229 | } |
| 230 | WebPSafeFree(lines); |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | struct Sum { |
| 235 | uint8_t index; |
| 236 | uint32_t sum; |
| 237 | }; |
| 238 | |
| 239 | // Implements the modified Zeng method from "A Survey on Palette Reordering |
| 240 | // Methods for Improving the Compression of Color-Indexed Images" by Armando J. |
| 241 | // Pinho and Antonio J. R. Neves. |
| 242 | static int PaletteSortModifiedZeng( |
| 243 | const WebPPicture* const pic, const uint32_t* const palette_sorted, |
| 244 | uint32_t num_colors, uint32_t* const palette) { |
| 245 | uint32_t i, j, ind; |
| 246 | uint8_t remapping[MAX_PALETTE_SIZE]; |
| 247 | uint32_t* cooccurrence; |
| 248 | struct Sum sums[MAX_PALETTE_SIZE]; |
| 249 | uint32_t first, last; |
| 250 | uint32_t num_sums; |
| 251 | // TODO(vrabaud) check whether one color images should use palette or not. |
| 252 | if (num_colors <= 1) return 1; |
| 253 | // Build the co-occurrence matrix. |
| 254 | cooccurrence = |
| 255 | (uint32_t*)WebPSafeCalloc(num_colors * num_colors, sizeof(*cooccurrence)); |
| 256 | if (cooccurrence == NULL) { |
| 257 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 258 | } |
| 259 | if (!CoOccurrenceBuild(pic, palette_sorted, num_colors, cooccurrence)) { |
| 260 | WebPSafeFree(cooccurrence); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | // Initialize the mapping list with the two best indices. |
| 265 | CoOccurrenceFindMax(cooccurrence, num_colors, &remapping[0], &remapping[1]); |
| 266 | |
| 267 | // We need to append and prepend to the list of remapping. To this end, we |
| 268 | // actually define the next start/end of the list as indices in a vector (with |
| 269 | // a wrap around when the end is reached). |
| 270 | first = 0; |
| 271 | last = 1; |
| 272 | num_sums = num_colors - 2; // -2 because we know the first two values |
| 273 | if (num_sums > 0) { |
| 274 | // Initialize the sums with the first two remappings and find the best one |
| 275 | struct Sum* best_sum = &sums[0]; |
| 276 | best_sum->index = 0u; |
| 277 | best_sum->sum = 0u; |
| 278 | for (i = 0, j = 0; i < num_colors; ++i) { |
| 279 | if (i == remapping[0] || i == remapping[1]) continue; |
| 280 | sums[j].index = i; |
| 281 | sums[j].sum = cooccurrence[i * num_colors + remapping[0]] + |
| 282 | cooccurrence[i * num_colors + remapping[1]]; |
| 283 | if (sums[j].sum > best_sum->sum) best_sum = &sums[j]; |
| 284 | ++j; |
| 285 | } |
| 286 | |
| 287 | while (num_sums > 0) { |
| 288 | const uint8_t best_index = best_sum->index; |
| 289 | // Compute delta to know if we need to prepend or append the best index. |
| 290 | int32_t delta = 0; |
| 291 | const int32_t n = num_colors - num_sums; |
| 292 | for (ind = first, j = 0; (ind + j) % num_colors != last + 1; ++j) { |
| 293 | const uint16_t l_j = remapping[(ind + j) % num_colors]; |
| 294 | delta += (n - 1 - 2 * (int32_t)j) * |
| 295 | (int32_t)cooccurrence[best_index * num_colors + l_j]; |
| 296 | } |
| 297 | if (delta > 0) { |
| 298 | first = (first == 0) ? num_colors - 1 : first - 1; |
| 299 | remapping[first] = best_index; |
| 300 | } else { |
| 301 | ++last; |
| 302 | remapping[last] = best_index; |
| 303 | } |
| 304 | // Remove best_sum from sums. |
| 305 | *best_sum = sums[num_sums - 1]; |
| 306 | --num_sums; |
| 307 | // Update all the sums and find the best one. |
| 308 | best_sum = &sums[0]; |
| 309 | for (i = 0; i < num_sums; ++i) { |
| 310 | sums[i].sum += cooccurrence[best_index * num_colors + sums[i].index]; |
| 311 | if (sums[i].sum > best_sum->sum) best_sum = &sums[i]; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | assert((last + 1) % num_colors == first); |
| 316 | WebPSafeFree(cooccurrence); |
| 317 | |
| 318 | // Re-map the palette. |
| 319 | for (i = 0; i < num_colors; ++i) { |
| 320 | palette[i] = palette_sorted[remapping[(first + i) % num_colors]]; |
| 321 | } |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | // ----------------------------------------------------------------------------- |
| 326 | // Palette |
| 327 | |
| 328 | // These five modes are evaluated and their respective entropy is computed. |
| 329 | typedef enum { |
| 330 | kDirect = 0, |
| 331 | kSpatial = 1, |
| 332 | kSubGreen = 2, |
| 333 | kSpatialSubGreen = 3, |
| 334 | kPalette = 4, |
| 335 | kPaletteAndSpatial = 5, |
| 336 | kNumEntropyIx = 6 |
| 337 | } EntropyIx; |
| 338 | |
| 339 | typedef enum { |
| 340 | kSortedDefault = 0, |
| 341 | kMinimizeDelta = 1, |
| 342 | kModifiedZeng = 2, |
| 343 | kUnusedPalette = 3, |
| 344 | } PaletteSorting; |
| 345 | |
| 346 | typedef enum { |
| 347 | kHistoAlpha = 0, |
| 348 | kHistoAlphaPred, |
| 349 | kHistoGreen, |
| 350 | kHistoGreenPred, |
| 351 | kHistoRed, |
| 352 | kHistoRedPred, |
| 353 | kHistoBlue, |
| 354 | kHistoBluePred, |
| 355 | kHistoRedSubGreen, |
| 356 | kHistoRedPredSubGreen, |
| 357 | kHistoBlueSubGreen, |
| 358 | kHistoBluePredSubGreen, |
| 359 | kHistoPalette, |
| 360 | kHistoTotal // Must be last. |
| 361 | } HistoIx; |
| 362 | |
| 363 | static void AddSingleSubGreen(uint32_t p, |
| 364 | uint32_t* const r, uint32_t* const b) { |
| 365 | const int green = (int)p >> 8; // The upper bits are masked away later. |
| 366 | ++r[(((int)p >> 16) - green) & 0xff]; |
| 367 | ++b[(((int)p >> 0) - green) & 0xff]; |
| 368 | } |
| 369 | |
| 370 | static void AddSingle(uint32_t p, |
| 371 | uint32_t* const a, uint32_t* const r, |
| 372 | uint32_t* const g, uint32_t* const b) { |
| 373 | ++a[(p >> 24) & 0xff]; |
| 374 | ++r[(p >> 16) & 0xff]; |
| 375 | ++g[(p >> 8) & 0xff]; |
| 376 | ++b[(p >> 0) & 0xff]; |
| 377 | } |
| 378 | |
| 379 | static WEBP_INLINE uint32_t HashPix(uint32_t pix) { |
| 380 | // Note that masking with 0xffffffffu is for preventing an |
| 381 | // 'unsigned int overflow' warning. Doesn't impact the compiled code. |
| 382 | return ((((uint64_t)pix + (pix >> 19)) * 0x39c5fba7ull) & 0xffffffffu) >> 24; |
| 383 | } |
| 384 | |
| 385 | static int AnalyzeEntropy(const uint32_t* argb, |
| 386 | int width, int height, int argb_stride, |
| 387 | int use_palette, |
| 388 | int palette_size, int transform_bits, |
| 389 | EntropyIx* const min_entropy_ix, |
| 390 | int* const red_and_blue_always_zero) { |
| 391 | // Allocate histogram set with cache_bits = 0. |
| 392 | uint32_t* histo; |
| 393 | |
| 394 | if (use_palette && palette_size <= 16) { |
| 395 | // In the case of small palettes, we pack 2, 4 or 8 pixels together. In |
| 396 | // practice, small palettes are better than any other transform. |
| 397 | *min_entropy_ix = kPalette; |
| 398 | *red_and_blue_always_zero = 1; |
| 399 | return 1; |
| 400 | } |
| 401 | histo = (uint32_t*)WebPSafeCalloc(kHistoTotal, sizeof(*histo) * 256); |
| 402 | if (histo != NULL) { |
| 403 | int i, x, y; |
| 404 | const uint32_t* prev_row = NULL; |
| 405 | const uint32_t* curr_row = argb; |
| 406 | uint32_t pix_prev = argb[0]; // Skip the first pixel. |
| 407 | for (y = 0; y < height; ++y) { |
| 408 | for (x = 0; x < width; ++x) { |
| 409 | const uint32_t pix = curr_row[x]; |
| 410 | const uint32_t pix_diff = VP8LSubPixels(pix, pix_prev); |
| 411 | pix_prev = pix; |
| 412 | if ((pix_diff == 0) || (prev_row != NULL && pix == prev_row[x])) { |
| 413 | continue; |
| 414 | } |
| 415 | AddSingle(pix, |
| 416 | &histo[kHistoAlpha * 256], |
| 417 | &histo[kHistoRed * 256], |
| 418 | &histo[kHistoGreen * 256], |
| 419 | &histo[kHistoBlue * 256]); |
| 420 | AddSingle(pix_diff, |
| 421 | &histo[kHistoAlphaPred * 256], |
| 422 | &histo[kHistoRedPred * 256], |
| 423 | &histo[kHistoGreenPred * 256], |
| 424 | &histo[kHistoBluePred * 256]); |
| 425 | AddSingleSubGreen(pix, |
| 426 | &histo[kHistoRedSubGreen * 256], |
| 427 | &histo[kHistoBlueSubGreen * 256]); |
| 428 | AddSingleSubGreen(pix_diff, |
| 429 | &histo[kHistoRedPredSubGreen * 256], |
| 430 | &histo[kHistoBluePredSubGreen * 256]); |
| 431 | { |
| 432 | // Approximate the palette by the entropy of the multiplicative hash. |
| 433 | const uint32_t hash = HashPix(pix); |
| 434 | ++histo[kHistoPalette * 256 + hash]; |
| 435 | } |
| 436 | } |
| 437 | prev_row = curr_row; |
| 438 | curr_row += argb_stride; |
| 439 | } |
| 440 | { |
| 441 | float entropy_comp[kHistoTotal]; |
| 442 | float entropy[kNumEntropyIx]; |
| 443 | int k; |
| 444 | int last_mode_to_analyze = use_palette ? kPalette : kSpatialSubGreen; |
| 445 | int j; |
| 446 | // Let's add one zero to the predicted histograms. The zeros are removed |
| 447 | // too efficiently by the pix_diff == 0 comparison, at least one of the |
| 448 | // zeros is likely to exist. |
| 449 | ++histo[kHistoRedPredSubGreen * 256]; |
| 450 | ++histo[kHistoBluePredSubGreen * 256]; |
| 451 | ++histo[kHistoRedPred * 256]; |
| 452 | ++histo[kHistoGreenPred * 256]; |
| 453 | ++histo[kHistoBluePred * 256]; |
| 454 | ++histo[kHistoAlphaPred * 256]; |
| 455 | |
| 456 | for (j = 0; j < kHistoTotal; ++j) { |
| 457 | entropy_comp[j] = VP8LBitsEntropy(&histo[j * 256], 256); |
| 458 | } |
| 459 | entropy[kDirect] = entropy_comp[kHistoAlpha] + |
| 460 | entropy_comp[kHistoRed] + |
| 461 | entropy_comp[kHistoGreen] + |
| 462 | entropy_comp[kHistoBlue]; |
| 463 | entropy[kSpatial] = entropy_comp[kHistoAlphaPred] + |
| 464 | entropy_comp[kHistoRedPred] + |
| 465 | entropy_comp[kHistoGreenPred] + |
| 466 | entropy_comp[kHistoBluePred]; |
| 467 | entropy[kSubGreen] = entropy_comp[kHistoAlpha] + |
| 468 | entropy_comp[kHistoRedSubGreen] + |
| 469 | entropy_comp[kHistoGreen] + |
| 470 | entropy_comp[kHistoBlueSubGreen]; |
| 471 | entropy[kSpatialSubGreen] = entropy_comp[kHistoAlphaPred] + |
| 472 | entropy_comp[kHistoRedPredSubGreen] + |
| 473 | entropy_comp[kHistoGreenPred] + |
| 474 | entropy_comp[kHistoBluePredSubGreen]; |
| 475 | entropy[kPalette] = entropy_comp[kHistoPalette]; |
| 476 | |
| 477 | // When including transforms, there is an overhead in bits from |
| 478 | // storing them. This overhead is small but matters for small images. |
| 479 | // For spatial, there are 14 transformations. |
| 480 | entropy[kSpatial] += VP8LSubSampleSize(width, transform_bits) * |
| 481 | VP8LSubSampleSize(height, transform_bits) * |
| 482 | VP8LFastLog2(14); |
| 483 | // For color transforms: 24 as only 3 channels are considered in a |
| 484 | // ColorTransformElement. |
| 485 | entropy[kSpatialSubGreen] += VP8LSubSampleSize(width, transform_bits) * |
| 486 | VP8LSubSampleSize(height, transform_bits) * |
| 487 | VP8LFastLog2(24); |
| 488 | // For palettes, add the cost of storing the palette. |
| 489 | // We empirically estimate the cost of a compressed entry as 8 bits. |
| 490 | // The palette is differential-coded when compressed hence a much |
| 491 | // lower cost than sizeof(uint32_t)*8. |
| 492 | entropy[kPalette] += palette_size * 8; |
| 493 | |
| 494 | *min_entropy_ix = kDirect; |
| 495 | for (k = kDirect + 1; k <= last_mode_to_analyze; ++k) { |
| 496 | if (entropy[*min_entropy_ix] > entropy[k]) { |
| 497 | *min_entropy_ix = (EntropyIx)k; |
| 498 | } |
| 499 | } |
| 500 | assert((int)*min_entropy_ix <= last_mode_to_analyze); |
| 501 | *red_and_blue_always_zero = 1; |
| 502 | // Let's check if the histogram of the chosen entropy mode has |
| 503 | // non-zero red and blue values. If all are zero, we can later skip |
| 504 | // the cross color optimization. |
| 505 | { |
| 506 | static const uint8_t kHistoPairs[5][2] = { |
| 507 | { kHistoRed, kHistoBlue }, |
| 508 | { kHistoRedPred, kHistoBluePred }, |
| 509 | { kHistoRedSubGreen, kHistoBlueSubGreen }, |
| 510 | { kHistoRedPredSubGreen, kHistoBluePredSubGreen }, |
| 511 | { kHistoRed, kHistoBlue } |
| 512 | }; |
| 513 | const uint32_t* const red_histo = |
| 514 | &histo[256 * kHistoPairs[*min_entropy_ix][0]]; |
| 515 | const uint32_t* const blue_histo = |
| 516 | &histo[256 * kHistoPairs[*min_entropy_ix][1]]; |
| 517 | for (i = 1; i < 256; ++i) { |
| 518 | if ((red_histo[i] | blue_histo[i]) != 0) { |
| 519 | *red_and_blue_always_zero = 0; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | WebPSafeFree(histo); |
| 526 | return 1; |
| 527 | } else { |
| 528 | return 0; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static int GetHistoBits(int method, int use_palette, int width, int height) { |
| 533 | // Make tile size a function of encoding method (Range: 0 to 6). |
| 534 | int histo_bits = (use_palette ? 9 : 7) - method; |
| 535 | while (1) { |
| 536 | const int huff_image_size = VP8LSubSampleSize(width, histo_bits) * |
| 537 | VP8LSubSampleSize(height, histo_bits); |
| 538 | if (huff_image_size <= MAX_HUFF_IMAGE_SIZE) break; |
| 539 | ++histo_bits; |
| 540 | } |
| 541 | return (histo_bits < MIN_HUFFMAN_BITS) ? MIN_HUFFMAN_BITS : |
| 542 | (histo_bits > MAX_HUFFMAN_BITS) ? MAX_HUFFMAN_BITS : histo_bits; |
| 543 | } |
| 544 | |
| 545 | static int GetTransformBits(int method, int histo_bits) { |
| 546 | const int max_transform_bits = (method < 4) ? 6 : (method > 4) ? 4 : 5; |
| 547 | const int res = |
| 548 | (histo_bits > max_transform_bits) ? max_transform_bits : histo_bits; |
| 549 | assert(res <= MAX_TRANSFORM_BITS); |
| 550 | return res; |
| 551 | } |
| 552 | |
| 553 | // Set of parameters to be used in each iteration of the cruncher. |
| 554 | #define CRUNCH_SUBCONFIGS_MAX 2 |
| 555 | typedef struct { |
| 556 | int lz77_; |
| 557 | int do_no_cache_; |
| 558 | } CrunchSubConfig; |
| 559 | typedef struct { |
| 560 | int entropy_idx_; |
| 561 | PaletteSorting palette_sorting_type_; |
| 562 | CrunchSubConfig sub_configs_[CRUNCH_SUBCONFIGS_MAX]; |
| 563 | int sub_configs_size_; |
| 564 | } CrunchConfig; |
| 565 | |
| 566 | // +2 because we add a palette sorting configuration for kPalette and |
| 567 | // kPaletteAndSpatial. |
| 568 | #define CRUNCH_CONFIGS_MAX (kNumEntropyIx + 2) |
| 569 | |
| 570 | static int EncoderAnalyze(VP8LEncoder* const enc, |
| 571 | CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX], |
| 572 | int* const crunch_configs_size, |
| 573 | int* const red_and_blue_always_zero) { |
| 574 | const WebPPicture* const pic = enc->pic_; |
| 575 | const int width = pic->width; |
| 576 | const int height = pic->height; |
| 577 | const WebPConfig* const config = enc->config_; |
| 578 | const int method = config->method; |
| 579 | const int low_effort = (config->method == 0); |
| 580 | int i; |
| 581 | int use_palette; |
| 582 | int n_lz77s; |
| 583 | // If set to 0, analyze the cache with the computed cache value. If 1, also |
| 584 | // analyze with no-cache. |
| 585 | int do_no_cache = 0; |
| 586 | assert(pic != NULL && pic->argb != NULL); |
| 587 | |
| 588 | // Check whether a palette is possible. |
| 589 | enc->palette_size_ = WebPGetColorPalette(pic, enc->palette_sorted_); |
| 590 | use_palette = (enc->palette_size_ <= MAX_PALETTE_SIZE); |
| 591 | if (!use_palette) { |
| 592 | enc->palette_size_ = 0; |
| 593 | } else { |
| 594 | qsort(enc->palette_sorted_, enc->palette_size_, |
| 595 | sizeof(*enc->palette_sorted_), PaletteCompareColorsForQsort); |
| 596 | } |
| 597 | |
| 598 | // Empirical bit sizes. |
| 599 | enc->histo_bits_ = GetHistoBits(method, use_palette, |
| 600 | pic->width, pic->height); |
| 601 | enc->transform_bits_ = GetTransformBits(method, enc->histo_bits_); |
| 602 | |
| 603 | if (low_effort) { |
| 604 | // AnalyzeEntropy is somewhat slow. |
| 605 | crunch_configs[0].entropy_idx_ = use_palette ? kPalette : kSpatialSubGreen; |
| 606 | crunch_configs[0].palette_sorting_type_ = |
| 607 | use_palette ? kSortedDefault : kUnusedPalette; |
| 608 | n_lz77s = 1; |
| 609 | *crunch_configs_size = 1; |
| 610 | } else { |
| 611 | EntropyIx min_entropy_ix; |
| 612 | // Try out multiple LZ77 on images with few colors. |
| 613 | n_lz77s = (enc->palette_size_ > 0 && enc->palette_size_ <= 16) ? 2 : 1; |
| 614 | if (!AnalyzeEntropy(pic->argb, width, height, pic->argb_stride, use_palette, |
| 615 | enc->palette_size_, enc->transform_bits_, |
| 616 | &min_entropy_ix, red_and_blue_always_zero)) { |
| 617 | return 0; |
| 618 | } |
| 619 | if (method == 6 && config->quality == 100) { |
| 620 | do_no_cache = 1; |
| 621 | // Go brute force on all transforms. |
| 622 | *crunch_configs_size = 0; |
| 623 | for (i = 0; i < kNumEntropyIx; ++i) { |
| 624 | // We can only apply kPalette or kPaletteAndSpatial if we can indeed use |
| 625 | // a palette. |
| 626 | if ((i != kPalette && i != kPaletteAndSpatial) || use_palette) { |
| 627 | assert(*crunch_configs_size < CRUNCH_CONFIGS_MAX); |
| 628 | crunch_configs[(*crunch_configs_size)].entropy_idx_ = i; |
| 629 | if (use_palette && (i == kPalette || i == kPaletteAndSpatial)) { |
| 630 | crunch_configs[(*crunch_configs_size)].palette_sorting_type_ = |
| 631 | kMinimizeDelta; |
| 632 | ++*crunch_configs_size; |
| 633 | // Also add modified Zeng's method. |
| 634 | crunch_configs[(*crunch_configs_size)].entropy_idx_ = i; |
| 635 | crunch_configs[(*crunch_configs_size)].palette_sorting_type_ = |
| 636 | kModifiedZeng; |
| 637 | } else { |
| 638 | crunch_configs[(*crunch_configs_size)].palette_sorting_type_ = |
| 639 | kUnusedPalette; |
| 640 | } |
| 641 | ++*crunch_configs_size; |
| 642 | } |
| 643 | } |
| 644 | } else { |
| 645 | // Only choose the guessed best transform. |
| 646 | *crunch_configs_size = 1; |
| 647 | crunch_configs[0].entropy_idx_ = min_entropy_ix; |
| 648 | crunch_configs[0].palette_sorting_type_ = |
| 649 | use_palette ? kMinimizeDelta : kUnusedPalette; |
| 650 | if (config->quality >= 75 && method == 5) { |
| 651 | // Test with and without color cache. |
| 652 | do_no_cache = 1; |
| 653 | // If we have a palette, also check in combination with spatial. |
| 654 | if (min_entropy_ix == kPalette) { |
| 655 | *crunch_configs_size = 2; |
| 656 | crunch_configs[1].entropy_idx_ = kPaletteAndSpatial; |
| 657 | crunch_configs[1].palette_sorting_type_ = kMinimizeDelta; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | // Fill in the different LZ77s. |
| 663 | assert(n_lz77s <= CRUNCH_SUBCONFIGS_MAX); |
| 664 | for (i = 0; i < *crunch_configs_size; ++i) { |
| 665 | int j; |
| 666 | for (j = 0; j < n_lz77s; ++j) { |
| 667 | assert(j < CRUNCH_SUBCONFIGS_MAX); |
| 668 | crunch_configs[i].sub_configs_[j].lz77_ = |
| 669 | (j == 0) ? kLZ77Standard | kLZ77RLE : kLZ77Box; |
| 670 | crunch_configs[i].sub_configs_[j].do_no_cache_ = do_no_cache; |
| 671 | } |
| 672 | crunch_configs[i].sub_configs_size_ = n_lz77s; |
| 673 | } |
| 674 | return 1; |
| 675 | } |
| 676 | |
| 677 | static int EncoderInit(VP8LEncoder* const enc) { |
| 678 | const WebPPicture* const pic = enc->pic_; |
| 679 | const int width = pic->width; |
| 680 | const int height = pic->height; |
| 681 | const int pix_cnt = width * height; |
| 682 | // we round the block size up, so we're guaranteed to have |
| 683 | // at most MAX_REFS_BLOCK_PER_IMAGE blocks used: |
| 684 | const int refs_block_size = (pix_cnt - 1) / MAX_REFS_BLOCK_PER_IMAGE + 1; |
| 685 | int i; |
| 686 | if (!VP8LHashChainInit(&enc->hash_chain_, pix_cnt)) return 0; |
| 687 | |
| 688 | for (i = 0; i < 4; ++i) VP8LBackwardRefsInit(&enc->refs_[i], refs_block_size); |
| 689 | |
| 690 | return 1; |
| 691 | } |
| 692 | |
| 693 | // Returns false in case of memory error. |
| 694 | static int GetHuffBitLengthsAndCodes( |
| 695 | const VP8LHistogramSet* const histogram_image, |
| 696 | HuffmanTreeCode* const huffman_codes) { |
| 697 | int i, k; |
| 698 | int ok = 0; |
| 699 | uint64_t total_length_size = 0; |
| 700 | uint8_t* mem_buf = NULL; |
| 701 | const int histogram_image_size = histogram_image->size; |
| 702 | int max_num_symbols = 0; |
| 703 | uint8_t* buf_rle = NULL; |
| 704 | HuffmanTree* huff_tree = NULL; |
| 705 | |
| 706 | // Iterate over all histograms and get the aggregate number of codes used. |
| 707 | for (i = 0; i < histogram_image_size; ++i) { |
| 708 | const VP8LHistogram* const histo = histogram_image->histograms[i]; |
| 709 | HuffmanTreeCode* const codes = &huffman_codes[5 * i]; |
| 710 | assert(histo != NULL); |
| 711 | for (k = 0; k < 5; ++k) { |
| 712 | const int num_symbols = |
| 713 | (k == 0) ? VP8LHistogramNumCodes(histo->palette_code_bits_) : |
| 714 | (k == 4) ? NUM_DISTANCE_CODES : 256; |
| 715 | codes[k].num_symbols = num_symbols; |
| 716 | total_length_size += num_symbols; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Allocate and Set Huffman codes. |
| 721 | { |
| 722 | uint16_t* codes; |
| 723 | uint8_t* lengths; |
| 724 | mem_buf = (uint8_t*)WebPSafeCalloc(total_length_size, |
| 725 | sizeof(*lengths) + sizeof(*codes)); |
| 726 | if (mem_buf == NULL) goto End; |
| 727 | |
| 728 | codes = (uint16_t*)mem_buf; |
| 729 | lengths = (uint8_t*)&codes[total_length_size]; |
| 730 | for (i = 0; i < 5 * histogram_image_size; ++i) { |
| 731 | const int bit_length = huffman_codes[i].num_symbols; |
| 732 | huffman_codes[i].codes = codes; |
| 733 | huffman_codes[i].code_lengths = lengths; |
| 734 | codes += bit_length; |
| 735 | lengths += bit_length; |
| 736 | if (max_num_symbols < bit_length) { |
| 737 | max_num_symbols = bit_length; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | buf_rle = (uint8_t*)WebPSafeMalloc(1ULL, max_num_symbols); |
| 743 | huff_tree = (HuffmanTree*)WebPSafeMalloc(3ULL * max_num_symbols, |
| 744 | sizeof(*huff_tree)); |
| 745 | if (buf_rle == NULL || huff_tree == NULL) goto End; |
| 746 | |
| 747 | // Create Huffman trees. |
| 748 | for (i = 0; i < histogram_image_size; ++i) { |
| 749 | HuffmanTreeCode* const codes = &huffman_codes[5 * i]; |
| 750 | VP8LHistogram* const histo = histogram_image->histograms[i]; |
| 751 | VP8LCreateHuffmanTree(histo->literal_, 15, buf_rle, huff_tree, codes + 0); |
| 752 | VP8LCreateHuffmanTree(histo->red_, 15, buf_rle, huff_tree, codes + 1); |
| 753 | VP8LCreateHuffmanTree(histo->blue_, 15, buf_rle, huff_tree, codes + 2); |
| 754 | VP8LCreateHuffmanTree(histo->alpha_, 15, buf_rle, huff_tree, codes + 3); |
| 755 | VP8LCreateHuffmanTree(histo->distance_, 15, buf_rle, huff_tree, codes + 4); |
| 756 | } |
| 757 | ok = 1; |
| 758 | End: |
| 759 | WebPSafeFree(huff_tree); |
| 760 | WebPSafeFree(buf_rle); |
| 761 | if (!ok) { |
| 762 | WebPSafeFree(mem_buf); |
| 763 | memset(huffman_codes, 0, 5 * histogram_image_size * sizeof(*huffman_codes)); |
| 764 | } |
| 765 | return ok; |
| 766 | } |
| 767 | |
| 768 | static void StoreHuffmanTreeOfHuffmanTreeToBitMask( |
| 769 | VP8LBitWriter* const bw, const uint8_t* code_length_bitdepth) { |
| 770 | // RFC 1951 will calm you down if you are worried about this funny sequence. |
| 771 | // This sequence is tuned from that, but more weighted for lower symbol count, |
| 772 | // and more spiking histograms. |
| 773 | static const uint8_t kStorageOrder[CODE_LENGTH_CODES] = { |
| 774 | 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 775 | }; |
| 776 | int i; |
| 777 | // Throw away trailing zeros: |
| 778 | int codes_to_store = CODE_LENGTH_CODES; |
| 779 | for (; codes_to_store > 4; --codes_to_store) { |
| 780 | if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) { |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | VP8LPutBits(bw, codes_to_store - 4, 4); |
| 785 | for (i = 0; i < codes_to_store; ++i) { |
| 786 | VP8LPutBits(bw, code_length_bitdepth[kStorageOrder[i]], 3); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | static void ClearHuffmanTreeIfOnlyOneSymbol( |
| 791 | HuffmanTreeCode* const huffman_code) { |
| 792 | int k; |
| 793 | int count = 0; |
| 794 | for (k = 0; k < huffman_code->num_symbols; ++k) { |
| 795 | if (huffman_code->code_lengths[k] != 0) { |
| 796 | ++count; |
| 797 | if (count > 1) return; |
| 798 | } |
| 799 | } |
| 800 | for (k = 0; k < huffman_code->num_symbols; ++k) { |
| 801 | huffman_code->code_lengths[k] = 0; |
| 802 | huffman_code->codes[k] = 0; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static void StoreHuffmanTreeToBitMask( |
| 807 | VP8LBitWriter* const bw, |
| 808 | const HuffmanTreeToken* const tokens, const int num_tokens, |
| 809 | const HuffmanTreeCode* const huffman_code) { |
| 810 | int i; |
| 811 | for (i = 0; i < num_tokens; ++i) { |
| 812 | const int ix = tokens[i].code; |
| 813 | const int = tokens[i].extra_bits; |
| 814 | VP8LPutBits(bw, huffman_code->codes[ix], huffman_code->code_lengths[ix]); |
| 815 | switch (ix) { |
| 816 | case 16: |
| 817 | VP8LPutBits(bw, extra_bits, 2); |
| 818 | break; |
| 819 | case 17: |
| 820 | VP8LPutBits(bw, extra_bits, 3); |
| 821 | break; |
| 822 | case 18: |
| 823 | VP8LPutBits(bw, extra_bits, 7); |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // 'huff_tree' and 'tokens' are pre-alloacted buffers. |
| 830 | static void StoreFullHuffmanCode(VP8LBitWriter* const bw, |
| 831 | HuffmanTree* const huff_tree, |
| 832 | HuffmanTreeToken* const tokens, |
| 833 | const HuffmanTreeCode* const tree) { |
| 834 | uint8_t code_length_bitdepth[CODE_LENGTH_CODES] = { 0 }; |
| 835 | uint16_t code_length_bitdepth_symbols[CODE_LENGTH_CODES] = { 0 }; |
| 836 | const int max_tokens = tree->num_symbols; |
| 837 | int num_tokens; |
| 838 | HuffmanTreeCode huffman_code; |
| 839 | huffman_code.num_symbols = CODE_LENGTH_CODES; |
| 840 | huffman_code.code_lengths = code_length_bitdepth; |
| 841 | huffman_code.codes = code_length_bitdepth_symbols; |
| 842 | |
| 843 | VP8LPutBits(bw, 0, 1); |
| 844 | num_tokens = VP8LCreateCompressedHuffmanTree(tree, tokens, max_tokens); |
| 845 | { |
| 846 | uint32_t histogram[CODE_LENGTH_CODES] = { 0 }; |
| 847 | uint8_t buf_rle[CODE_LENGTH_CODES] = { 0 }; |
| 848 | int i; |
| 849 | for (i = 0; i < num_tokens; ++i) { |
| 850 | ++histogram[tokens[i].code]; |
| 851 | } |
| 852 | |
| 853 | VP8LCreateHuffmanTree(histogram, 7, buf_rle, huff_tree, &huffman_code); |
| 854 | } |
| 855 | |
| 856 | StoreHuffmanTreeOfHuffmanTreeToBitMask(bw, code_length_bitdepth); |
| 857 | ClearHuffmanTreeIfOnlyOneSymbol(&huffman_code); |
| 858 | { |
| 859 | int trailing_zero_bits = 0; |
| 860 | int trimmed_length = num_tokens; |
| 861 | int write_trimmed_length; |
| 862 | int length; |
| 863 | int i = num_tokens; |
| 864 | while (i-- > 0) { |
| 865 | const int ix = tokens[i].code; |
| 866 | if (ix == 0 || ix == 17 || ix == 18) { |
| 867 | --trimmed_length; // discount trailing zeros |
| 868 | trailing_zero_bits += code_length_bitdepth[ix]; |
| 869 | if (ix == 17) { |
| 870 | trailing_zero_bits += 3; |
| 871 | } else if (ix == 18) { |
| 872 | trailing_zero_bits += 7; |
| 873 | } |
| 874 | } else { |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | write_trimmed_length = (trimmed_length > 1 && trailing_zero_bits > 12); |
| 879 | length = write_trimmed_length ? trimmed_length : num_tokens; |
| 880 | VP8LPutBits(bw, write_trimmed_length, 1); |
| 881 | if (write_trimmed_length) { |
| 882 | if (trimmed_length == 2) { |
| 883 | VP8LPutBits(bw, 0, 3 + 2); // nbitpairs=1, trimmed_length=2 |
| 884 | } else { |
| 885 | const int nbits = BitsLog2Floor(trimmed_length - 2); |
| 886 | const int nbitpairs = nbits / 2 + 1; |
| 887 | assert(trimmed_length > 2); |
| 888 | assert(nbitpairs - 1 < 8); |
| 889 | VP8LPutBits(bw, nbitpairs - 1, 3); |
| 890 | VP8LPutBits(bw, trimmed_length - 2, nbitpairs * 2); |
| 891 | } |
| 892 | } |
| 893 | StoreHuffmanTreeToBitMask(bw, tokens, length, &huffman_code); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | // 'huff_tree' and 'tokens' are pre-alloacted buffers. |
| 898 | static void StoreHuffmanCode(VP8LBitWriter* const bw, |
| 899 | HuffmanTree* const huff_tree, |
| 900 | HuffmanTreeToken* const tokens, |
| 901 | const HuffmanTreeCode* const huffman_code) { |
| 902 | int i; |
| 903 | int count = 0; |
| 904 | int symbols[2] = { 0, 0 }; |
| 905 | const int kMaxBits = 8; |
| 906 | const int kMaxSymbol = 1 << kMaxBits; |
| 907 | |
| 908 | // Check whether it's a small tree. |
| 909 | for (i = 0; i < huffman_code->num_symbols && count < 3; ++i) { |
| 910 | if (huffman_code->code_lengths[i] != 0) { |
| 911 | if (count < 2) symbols[count] = i; |
| 912 | ++count; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (count == 0) { // emit minimal tree for empty cases |
| 917 | // bits: small tree marker: 1, count-1: 0, large 8-bit code: 0, code: 0 |
| 918 | VP8LPutBits(bw, 0x01, 4); |
| 919 | } else if (count <= 2 && symbols[0] < kMaxSymbol && symbols[1] < kMaxSymbol) { |
| 920 | VP8LPutBits(bw, 1, 1); // Small tree marker to encode 1 or 2 symbols. |
| 921 | VP8LPutBits(bw, count - 1, 1); |
| 922 | if (symbols[0] <= 1) { |
| 923 | VP8LPutBits(bw, 0, 1); // Code bit for small (1 bit) symbol value. |
| 924 | VP8LPutBits(bw, symbols[0], 1); |
| 925 | } else { |
| 926 | VP8LPutBits(bw, 1, 1); |
| 927 | VP8LPutBits(bw, symbols[0], 8); |
| 928 | } |
| 929 | if (count == 2) { |
| 930 | VP8LPutBits(bw, symbols[1], 8); |
| 931 | } |
| 932 | } else { |
| 933 | StoreFullHuffmanCode(bw, huff_tree, tokens, huffman_code); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | static WEBP_INLINE void WriteHuffmanCode(VP8LBitWriter* const bw, |
| 938 | const HuffmanTreeCode* const code, |
| 939 | int code_index) { |
| 940 | const int depth = code->code_lengths[code_index]; |
| 941 | const int symbol = code->codes[code_index]; |
| 942 | VP8LPutBits(bw, symbol, depth); |
| 943 | } |
| 944 | |
| 945 | static WEBP_INLINE void ( |
| 946 | VP8LBitWriter* const bw, |
| 947 | const HuffmanTreeCode* const code, |
| 948 | int code_index, |
| 949 | int bits, |
| 950 | int n_bits) { |
| 951 | const int depth = code->code_lengths[code_index]; |
| 952 | const int symbol = code->codes[code_index]; |
| 953 | VP8LPutBits(bw, (bits << depth) | symbol, depth + n_bits); |
| 954 | } |
| 955 | |
| 956 | static int StoreImageToBitMask( |
| 957 | VP8LBitWriter* const bw, int width, int histo_bits, |
| 958 | const VP8LBackwardRefs* const refs, |
| 959 | const uint16_t* histogram_symbols, |
| 960 | const HuffmanTreeCode* const huffman_codes, const WebPPicture* const pic) { |
| 961 | const int histo_xsize = histo_bits ? VP8LSubSampleSize(width, histo_bits) : 1; |
| 962 | const int tile_mask = (histo_bits == 0) ? 0 : -(1 << histo_bits); |
| 963 | // x and y trace the position in the image. |
| 964 | int x = 0; |
| 965 | int y = 0; |
| 966 | int tile_x = x & tile_mask; |
| 967 | int tile_y = y & tile_mask; |
| 968 | int histogram_ix = histogram_symbols[0]; |
| 969 | const HuffmanTreeCode* codes = huffman_codes + 5 * histogram_ix; |
| 970 | VP8LRefsCursor c = VP8LRefsCursorInit(refs); |
| 971 | while (VP8LRefsCursorOk(&c)) { |
| 972 | const PixOrCopy* const v = c.cur_pos; |
| 973 | if ((tile_x != (x & tile_mask)) || (tile_y != (y & tile_mask))) { |
| 974 | tile_x = x & tile_mask; |
| 975 | tile_y = y & tile_mask; |
| 976 | histogram_ix = histogram_symbols[(y >> histo_bits) * histo_xsize + |
| 977 | (x >> histo_bits)]; |
| 978 | codes = huffman_codes + 5 * histogram_ix; |
| 979 | } |
| 980 | if (PixOrCopyIsLiteral(v)) { |
| 981 | static const uint8_t order[] = { 1, 2, 0, 3 }; |
| 982 | int k; |
| 983 | for (k = 0; k < 4; ++k) { |
| 984 | const int code = PixOrCopyLiteral(v, order[k]); |
| 985 | WriteHuffmanCode(bw, codes + k, code); |
| 986 | } |
| 987 | } else if (PixOrCopyIsCacheIdx(v)) { |
| 988 | const int code = PixOrCopyCacheIdx(v); |
| 989 | const int literal_ix = 256 + NUM_LENGTH_CODES + code; |
| 990 | WriteHuffmanCode(bw, codes, literal_ix); |
| 991 | } else { |
| 992 | int bits, n_bits; |
| 993 | int code; |
| 994 | |
| 995 | const int distance = PixOrCopyDistance(v); |
| 996 | VP8LPrefixEncode(v->len, &code, &n_bits, &bits); |
| 997 | WriteHuffmanCodeWithExtraBits(bw, codes, 256 + code, bits, n_bits); |
| 998 | |
| 999 | // Don't write the distance with the extra bits code since |
| 1000 | // the distance can be up to 18 bits of extra bits, and the prefix |
| 1001 | // 15 bits, totaling to 33, and our PutBits only supports up to 32 bits. |
| 1002 | VP8LPrefixEncode(distance, &code, &n_bits, &bits); |
| 1003 | WriteHuffmanCode(bw, codes + 4, code); |
| 1004 | VP8LPutBits(bw, bits, n_bits); |
| 1005 | } |
| 1006 | x += PixOrCopyLength(v); |
| 1007 | while (x >= width) { |
| 1008 | x -= width; |
| 1009 | ++y; |
| 1010 | } |
| 1011 | VP8LRefsCursorNext(&c); |
| 1012 | } |
| 1013 | if (bw->error_) { |
| 1014 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1015 | } |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
| 1019 | // Special case of EncodeImageInternal() for cache-bits=0, histo_bits=31. |
| 1020 | // pic and percent are for progress. |
| 1021 | static int EncodeImageNoHuffman(VP8LBitWriter* const bw, |
| 1022 | const uint32_t* const argb, |
| 1023 | VP8LHashChain* const hash_chain, |
| 1024 | VP8LBackwardRefs* const refs_array, int width, |
| 1025 | int height, int quality, int low_effort, |
| 1026 | const WebPPicture* const pic, int percent_range, |
| 1027 | int* const percent) { |
| 1028 | int i; |
| 1029 | int max_tokens = 0; |
| 1030 | VP8LBackwardRefs* refs; |
| 1031 | HuffmanTreeToken* tokens = NULL; |
| 1032 | HuffmanTreeCode huffman_codes[5] = {{0, NULL, NULL}}; |
| 1033 | const uint16_t histogram_symbols[1] = {0}; // only one tree, one symbol |
| 1034 | int cache_bits = 0; |
| 1035 | VP8LHistogramSet* histogram_image = NULL; |
| 1036 | HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc( |
| 1037 | 3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree)); |
| 1038 | if (huff_tree == NULL) { |
| 1039 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1040 | goto Error; |
| 1041 | } |
| 1042 | |
| 1043 | // Calculate backward references from ARGB image. |
| 1044 | if (!VP8LHashChainFill(hash_chain, quality, argb, width, height, low_effort, |
| 1045 | pic, percent_range / 2, percent)) { |
| 1046 | goto Error; |
| 1047 | } |
| 1048 | if (!VP8LGetBackwardReferences(width, height, argb, quality, /*low_effort=*/0, |
| 1049 | kLZ77Standard | kLZ77RLE, cache_bits, |
| 1050 | /*do_no_cache=*/0, hash_chain, refs_array, |
| 1051 | &cache_bits, pic, |
| 1052 | percent_range - percent_range / 2, percent)) { |
| 1053 | goto Error; |
| 1054 | } |
| 1055 | refs = &refs_array[0]; |
| 1056 | histogram_image = VP8LAllocateHistogramSet(1, cache_bits); |
| 1057 | if (histogram_image == NULL) { |
| 1058 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1059 | goto Error; |
| 1060 | } |
| 1061 | VP8LHistogramSetClear(histogram_image); |
| 1062 | |
| 1063 | // Build histogram image and symbols from backward references. |
| 1064 | VP8LHistogramStoreRefs(refs, histogram_image->histograms[0]); |
| 1065 | |
| 1066 | // Create Huffman bit lengths and codes for each histogram image. |
| 1067 | assert(histogram_image->size == 1); |
| 1068 | if (!GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) { |
| 1069 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1070 | goto Error; |
| 1071 | } |
| 1072 | |
| 1073 | // No color cache, no Huffman image. |
| 1074 | VP8LPutBits(bw, 0, 1); |
| 1075 | |
| 1076 | // Find maximum number of symbols for the huffman tree-set. |
| 1077 | for (i = 0; i < 5; ++i) { |
| 1078 | HuffmanTreeCode* const codes = &huffman_codes[i]; |
| 1079 | if (max_tokens < codes->num_symbols) { |
| 1080 | max_tokens = codes->num_symbols; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens)); |
| 1085 | if (tokens == NULL) { |
| 1086 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1087 | goto Error; |
| 1088 | } |
| 1089 | |
| 1090 | // Store Huffman codes. |
| 1091 | for (i = 0; i < 5; ++i) { |
| 1092 | HuffmanTreeCode* const codes = &huffman_codes[i]; |
| 1093 | StoreHuffmanCode(bw, huff_tree, tokens, codes); |
| 1094 | ClearHuffmanTreeIfOnlyOneSymbol(codes); |
| 1095 | } |
| 1096 | |
| 1097 | // Store actual literals. |
| 1098 | if (!StoreImageToBitMask(bw, width, 0, refs, histogram_symbols, huffman_codes, |
| 1099 | pic)) { |
| 1100 | goto Error; |
| 1101 | } |
| 1102 | |
| 1103 | Error: |
| 1104 | WebPSafeFree(tokens); |
| 1105 | WebPSafeFree(huff_tree); |
| 1106 | VP8LFreeHistogramSet(histogram_image); |
| 1107 | WebPSafeFree(huffman_codes[0].codes); |
| 1108 | return (pic->error_code == VP8_ENC_OK); |
| 1109 | } |
| 1110 | |
| 1111 | // pic and percent are for progress. |
| 1112 | static int EncodeImageInternal( |
| 1113 | VP8LBitWriter* const bw, const uint32_t* const argb, |
| 1114 | VP8LHashChain* const hash_chain, VP8LBackwardRefs refs_array[4], int width, |
| 1115 | int height, int quality, int low_effort, int use_cache, |
| 1116 | const CrunchConfig* const config, int* cache_bits, int histogram_bits, |
| 1117 | size_t init_byte_position, int* const hdr_size, int* const data_size, |
| 1118 | const WebPPicture* const pic, int percent_range, int* const percent) { |
| 1119 | const uint32_t histogram_image_xysize = |
| 1120 | VP8LSubSampleSize(width, histogram_bits) * |
| 1121 | VP8LSubSampleSize(height, histogram_bits); |
| 1122 | int remaining_percent = percent_range; |
| 1123 | int percent_start = *percent; |
| 1124 | VP8LHistogramSet* histogram_image = NULL; |
| 1125 | VP8LHistogram* tmp_histo = NULL; |
| 1126 | int histogram_image_size = 0; |
| 1127 | size_t bit_array_size = 0; |
| 1128 | HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc( |
| 1129 | 3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree)); |
| 1130 | HuffmanTreeToken* tokens = NULL; |
| 1131 | HuffmanTreeCode* huffman_codes = NULL; |
| 1132 | uint16_t* const histogram_symbols = (uint16_t*)WebPSafeMalloc( |
| 1133 | histogram_image_xysize, sizeof(*histogram_symbols)); |
| 1134 | int sub_configs_idx; |
| 1135 | int cache_bits_init, write_histogram_image; |
| 1136 | VP8LBitWriter bw_init = *bw, bw_best; |
| 1137 | int hdr_size_tmp; |
| 1138 | VP8LHashChain hash_chain_histogram; // histogram image hash chain |
| 1139 | size_t bw_size_best = ~(size_t)0; |
| 1140 | assert(histogram_bits >= MIN_HUFFMAN_BITS); |
| 1141 | assert(histogram_bits <= MAX_HUFFMAN_BITS); |
| 1142 | assert(hdr_size != NULL); |
| 1143 | assert(data_size != NULL); |
| 1144 | |
| 1145 | memset(&hash_chain_histogram, 0, sizeof(hash_chain_histogram)); |
| 1146 | if (!VP8LBitWriterInit(&bw_best, 0)) { |
| 1147 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1148 | goto Error; |
| 1149 | } |
| 1150 | |
| 1151 | // Make sure we can allocate the different objects. |
| 1152 | if (huff_tree == NULL || histogram_symbols == NULL || |
| 1153 | !VP8LHashChainInit(&hash_chain_histogram, histogram_image_xysize)) { |
| 1154 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1155 | goto Error; |
| 1156 | } |
| 1157 | |
| 1158 | percent_range = remaining_percent / 5; |
| 1159 | if (!VP8LHashChainFill(hash_chain, quality, argb, width, height, |
| 1160 | low_effort, pic, percent_range, percent)) { |
| 1161 | goto Error; |
| 1162 | } |
| 1163 | percent_start += percent_range; |
| 1164 | remaining_percent -= percent_range; |
| 1165 | |
| 1166 | if (use_cache) { |
| 1167 | // If the value is different from zero, it has been set during the |
| 1168 | // palette analysis. |
| 1169 | cache_bits_init = (*cache_bits == 0) ? MAX_COLOR_CACHE_BITS : *cache_bits; |
| 1170 | } else { |
| 1171 | cache_bits_init = 0; |
| 1172 | } |
| 1173 | // If several iterations will happen, clone into bw_best. |
| 1174 | if ((config->sub_configs_size_ > 1 || config->sub_configs_[0].do_no_cache_) && |
| 1175 | !VP8LBitWriterClone(bw, &bw_best)) { |
| 1176 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1177 | goto Error; |
| 1178 | } |
| 1179 | |
| 1180 | for (sub_configs_idx = 0; sub_configs_idx < config->sub_configs_size_; |
| 1181 | ++sub_configs_idx) { |
| 1182 | const CrunchSubConfig* const sub_config = |
| 1183 | &config->sub_configs_[sub_configs_idx]; |
| 1184 | int cache_bits_best, i_cache; |
| 1185 | int i_remaining_percent = remaining_percent / config->sub_configs_size_; |
| 1186 | int i_percent_range = i_remaining_percent / 4; |
| 1187 | i_remaining_percent -= i_percent_range; |
| 1188 | |
| 1189 | if (!VP8LGetBackwardReferences( |
| 1190 | width, height, argb, quality, low_effort, sub_config->lz77_, |
| 1191 | cache_bits_init, sub_config->do_no_cache_, hash_chain, |
| 1192 | &refs_array[0], &cache_bits_best, pic, i_percent_range, percent)) { |
| 1193 | goto Error; |
| 1194 | } |
| 1195 | |
| 1196 | for (i_cache = 0; i_cache < (sub_config->do_no_cache_ ? 2 : 1); ++i_cache) { |
| 1197 | const int cache_bits_tmp = (i_cache == 0) ? cache_bits_best : 0; |
| 1198 | // Speed-up: no need to study the no-cache case if it was already studied |
| 1199 | // in i_cache == 0. |
| 1200 | if (i_cache == 1 && cache_bits_best == 0) break; |
| 1201 | |
| 1202 | // Reset the bit writer for this iteration. |
| 1203 | VP8LBitWriterReset(&bw_init, bw); |
| 1204 | |
| 1205 | // Build histogram image and symbols from backward references. |
| 1206 | histogram_image = |
| 1207 | VP8LAllocateHistogramSet(histogram_image_xysize, cache_bits_tmp); |
| 1208 | tmp_histo = VP8LAllocateHistogram(cache_bits_tmp); |
| 1209 | if (histogram_image == NULL || tmp_histo == NULL) { |
| 1210 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1211 | goto Error; |
| 1212 | } |
| 1213 | |
| 1214 | i_percent_range = i_remaining_percent / 3; |
| 1215 | i_remaining_percent -= i_percent_range; |
| 1216 | if (!VP8LGetHistoImageSymbols( |
| 1217 | width, height, &refs_array[i_cache], quality, low_effort, |
| 1218 | histogram_bits, cache_bits_tmp, histogram_image, tmp_histo, |
| 1219 | histogram_symbols, pic, i_percent_range, percent)) { |
| 1220 | goto Error; |
| 1221 | } |
| 1222 | // Create Huffman bit lengths and codes for each histogram image. |
| 1223 | histogram_image_size = histogram_image->size; |
| 1224 | bit_array_size = 5 * histogram_image_size; |
| 1225 | huffman_codes = (HuffmanTreeCode*)WebPSafeCalloc(bit_array_size, |
| 1226 | sizeof(*huffman_codes)); |
| 1227 | // Note: some histogram_image entries may point to tmp_histos[], so the |
| 1228 | // latter need to outlive the following call to |
| 1229 | // GetHuffBitLengthsAndCodes(). |
| 1230 | if (huffman_codes == NULL || |
| 1231 | !GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) { |
| 1232 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1233 | goto Error; |
| 1234 | } |
| 1235 | // Free combined histograms. |
| 1236 | VP8LFreeHistogramSet(histogram_image); |
| 1237 | histogram_image = NULL; |
| 1238 | |
| 1239 | // Free scratch histograms. |
| 1240 | VP8LFreeHistogram(tmp_histo); |
| 1241 | tmp_histo = NULL; |
| 1242 | |
| 1243 | // Color Cache parameters. |
| 1244 | if (cache_bits_tmp > 0) { |
| 1245 | VP8LPutBits(bw, 1, 1); |
| 1246 | VP8LPutBits(bw, cache_bits_tmp, 4); |
| 1247 | } else { |
| 1248 | VP8LPutBits(bw, 0, 1); |
| 1249 | } |
| 1250 | |
| 1251 | // Huffman image + meta huffman. |
| 1252 | write_histogram_image = (histogram_image_size > 1); |
| 1253 | VP8LPutBits(bw, write_histogram_image, 1); |
| 1254 | if (write_histogram_image) { |
| 1255 | uint32_t* const histogram_argb = (uint32_t*)WebPSafeMalloc( |
| 1256 | histogram_image_xysize, sizeof(*histogram_argb)); |
| 1257 | int max_index = 0; |
| 1258 | uint32_t i; |
| 1259 | if (histogram_argb == NULL) { |
| 1260 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1261 | goto Error; |
| 1262 | } |
| 1263 | for (i = 0; i < histogram_image_xysize; ++i) { |
| 1264 | const int symbol_index = histogram_symbols[i] & 0xffff; |
| 1265 | histogram_argb[i] = (symbol_index << 8); |
| 1266 | if (symbol_index >= max_index) { |
| 1267 | max_index = symbol_index + 1; |
| 1268 | } |
| 1269 | } |
| 1270 | histogram_image_size = max_index; |
| 1271 | |
| 1272 | VP8LPutBits(bw, histogram_bits - 2, 3); |
| 1273 | i_percent_range = i_remaining_percent / 2; |
| 1274 | i_remaining_percent -= i_percent_range; |
| 1275 | if (!EncodeImageNoHuffman( |
| 1276 | bw, histogram_argb, &hash_chain_histogram, &refs_array[2], |
| 1277 | VP8LSubSampleSize(width, histogram_bits), |
| 1278 | VP8LSubSampleSize(height, histogram_bits), quality, low_effort, |
| 1279 | pic, i_percent_range, percent)) { |
| 1280 | WebPSafeFree(histogram_argb); |
| 1281 | goto Error; |
| 1282 | } |
| 1283 | WebPSafeFree(histogram_argb); |
| 1284 | } |
| 1285 | |
| 1286 | // Store Huffman codes. |
| 1287 | { |
| 1288 | int i; |
| 1289 | int max_tokens = 0; |
| 1290 | // Find maximum number of symbols for the huffman tree-set. |
| 1291 | for (i = 0; i < 5 * histogram_image_size; ++i) { |
| 1292 | HuffmanTreeCode* const codes = &huffman_codes[i]; |
| 1293 | if (max_tokens < codes->num_symbols) { |
| 1294 | max_tokens = codes->num_symbols; |
| 1295 | } |
| 1296 | } |
| 1297 | tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens)); |
| 1298 | if (tokens == NULL) { |
| 1299 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1300 | goto Error; |
| 1301 | } |
| 1302 | for (i = 0; i < 5 * histogram_image_size; ++i) { |
| 1303 | HuffmanTreeCode* const codes = &huffman_codes[i]; |
| 1304 | StoreHuffmanCode(bw, huff_tree, tokens, codes); |
| 1305 | ClearHuffmanTreeIfOnlyOneSymbol(codes); |
| 1306 | } |
| 1307 | } |
| 1308 | // Store actual literals. |
| 1309 | hdr_size_tmp = (int)(VP8LBitWriterNumBytes(bw) - init_byte_position); |
| 1310 | if (!StoreImageToBitMask(bw, width, histogram_bits, &refs_array[i_cache], |
| 1311 | histogram_symbols, huffman_codes, pic)) { |
| 1312 | goto Error; |
| 1313 | } |
| 1314 | // Keep track of the smallest image so far. |
| 1315 | if (VP8LBitWriterNumBytes(bw) < bw_size_best) { |
| 1316 | bw_size_best = VP8LBitWriterNumBytes(bw); |
| 1317 | *cache_bits = cache_bits_tmp; |
| 1318 | *hdr_size = hdr_size_tmp; |
| 1319 | *data_size = |
| 1320 | (int)(VP8LBitWriterNumBytes(bw) - init_byte_position - *hdr_size); |
| 1321 | VP8LBitWriterSwap(bw, &bw_best); |
| 1322 | } |
| 1323 | WebPSafeFree(tokens); |
| 1324 | tokens = NULL; |
| 1325 | if (huffman_codes != NULL) { |
| 1326 | WebPSafeFree(huffman_codes->codes); |
| 1327 | WebPSafeFree(huffman_codes); |
| 1328 | huffman_codes = NULL; |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | VP8LBitWriterSwap(bw, &bw_best); |
| 1333 | |
| 1334 | if (!WebPReportProgress(pic, percent_start + remaining_percent, percent)) { |
| 1335 | goto Error; |
| 1336 | } |
| 1337 | |
| 1338 | Error: |
| 1339 | WebPSafeFree(tokens); |
| 1340 | WebPSafeFree(huff_tree); |
| 1341 | VP8LFreeHistogramSet(histogram_image); |
| 1342 | VP8LFreeHistogram(tmp_histo); |
| 1343 | VP8LHashChainClear(&hash_chain_histogram); |
| 1344 | if (huffman_codes != NULL) { |
| 1345 | WebPSafeFree(huffman_codes->codes); |
| 1346 | WebPSafeFree(huffman_codes); |
| 1347 | } |
| 1348 | WebPSafeFree(histogram_symbols); |
| 1349 | VP8LBitWriterWipeOut(&bw_best); |
| 1350 | return (pic->error_code == VP8_ENC_OK); |
| 1351 | } |
| 1352 | |
| 1353 | // ----------------------------------------------------------------------------- |
| 1354 | // Transforms |
| 1355 | |
| 1356 | static void ApplySubtractGreen(VP8LEncoder* const enc, int width, int height, |
| 1357 | VP8LBitWriter* const bw) { |
| 1358 | VP8LPutBits(bw, TRANSFORM_PRESENT, 1); |
| 1359 | VP8LPutBits(bw, SUBTRACT_GREEN_TRANSFORM, 2); |
| 1360 | VP8LSubtractGreenFromBlueAndRed(enc->argb_, width * height); |
| 1361 | } |
| 1362 | |
| 1363 | static int ApplyPredictFilter(const VP8LEncoder* const enc, int width, |
| 1364 | int height, int quality, int low_effort, |
| 1365 | int used_subtract_green, VP8LBitWriter* const bw, |
| 1366 | int percent_range, int* const percent) { |
| 1367 | const int pred_bits = enc->transform_bits_; |
| 1368 | const int transform_width = VP8LSubSampleSize(width, pred_bits); |
| 1369 | const int transform_height = VP8LSubSampleSize(height, pred_bits); |
| 1370 | // we disable near-lossless quantization if palette is used. |
| 1371 | const int near_lossless_strength = |
| 1372 | enc->use_palette_ ? 100 : enc->config_->near_lossless; |
| 1373 | |
| 1374 | if (!VP8LResidualImage( |
| 1375 | width, height, pred_bits, low_effort, enc->argb_, enc->argb_scratch_, |
| 1376 | enc->transform_data_, near_lossless_strength, enc->config_->exact, |
| 1377 | used_subtract_green, enc->pic_, percent_range / 2, percent)) { |
| 1378 | return 0; |
| 1379 | } |
| 1380 | VP8LPutBits(bw, TRANSFORM_PRESENT, 1); |
| 1381 | VP8LPutBits(bw, PREDICTOR_TRANSFORM, 2); |
| 1382 | assert(pred_bits >= 2); |
| 1383 | VP8LPutBits(bw, pred_bits - 2, 3); |
| 1384 | return EncodeImageNoHuffman( |
| 1385 | bw, enc->transform_data_, (VP8LHashChain*)&enc->hash_chain_, |
| 1386 | (VP8LBackwardRefs*)&enc->refs_[0], transform_width, transform_height, |
| 1387 | quality, low_effort, enc->pic_, percent_range - percent_range / 2, |
| 1388 | percent); |
| 1389 | } |
| 1390 | |
| 1391 | static int ApplyCrossColorFilter(const VP8LEncoder* const enc, int width, |
| 1392 | int height, int quality, int low_effort, |
| 1393 | VP8LBitWriter* const bw, int percent_range, |
| 1394 | int* const percent) { |
| 1395 | const int ccolor_transform_bits = enc->transform_bits_; |
| 1396 | const int transform_width = VP8LSubSampleSize(width, ccolor_transform_bits); |
| 1397 | const int transform_height = VP8LSubSampleSize(height, ccolor_transform_bits); |
| 1398 | |
| 1399 | if (!VP8LColorSpaceTransform(width, height, ccolor_transform_bits, quality, |
| 1400 | enc->argb_, enc->transform_data_, enc->pic_, |
| 1401 | percent_range / 2, percent)) { |
| 1402 | return 0; |
| 1403 | } |
| 1404 | VP8LPutBits(bw, TRANSFORM_PRESENT, 1); |
| 1405 | VP8LPutBits(bw, CROSS_COLOR_TRANSFORM, 2); |
| 1406 | assert(ccolor_transform_bits >= 2); |
| 1407 | VP8LPutBits(bw, ccolor_transform_bits - 2, 3); |
| 1408 | return EncodeImageNoHuffman( |
| 1409 | bw, enc->transform_data_, (VP8LHashChain*)&enc->hash_chain_, |
| 1410 | (VP8LBackwardRefs*)&enc->refs_[0], transform_width, transform_height, |
| 1411 | quality, low_effort, enc->pic_, percent_range - percent_range / 2, |
| 1412 | percent); |
| 1413 | } |
| 1414 | |
| 1415 | // ----------------------------------------------------------------------------- |
| 1416 | |
| 1417 | static int (const WebPPicture* const pic, size_t riff_size, |
| 1418 | size_t vp8l_size) { |
| 1419 | uint8_t riff[RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE + VP8L_SIGNATURE_SIZE] = { |
| 1420 | 'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P', |
| 1421 | 'V', 'P', '8', 'L', 0, 0, 0, 0, VP8L_MAGIC_BYTE, |
| 1422 | }; |
| 1423 | PutLE32(riff + TAG_SIZE, (uint32_t)riff_size); |
| 1424 | PutLE32(riff + RIFF_HEADER_SIZE + TAG_SIZE, (uint32_t)vp8l_size); |
| 1425 | return pic->writer(riff, sizeof(riff), pic); |
| 1426 | } |
| 1427 | |
| 1428 | static int WriteImageSize(const WebPPicture* const pic, |
| 1429 | VP8LBitWriter* const bw) { |
| 1430 | const int width = pic->width - 1; |
| 1431 | const int height = pic->height - 1; |
| 1432 | assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION); |
| 1433 | |
| 1434 | VP8LPutBits(bw, width, VP8L_IMAGE_SIZE_BITS); |
| 1435 | VP8LPutBits(bw, height, VP8L_IMAGE_SIZE_BITS); |
| 1436 | return !bw->error_; |
| 1437 | } |
| 1438 | |
| 1439 | static int WriteRealAlphaAndVersion(VP8LBitWriter* const bw, int has_alpha) { |
| 1440 | VP8LPutBits(bw, has_alpha, 1); |
| 1441 | VP8LPutBits(bw, VP8L_VERSION, VP8L_VERSION_BITS); |
| 1442 | return !bw->error_; |
| 1443 | } |
| 1444 | |
| 1445 | static int WriteImage(const WebPPicture* const pic, VP8LBitWriter* const bw, |
| 1446 | size_t* const coded_size) { |
| 1447 | const uint8_t* const webpll_data = VP8LBitWriterFinish(bw); |
| 1448 | const size_t webpll_size = VP8LBitWriterNumBytes(bw); |
| 1449 | const size_t vp8l_size = VP8L_SIGNATURE_SIZE + webpll_size; |
| 1450 | const size_t pad = vp8l_size & 1; |
| 1451 | const size_t riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8l_size + pad; |
| 1452 | *coded_size = 0; |
| 1453 | |
| 1454 | if (bw->error_) { |
| 1455 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1456 | } |
| 1457 | |
| 1458 | if (!WriteRiffHeader(pic, riff_size, vp8l_size) || |
| 1459 | !pic->writer(webpll_data, webpll_size, pic)) { |
| 1460 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE); |
| 1461 | } |
| 1462 | |
| 1463 | if (pad) { |
| 1464 | const uint8_t pad_byte[1] = { 0 }; |
| 1465 | if (!pic->writer(pad_byte, 1, pic)) { |
| 1466 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE); |
| 1467 | } |
| 1468 | } |
| 1469 | *coded_size = CHUNK_HEADER_SIZE + riff_size; |
| 1470 | return 1; |
| 1471 | } |
| 1472 | |
| 1473 | // ----------------------------------------------------------------------------- |
| 1474 | |
| 1475 | static void ClearTransformBuffer(VP8LEncoder* const enc) { |
| 1476 | WebPSafeFree(enc->transform_mem_); |
| 1477 | enc->transform_mem_ = NULL; |
| 1478 | enc->transform_mem_size_ = 0; |
| 1479 | } |
| 1480 | |
| 1481 | // Allocates the memory for argb (W x H) buffer, 2 rows of context for |
| 1482 | // prediction and transform data. |
| 1483 | // Flags influencing the memory allocated: |
| 1484 | // enc->transform_bits_ |
| 1485 | // enc->use_predict_, enc->use_cross_color_ |
| 1486 | static int AllocateTransformBuffer(VP8LEncoder* const enc, int width, |
| 1487 | int height) { |
| 1488 | const uint64_t image_size = width * height; |
| 1489 | // VP8LResidualImage needs room for 2 scanlines of uint32 pixels with an extra |
| 1490 | // pixel in each, plus 2 regular scanlines of bytes. |
| 1491 | // TODO(skal): Clean up by using arithmetic in bytes instead of words. |
| 1492 | const uint64_t argb_scratch_size = |
| 1493 | enc->use_predict_ ? (width + 1) * 2 + (width * 2 + sizeof(uint32_t) - 1) / |
| 1494 | sizeof(uint32_t) |
| 1495 | : 0; |
| 1496 | const uint64_t transform_data_size = |
| 1497 | (enc->use_predict_ || enc->use_cross_color_) |
| 1498 | ? VP8LSubSampleSize(width, enc->transform_bits_) * |
| 1499 | VP8LSubSampleSize(height, enc->transform_bits_) |
| 1500 | : 0; |
| 1501 | const uint64_t max_alignment_in_words = |
| 1502 | (WEBP_ALIGN_CST + sizeof(uint32_t) - 1) / sizeof(uint32_t); |
| 1503 | const uint64_t mem_size = image_size + max_alignment_in_words + |
| 1504 | argb_scratch_size + max_alignment_in_words + |
| 1505 | transform_data_size; |
| 1506 | uint32_t* mem = enc->transform_mem_; |
| 1507 | if (mem == NULL || mem_size > enc->transform_mem_size_) { |
| 1508 | ClearTransformBuffer(enc); |
| 1509 | mem = (uint32_t*)WebPSafeMalloc(mem_size, sizeof(*mem)); |
| 1510 | if (mem == NULL) { |
| 1511 | return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1512 | } |
| 1513 | enc->transform_mem_ = mem; |
| 1514 | enc->transform_mem_size_ = (size_t)mem_size; |
| 1515 | enc->argb_content_ = kEncoderNone; |
| 1516 | } |
| 1517 | enc->argb_ = mem; |
| 1518 | mem = (uint32_t*)WEBP_ALIGN(mem + image_size); |
| 1519 | enc->argb_scratch_ = mem; |
| 1520 | mem = (uint32_t*)WEBP_ALIGN(mem + argb_scratch_size); |
| 1521 | enc->transform_data_ = mem; |
| 1522 | |
| 1523 | enc->current_width_ = width; |
| 1524 | return 1; |
| 1525 | } |
| 1526 | |
| 1527 | static int MakeInputImageCopy(VP8LEncoder* const enc) { |
| 1528 | const WebPPicture* const picture = enc->pic_; |
| 1529 | const int width = picture->width; |
| 1530 | const int height = picture->height; |
| 1531 | |
| 1532 | if (!AllocateTransformBuffer(enc, width, height)) return 0; |
| 1533 | if (enc->argb_content_ == kEncoderARGB) return 1; |
| 1534 | |
| 1535 | { |
| 1536 | uint32_t* dst = enc->argb_; |
| 1537 | const uint32_t* src = picture->argb; |
| 1538 | int y; |
| 1539 | for (y = 0; y < height; ++y) { |
| 1540 | memcpy(dst, src, width * sizeof(*dst)); |
| 1541 | dst += width; |
| 1542 | src += picture->argb_stride; |
| 1543 | } |
| 1544 | } |
| 1545 | enc->argb_content_ = kEncoderARGB; |
| 1546 | assert(enc->current_width_ == width); |
| 1547 | return 1; |
| 1548 | } |
| 1549 | |
| 1550 | // ----------------------------------------------------------------------------- |
| 1551 | |
| 1552 | #define APPLY_PALETTE_GREEDY_MAX 4 |
| 1553 | |
| 1554 | static WEBP_INLINE uint32_t SearchColorGreedy(const uint32_t palette[], |
| 1555 | int palette_size, |
| 1556 | uint32_t color) { |
| 1557 | (void)palette_size; |
| 1558 | assert(palette_size < APPLY_PALETTE_GREEDY_MAX); |
| 1559 | assert(3 == APPLY_PALETTE_GREEDY_MAX - 1); |
| 1560 | if (color == palette[0]) return 0; |
| 1561 | if (color == palette[1]) return 1; |
| 1562 | if (color == palette[2]) return 2; |
| 1563 | return 3; |
| 1564 | } |
| 1565 | |
| 1566 | static WEBP_INLINE uint32_t ApplyPaletteHash0(uint32_t color) { |
| 1567 | // Focus on the green color. |
| 1568 | return (color >> 8) & 0xff; |
| 1569 | } |
| 1570 | |
| 1571 | #define PALETTE_INV_SIZE_BITS 11 |
| 1572 | #define PALETTE_INV_SIZE (1 << PALETTE_INV_SIZE_BITS) |
| 1573 | |
| 1574 | static WEBP_INLINE uint32_t ApplyPaletteHash1(uint32_t color) { |
| 1575 | // Forget about alpha. |
| 1576 | return ((uint32_t)((color & 0x00ffffffu) * 4222244071ull)) >> |
| 1577 | (32 - PALETTE_INV_SIZE_BITS); |
| 1578 | } |
| 1579 | |
| 1580 | static WEBP_INLINE uint32_t ApplyPaletteHash2(uint32_t color) { |
| 1581 | // Forget about alpha. |
| 1582 | return ((uint32_t)((color & 0x00ffffffu) * ((1ull << 31) - 1))) >> |
| 1583 | (32 - PALETTE_INV_SIZE_BITS); |
| 1584 | } |
| 1585 | |
| 1586 | // Use 1 pixel cache for ARGB pixels. |
| 1587 | #define APPLY_PALETTE_FOR(COLOR_INDEX) do { \ |
| 1588 | uint32_t prev_pix = palette[0]; \ |
| 1589 | uint32_t prev_idx = 0; \ |
| 1590 | for (y = 0; y < height; ++y) { \ |
| 1591 | for (x = 0; x < width; ++x) { \ |
| 1592 | const uint32_t pix = src[x]; \ |
| 1593 | if (pix != prev_pix) { \ |
| 1594 | prev_idx = COLOR_INDEX; \ |
| 1595 | prev_pix = pix; \ |
| 1596 | } \ |
| 1597 | tmp_row[x] = prev_idx; \ |
| 1598 | } \ |
| 1599 | VP8LBundleColorMap(tmp_row, width, xbits, dst); \ |
| 1600 | src += src_stride; \ |
| 1601 | dst += dst_stride; \ |
| 1602 | } \ |
| 1603 | } while (0) |
| 1604 | |
| 1605 | // Remap argb values in src[] to packed palettes entries in dst[] |
| 1606 | // using 'row' as a temporary buffer of size 'width'. |
| 1607 | // We assume that all src[] values have a corresponding entry in the palette. |
| 1608 | // Note: src[] can be the same as dst[] |
| 1609 | static int ApplyPalette(const uint32_t* src, uint32_t src_stride, uint32_t* dst, |
| 1610 | uint32_t dst_stride, const uint32_t* palette, |
| 1611 | int palette_size, int width, int height, int xbits, |
| 1612 | const WebPPicture* const pic) { |
| 1613 | // TODO(skal): this tmp buffer is not needed if VP8LBundleColorMap() can be |
| 1614 | // made to work in-place. |
| 1615 | uint8_t* const tmp_row = (uint8_t*)WebPSafeMalloc(width, sizeof(*tmp_row)); |
| 1616 | int x, y; |
| 1617 | |
| 1618 | if (tmp_row == NULL) { |
| 1619 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1620 | } |
| 1621 | |
| 1622 | if (palette_size < APPLY_PALETTE_GREEDY_MAX) { |
| 1623 | APPLY_PALETTE_FOR(SearchColorGreedy(palette, palette_size, pix)); |
| 1624 | } else { |
| 1625 | int i, j; |
| 1626 | uint16_t buffer[PALETTE_INV_SIZE]; |
| 1627 | uint32_t (*const hash_functions[])(uint32_t) = { |
| 1628 | ApplyPaletteHash0, ApplyPaletteHash1, ApplyPaletteHash2 |
| 1629 | }; |
| 1630 | |
| 1631 | // Try to find a perfect hash function able to go from a color to an index |
| 1632 | // within 1 << PALETTE_INV_SIZE_BITS in order to build a hash map to go |
| 1633 | // from color to index in palette. |
| 1634 | for (i = 0; i < 3; ++i) { |
| 1635 | int use_LUT = 1; |
| 1636 | // Set each element in buffer to max uint16_t. |
| 1637 | memset(buffer, 0xff, sizeof(buffer)); |
| 1638 | for (j = 0; j < palette_size; ++j) { |
| 1639 | const uint32_t ind = hash_functions[i](palette[j]); |
| 1640 | if (buffer[ind] != 0xffffu) { |
| 1641 | use_LUT = 0; |
| 1642 | break; |
| 1643 | } else { |
| 1644 | buffer[ind] = j; |
| 1645 | } |
| 1646 | } |
| 1647 | if (use_LUT) break; |
| 1648 | } |
| 1649 | |
| 1650 | if (i == 0) { |
| 1651 | APPLY_PALETTE_FOR(buffer[ApplyPaletteHash0(pix)]); |
| 1652 | } else if (i == 1) { |
| 1653 | APPLY_PALETTE_FOR(buffer[ApplyPaletteHash1(pix)]); |
| 1654 | } else if (i == 2) { |
| 1655 | APPLY_PALETTE_FOR(buffer[ApplyPaletteHash2(pix)]); |
| 1656 | } else { |
| 1657 | uint32_t idx_map[MAX_PALETTE_SIZE]; |
| 1658 | uint32_t palette_sorted[MAX_PALETTE_SIZE]; |
| 1659 | PrepareMapToPalette(palette, palette_size, palette_sorted, idx_map); |
| 1660 | APPLY_PALETTE_FOR( |
| 1661 | idx_map[SearchColorNoIdx(palette_sorted, pix, palette_size)]); |
| 1662 | } |
| 1663 | } |
| 1664 | WebPSafeFree(tmp_row); |
| 1665 | return 1; |
| 1666 | } |
| 1667 | #undef APPLY_PALETTE_FOR |
| 1668 | #undef PALETTE_INV_SIZE_BITS |
| 1669 | #undef PALETTE_INV_SIZE |
| 1670 | #undef APPLY_PALETTE_GREEDY_MAX |
| 1671 | |
| 1672 | // Note: Expects "enc->palette_" to be set properly. |
| 1673 | static int MapImageFromPalette(VP8LEncoder* const enc, int in_place) { |
| 1674 | const WebPPicture* const pic = enc->pic_; |
| 1675 | const int width = pic->width; |
| 1676 | const int height = pic->height; |
| 1677 | const uint32_t* const palette = enc->palette_; |
| 1678 | const uint32_t* src = in_place ? enc->argb_ : pic->argb; |
| 1679 | const int src_stride = in_place ? enc->current_width_ : pic->argb_stride; |
| 1680 | const int palette_size = enc->palette_size_; |
| 1681 | int xbits; |
| 1682 | |
| 1683 | // Replace each input pixel by corresponding palette index. |
| 1684 | // This is done line by line. |
| 1685 | if (palette_size <= 4) { |
| 1686 | xbits = (palette_size <= 2) ? 3 : 2; |
| 1687 | } else { |
| 1688 | xbits = (palette_size <= 16) ? 1 : 0; |
| 1689 | } |
| 1690 | |
| 1691 | if (!AllocateTransformBuffer(enc, VP8LSubSampleSize(width, xbits), height)) { |
| 1692 | return 0; |
| 1693 | } |
| 1694 | if (!ApplyPalette(src, src_stride, |
| 1695 | enc->argb_, enc->current_width_, |
| 1696 | palette, palette_size, width, height, xbits, pic)) { |
| 1697 | return 0; |
| 1698 | } |
| 1699 | enc->argb_content_ = kEncoderPalette; |
| 1700 | return 1; |
| 1701 | } |
| 1702 | |
| 1703 | // Save palette_[] to bitstream. |
| 1704 | static WebPEncodingError EncodePalette(VP8LBitWriter* const bw, int low_effort, |
| 1705 | VP8LEncoder* const enc, |
| 1706 | int percent_range, int* const percent) { |
| 1707 | int i; |
| 1708 | uint32_t tmp_palette[MAX_PALETTE_SIZE]; |
| 1709 | const int palette_size = enc->palette_size_; |
| 1710 | const uint32_t* const palette = enc->palette_; |
| 1711 | VP8LPutBits(bw, TRANSFORM_PRESENT, 1); |
| 1712 | VP8LPutBits(bw, COLOR_INDEXING_TRANSFORM, 2); |
| 1713 | assert(palette_size >= 1 && palette_size <= MAX_PALETTE_SIZE); |
| 1714 | VP8LPutBits(bw, palette_size - 1, 8); |
| 1715 | for (i = palette_size - 1; i >= 1; --i) { |
| 1716 | tmp_palette[i] = VP8LSubPixels(palette[i], palette[i - 1]); |
| 1717 | } |
| 1718 | tmp_palette[0] = palette[0]; |
| 1719 | return EncodeImageNoHuffman(bw, tmp_palette, &enc->hash_chain_, |
| 1720 | &enc->refs_[0], palette_size, 1, /*quality=*/20, |
| 1721 | low_effort, enc->pic_, percent_range, percent); |
| 1722 | } |
| 1723 | |
| 1724 | // ----------------------------------------------------------------------------- |
| 1725 | // VP8LEncoder |
| 1726 | |
| 1727 | static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config, |
| 1728 | const WebPPicture* const picture) { |
| 1729 | VP8LEncoder* const enc = (VP8LEncoder*)WebPSafeCalloc(1ULL, sizeof(*enc)); |
| 1730 | if (enc == NULL) { |
| 1731 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | enc->config_ = config; |
| 1735 | enc->pic_ = picture; |
| 1736 | enc->argb_content_ = kEncoderNone; |
| 1737 | |
| 1738 | VP8LEncDspInit(); |
| 1739 | |
| 1740 | return enc; |
| 1741 | } |
| 1742 | |
| 1743 | static void VP8LEncoderDelete(VP8LEncoder* enc) { |
| 1744 | if (enc != NULL) { |
| 1745 | int i; |
| 1746 | VP8LHashChainClear(&enc->hash_chain_); |
| 1747 | for (i = 0; i < 4; ++i) VP8LBackwardRefsClear(&enc->refs_[i]); |
| 1748 | ClearTransformBuffer(enc); |
| 1749 | WebPSafeFree(enc); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | // ----------------------------------------------------------------------------- |
| 1754 | // Main call |
| 1755 | |
| 1756 | typedef struct { |
| 1757 | const WebPConfig* config_; |
| 1758 | const WebPPicture* picture_; |
| 1759 | VP8LBitWriter* bw_; |
| 1760 | VP8LEncoder* enc_; |
| 1761 | int use_cache_; |
| 1762 | CrunchConfig crunch_configs_[CRUNCH_CONFIGS_MAX]; |
| 1763 | int num_crunch_configs_; |
| 1764 | int red_and_blue_always_zero_; |
| 1765 | WebPAuxStats* stats_; |
| 1766 | } StreamEncodeContext; |
| 1767 | |
| 1768 | static int EncodeStreamHook(void* input, void* data2) { |
| 1769 | StreamEncodeContext* const params = (StreamEncodeContext*)input; |
| 1770 | const WebPConfig* const config = params->config_; |
| 1771 | const WebPPicture* const picture = params->picture_; |
| 1772 | VP8LBitWriter* const bw = params->bw_; |
| 1773 | VP8LEncoder* const enc = params->enc_; |
| 1774 | const int use_cache = params->use_cache_; |
| 1775 | const CrunchConfig* const crunch_configs = params->crunch_configs_; |
| 1776 | const int num_crunch_configs = params->num_crunch_configs_; |
| 1777 | const int red_and_blue_always_zero = params->red_and_blue_always_zero_; |
| 1778 | #if !defined(WEBP_DISABLE_STATS) |
| 1779 | WebPAuxStats* const stats = params->stats_; |
| 1780 | #endif |
| 1781 | const int quality = (int)config->quality; |
| 1782 | const int low_effort = (config->method == 0); |
| 1783 | #if (WEBP_NEAR_LOSSLESS == 1) |
| 1784 | const int width = picture->width; |
| 1785 | #endif |
| 1786 | const int height = picture->height; |
| 1787 | const size_t byte_position = VP8LBitWriterNumBytes(bw); |
| 1788 | int percent = 2; // for WebPProgressHook |
| 1789 | #if (WEBP_NEAR_LOSSLESS == 1) |
| 1790 | int use_near_lossless = 0; |
| 1791 | #endif |
| 1792 | int hdr_size = 0; |
| 1793 | int data_size = 0; |
| 1794 | int use_delta_palette = 0; |
| 1795 | int idx; |
| 1796 | size_t best_size = ~(size_t)0; |
| 1797 | VP8LBitWriter bw_init = *bw, bw_best; |
| 1798 | (void)data2; |
| 1799 | |
| 1800 | if (!VP8LBitWriterInit(&bw_best, 0) || |
| 1801 | (num_crunch_configs > 1 && !VP8LBitWriterClone(bw, &bw_best))) { |
| 1802 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1803 | goto Error; |
| 1804 | } |
| 1805 | |
| 1806 | for (idx = 0; idx < num_crunch_configs; ++idx) { |
| 1807 | const int entropy_idx = crunch_configs[idx].entropy_idx_; |
| 1808 | int remaining_percent = 97 / num_crunch_configs, percent_range; |
| 1809 | enc->use_palette_ = |
| 1810 | (entropy_idx == kPalette) || (entropy_idx == kPaletteAndSpatial); |
| 1811 | enc->use_subtract_green_ = |
| 1812 | (entropy_idx == kSubGreen) || (entropy_idx == kSpatialSubGreen); |
| 1813 | enc->use_predict_ = (entropy_idx == kSpatial) || |
| 1814 | (entropy_idx == kSpatialSubGreen) || |
| 1815 | (entropy_idx == kPaletteAndSpatial); |
| 1816 | // When using a palette, R/B==0, hence no need to test for cross-color. |
| 1817 | if (low_effort || enc->use_palette_) { |
| 1818 | enc->use_cross_color_ = 0; |
| 1819 | } else { |
| 1820 | enc->use_cross_color_ = red_and_blue_always_zero ? 0 : enc->use_predict_; |
| 1821 | } |
| 1822 | // Reset any parameter in the encoder that is set in the previous iteration. |
| 1823 | enc->cache_bits_ = 0; |
| 1824 | VP8LBackwardRefsClear(&enc->refs_[0]); |
| 1825 | VP8LBackwardRefsClear(&enc->refs_[1]); |
| 1826 | |
| 1827 | #if (WEBP_NEAR_LOSSLESS == 1) |
| 1828 | // Apply near-lossless preprocessing. |
| 1829 | use_near_lossless = (config->near_lossless < 100) && !enc->use_palette_ && |
| 1830 | !enc->use_predict_; |
| 1831 | if (use_near_lossless) { |
| 1832 | if (!AllocateTransformBuffer(enc, width, height)) goto Error; |
| 1833 | if ((enc->argb_content_ != kEncoderNearLossless) && |
| 1834 | !VP8ApplyNearLossless(picture, config->near_lossless, enc->argb_)) { |
| 1835 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1836 | goto Error; |
| 1837 | } |
| 1838 | enc->argb_content_ = kEncoderNearLossless; |
| 1839 | } else { |
| 1840 | enc->argb_content_ = kEncoderNone; |
| 1841 | } |
| 1842 | #else |
| 1843 | enc->argb_content_ = kEncoderNone; |
| 1844 | #endif |
| 1845 | |
| 1846 | // Encode palette |
| 1847 | if (enc->use_palette_) { |
| 1848 | if (crunch_configs[idx].palette_sorting_type_ == kSortedDefault) { |
| 1849 | // Nothing to do, we have already sorted the palette. |
| 1850 | memcpy(enc->palette_, enc->palette_sorted_, |
| 1851 | enc->palette_size_ * sizeof(*enc->palette_)); |
| 1852 | } else if (crunch_configs[idx].palette_sorting_type_ == kMinimizeDelta) { |
| 1853 | PaletteSortMinimizeDeltas(enc->palette_sorted_, enc->palette_size_, |
| 1854 | enc->palette_); |
| 1855 | } else { |
| 1856 | assert(crunch_configs[idx].palette_sorting_type_ == kModifiedZeng); |
| 1857 | if (!PaletteSortModifiedZeng(enc->pic_, enc->palette_sorted_, |
| 1858 | enc->palette_size_, enc->palette_)) { |
| 1859 | goto Error; |
| 1860 | } |
| 1861 | } |
| 1862 | percent_range = remaining_percent / 4; |
| 1863 | if (!EncodePalette(bw, low_effort, enc, percent_range, &percent)) { |
| 1864 | goto Error; |
| 1865 | } |
| 1866 | remaining_percent -= percent_range; |
| 1867 | if (!MapImageFromPalette(enc, use_delta_palette)) goto Error; |
| 1868 | // If using a color cache, do not have it bigger than the number of |
| 1869 | // colors. |
| 1870 | if (use_cache && enc->palette_size_ < (1 << MAX_COLOR_CACHE_BITS)) { |
| 1871 | enc->cache_bits_ = BitsLog2Floor(enc->palette_size_) + 1; |
| 1872 | } |
| 1873 | } |
| 1874 | if (!use_delta_palette) { |
| 1875 | // In case image is not packed. |
| 1876 | if (enc->argb_content_ != kEncoderNearLossless && |
| 1877 | enc->argb_content_ != kEncoderPalette) { |
| 1878 | if (!MakeInputImageCopy(enc)) goto Error; |
| 1879 | } |
| 1880 | |
| 1881 | // ----------------------------------------------------------------------- |
| 1882 | // Apply transforms and write transform data. |
| 1883 | |
| 1884 | if (enc->use_subtract_green_) { |
| 1885 | ApplySubtractGreen(enc, enc->current_width_, height, bw); |
| 1886 | } |
| 1887 | |
| 1888 | if (enc->use_predict_) { |
| 1889 | percent_range = remaining_percent / 3; |
| 1890 | if (!ApplyPredictFilter(enc, enc->current_width_, height, quality, |
| 1891 | low_effort, enc->use_subtract_green_, bw, |
| 1892 | percent_range, &percent)) { |
| 1893 | goto Error; |
| 1894 | } |
| 1895 | remaining_percent -= percent_range; |
| 1896 | } |
| 1897 | |
| 1898 | if (enc->use_cross_color_) { |
| 1899 | percent_range = remaining_percent / 2; |
| 1900 | if (!ApplyCrossColorFilter(enc, enc->current_width_, height, quality, |
| 1901 | low_effort, bw, percent_range, &percent)) { |
| 1902 | goto Error; |
| 1903 | } |
| 1904 | remaining_percent -= percent_range; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | VP8LPutBits(bw, !TRANSFORM_PRESENT, 1); // No more transforms. |
| 1909 | |
| 1910 | // ------------------------------------------------------------------------- |
| 1911 | // Encode and write the transformed image. |
| 1912 | if (!EncodeImageInternal( |
| 1913 | bw, enc->argb_, &enc->hash_chain_, enc->refs_, enc->current_width_, |
| 1914 | height, quality, low_effort, use_cache, &crunch_configs[idx], |
| 1915 | &enc->cache_bits_, enc->histo_bits_, byte_position, &hdr_size, |
| 1916 | &data_size, picture, remaining_percent, &percent)) { |
| 1917 | goto Error; |
| 1918 | } |
| 1919 | |
| 1920 | // If we are better than what we already have. |
| 1921 | if (VP8LBitWriterNumBytes(bw) < best_size) { |
| 1922 | best_size = VP8LBitWriterNumBytes(bw); |
| 1923 | // Store the BitWriter. |
| 1924 | VP8LBitWriterSwap(bw, &bw_best); |
| 1925 | #if !defined(WEBP_DISABLE_STATS) |
| 1926 | // Update the stats. |
| 1927 | if (stats != NULL) { |
| 1928 | stats->lossless_features = 0; |
| 1929 | if (enc->use_predict_) stats->lossless_features |= 1; |
| 1930 | if (enc->use_cross_color_) stats->lossless_features |= 2; |
| 1931 | if (enc->use_subtract_green_) stats->lossless_features |= 4; |
| 1932 | if (enc->use_palette_) stats->lossless_features |= 8; |
| 1933 | stats->histogram_bits = enc->histo_bits_; |
| 1934 | stats->transform_bits = enc->transform_bits_; |
| 1935 | stats->cache_bits = enc->cache_bits_; |
| 1936 | stats->palette_size = enc->palette_size_; |
| 1937 | stats->lossless_size = (int)(best_size - byte_position); |
| 1938 | stats->lossless_hdr_size = hdr_size; |
| 1939 | stats->lossless_data_size = data_size; |
| 1940 | } |
| 1941 | #endif |
| 1942 | } |
| 1943 | // Reset the bit writer for the following iteration if any. |
| 1944 | if (num_crunch_configs > 1) VP8LBitWriterReset(&bw_init, bw); |
| 1945 | } |
| 1946 | VP8LBitWriterSwap(&bw_best, bw); |
| 1947 | |
| 1948 | Error: |
| 1949 | VP8LBitWriterWipeOut(&bw_best); |
| 1950 | // The hook should return false in case of error. |
| 1951 | return (params->picture_->error_code == VP8_ENC_OK); |
| 1952 | } |
| 1953 | |
| 1954 | int VP8LEncodeStream(const WebPConfig* const config, |
| 1955 | const WebPPicture* const picture, |
| 1956 | VP8LBitWriter* const bw_main, int use_cache) { |
| 1957 | VP8LEncoder* const enc_main = VP8LEncoderNew(config, picture); |
| 1958 | VP8LEncoder* enc_side = NULL; |
| 1959 | CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX]; |
| 1960 | int num_crunch_configs_main, num_crunch_configs_side = 0; |
| 1961 | int idx; |
| 1962 | int red_and_blue_always_zero = 0; |
| 1963 | WebPWorker worker_main, worker_side; |
| 1964 | StreamEncodeContext params_main, params_side; |
| 1965 | // The main thread uses picture->stats, the side thread uses stats_side. |
| 1966 | WebPAuxStats stats_side; |
| 1967 | VP8LBitWriter bw_side; |
| 1968 | WebPPicture picture_side; |
| 1969 | const WebPWorkerInterface* const worker_interface = WebPGetWorkerInterface(); |
| 1970 | int ok_main; |
| 1971 | |
| 1972 | if (enc_main == NULL || !VP8LBitWriterInit(&bw_side, 0)) { |
| 1973 | VP8LEncoderDelete(enc_main); |
| 1974 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1975 | } |
| 1976 | |
| 1977 | // Avoid "garbage value" error from Clang's static analysis tool. |
| 1978 | WebPPictureInit(&picture_side); |
| 1979 | |
| 1980 | // Analyze image (entropy, num_palettes etc) |
| 1981 | if (!EncoderAnalyze(enc_main, crunch_configs, &num_crunch_configs_main, |
| 1982 | &red_and_blue_always_zero) || |
| 1983 | !EncoderInit(enc_main)) { |
| 1984 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 1985 | goto Error; |
| 1986 | } |
| 1987 | |
| 1988 | // Split the configs between the main and side threads (if any). |
| 1989 | if (config->thread_level > 0) { |
| 1990 | num_crunch_configs_side = num_crunch_configs_main / 2; |
| 1991 | for (idx = 0; idx < num_crunch_configs_side; ++idx) { |
| 1992 | params_side.crunch_configs_[idx] = |
| 1993 | crunch_configs[num_crunch_configs_main - num_crunch_configs_side + |
| 1994 | idx]; |
| 1995 | } |
| 1996 | params_side.num_crunch_configs_ = num_crunch_configs_side; |
| 1997 | } |
| 1998 | num_crunch_configs_main -= num_crunch_configs_side; |
| 1999 | for (idx = 0; idx < num_crunch_configs_main; ++idx) { |
| 2000 | params_main.crunch_configs_[idx] = crunch_configs[idx]; |
| 2001 | } |
| 2002 | params_main.num_crunch_configs_ = num_crunch_configs_main; |
| 2003 | |
| 2004 | // Fill in the parameters for the thread workers. |
| 2005 | { |
| 2006 | const int params_size = (num_crunch_configs_side > 0) ? 2 : 1; |
| 2007 | for (idx = 0; idx < params_size; ++idx) { |
| 2008 | // Create the parameters for each worker. |
| 2009 | WebPWorker* const worker = (idx == 0) ? &worker_main : &worker_side; |
| 2010 | StreamEncodeContext* const param = |
| 2011 | (idx == 0) ? ¶ms_main : ¶ms_side; |
| 2012 | param->config_ = config; |
| 2013 | param->use_cache_ = use_cache; |
| 2014 | param->red_and_blue_always_zero_ = red_and_blue_always_zero; |
| 2015 | if (idx == 0) { |
| 2016 | param->picture_ = picture; |
| 2017 | param->stats_ = picture->stats; |
| 2018 | param->bw_ = bw_main; |
| 2019 | param->enc_ = enc_main; |
| 2020 | } else { |
| 2021 | // Create a side picture (error_code is not thread-safe). |
| 2022 | if (!WebPPictureView(picture, /*left=*/0, /*top=*/0, picture->width, |
| 2023 | picture->height, &picture_side)) { |
| 2024 | assert(0); |
| 2025 | } |
| 2026 | picture_side.progress_hook = NULL; // Progress hook is not thread-safe. |
| 2027 | param->picture_ = &picture_side; // No need to free a view afterwards. |
| 2028 | param->stats_ = (picture->stats == NULL) ? NULL : &stats_side; |
| 2029 | // Create a side bit writer. |
| 2030 | if (!VP8LBitWriterClone(bw_main, &bw_side)) { |
| 2031 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2032 | goto Error; |
| 2033 | } |
| 2034 | param->bw_ = &bw_side; |
| 2035 | // Create a side encoder. |
| 2036 | enc_side = VP8LEncoderNew(config, &picture_side); |
| 2037 | if (enc_side == NULL || !EncoderInit(enc_side)) { |
| 2038 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2039 | goto Error; |
| 2040 | } |
| 2041 | // Copy the values that were computed for the main encoder. |
| 2042 | enc_side->histo_bits_ = enc_main->histo_bits_; |
| 2043 | enc_side->transform_bits_ = enc_main->transform_bits_; |
| 2044 | enc_side->palette_size_ = enc_main->palette_size_; |
| 2045 | memcpy(enc_side->palette_, enc_main->palette_, |
| 2046 | sizeof(enc_main->palette_)); |
| 2047 | memcpy(enc_side->palette_sorted_, enc_main->palette_sorted_, |
| 2048 | sizeof(enc_main->palette_sorted_)); |
| 2049 | param->enc_ = enc_side; |
| 2050 | } |
| 2051 | // Create the workers. |
| 2052 | worker_interface->Init(worker); |
| 2053 | worker->data1 = param; |
| 2054 | worker->data2 = NULL; |
| 2055 | worker->hook = EncodeStreamHook; |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | // Start the second thread if needed. |
| 2060 | if (num_crunch_configs_side != 0) { |
| 2061 | if (!worker_interface->Reset(&worker_side)) { |
| 2062 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2063 | goto Error; |
| 2064 | } |
| 2065 | #if !defined(WEBP_DISABLE_STATS) |
| 2066 | // This line is here and not in the param initialization above to remove a |
| 2067 | // Clang static analyzer warning. |
| 2068 | if (picture->stats != NULL) { |
| 2069 | memcpy(&stats_side, picture->stats, sizeof(stats_side)); |
| 2070 | } |
| 2071 | #endif |
| 2072 | worker_interface->Launch(&worker_side); |
| 2073 | } |
| 2074 | // Execute the main thread. |
| 2075 | worker_interface->Execute(&worker_main); |
| 2076 | ok_main = worker_interface->Sync(&worker_main); |
| 2077 | worker_interface->End(&worker_main); |
| 2078 | if (num_crunch_configs_side != 0) { |
| 2079 | // Wait for the second thread. |
| 2080 | const int ok_side = worker_interface->Sync(&worker_side); |
| 2081 | worker_interface->End(&worker_side); |
| 2082 | if (!ok_main || !ok_side) { |
| 2083 | if (picture->error_code == VP8_ENC_OK) { |
| 2084 | assert(picture_side.error_code != VP8_ENC_OK); |
| 2085 | WebPEncodingSetError(picture, picture_side.error_code); |
| 2086 | } |
| 2087 | goto Error; |
| 2088 | } |
| 2089 | if (VP8LBitWriterNumBytes(&bw_side) < VP8LBitWriterNumBytes(bw_main)) { |
| 2090 | VP8LBitWriterSwap(bw_main, &bw_side); |
| 2091 | #if !defined(WEBP_DISABLE_STATS) |
| 2092 | if (picture->stats != NULL) { |
| 2093 | memcpy(picture->stats, &stats_side, sizeof(*picture->stats)); |
| 2094 | } |
| 2095 | #endif |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | Error: |
| 2100 | VP8LBitWriterWipeOut(&bw_side); |
| 2101 | VP8LEncoderDelete(enc_main); |
| 2102 | VP8LEncoderDelete(enc_side); |
| 2103 | return (picture->error_code == VP8_ENC_OK); |
| 2104 | } |
| 2105 | |
| 2106 | #undef CRUNCH_CONFIGS_MAX |
| 2107 | #undef CRUNCH_SUBCONFIGS_MAX |
| 2108 | |
| 2109 | int VP8LEncodeImage(const WebPConfig* const config, |
| 2110 | const WebPPicture* const picture) { |
| 2111 | int width, height; |
| 2112 | int has_alpha; |
| 2113 | size_t coded_size; |
| 2114 | int percent = 0; |
| 2115 | int initial_size; |
| 2116 | VP8LBitWriter bw; |
| 2117 | |
| 2118 | if (picture == NULL) return 0; |
| 2119 | |
| 2120 | if (config == NULL || picture->argb == NULL) { |
| 2121 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER); |
| 2122 | } |
| 2123 | |
| 2124 | width = picture->width; |
| 2125 | height = picture->height; |
| 2126 | // Initialize BitWriter with size corresponding to 16 bpp to photo images and |
| 2127 | // 8 bpp for graphical images. |
| 2128 | initial_size = (config->image_hint == WEBP_HINT_GRAPH) ? |
| 2129 | width * height : width * height * 2; |
| 2130 | if (!VP8LBitWriterInit(&bw, initial_size)) { |
| 2131 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2132 | goto Error; |
| 2133 | } |
| 2134 | |
| 2135 | if (!WebPReportProgress(picture, 1, &percent)) { |
| 2136 | UserAbort: |
| 2137 | WebPEncodingSetError(picture, VP8_ENC_ERROR_USER_ABORT); |
| 2138 | goto Error; |
| 2139 | } |
| 2140 | // Reset stats (for pure lossless coding) |
| 2141 | if (picture->stats != NULL) { |
| 2142 | WebPAuxStats* const stats = picture->stats; |
| 2143 | memset(stats, 0, sizeof(*stats)); |
| 2144 | stats->PSNR[0] = 99.f; |
| 2145 | stats->PSNR[1] = 99.f; |
| 2146 | stats->PSNR[2] = 99.f; |
| 2147 | stats->PSNR[3] = 99.f; |
| 2148 | stats->PSNR[4] = 99.f; |
| 2149 | } |
| 2150 | |
| 2151 | // Write image size. |
| 2152 | if (!WriteImageSize(picture, &bw)) { |
| 2153 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2154 | goto Error; |
| 2155 | } |
| 2156 | |
| 2157 | has_alpha = WebPPictureHasTransparency(picture); |
| 2158 | // Write the non-trivial Alpha flag and lossless version. |
| 2159 | if (!WriteRealAlphaAndVersion(&bw, has_alpha)) { |
| 2160 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2161 | goto Error; |
| 2162 | } |
| 2163 | |
| 2164 | if (!WebPReportProgress(picture, 2, &percent)) goto UserAbort; |
| 2165 | |
| 2166 | // Encode main image stream. |
| 2167 | if (!VP8LEncodeStream(config, picture, &bw, 1 /*use_cache*/)) goto Error; |
| 2168 | |
| 2169 | if (!WebPReportProgress(picture, 99, &percent)) goto UserAbort; |
| 2170 | |
| 2171 | // Finish the RIFF chunk. |
| 2172 | if (!WriteImage(picture, &bw, &coded_size)) goto Error; |
| 2173 | |
| 2174 | if (!WebPReportProgress(picture, 100, &percent)) goto UserAbort; |
| 2175 | |
| 2176 | #if !defined(WEBP_DISABLE_STATS) |
| 2177 | // Save size. |
| 2178 | if (picture->stats != NULL) { |
| 2179 | picture->stats->coded_size += (int)coded_size; |
| 2180 | picture->stats->lossless_size = (int)coded_size; |
| 2181 | } |
| 2182 | #endif |
| 2183 | |
| 2184 | if (picture->extra_info != NULL) { |
| 2185 | const int mb_w = (width + 15) >> 4; |
| 2186 | const int mb_h = (height + 15) >> 4; |
| 2187 | memset(picture->extra_info, 0, mb_w * mb_h * sizeof(*picture->extra_info)); |
| 2188 | } |
| 2189 | |
| 2190 | Error: |
| 2191 | if (bw.error_) { |
| 2192 | WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 2193 | } |
| 2194 | VP8LBitWriterWipeOut(&bw); |
| 2195 | return (picture->error_code == VP8_ENC_OK); |
| 2196 | } |
| 2197 | |
| 2198 | //------------------------------------------------------------------------------ |
| 2199 | |