1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
8 // main entry for the lossless encoder.
10 // Author: Vikas Arora (vikaas.arora@gmail.com)
17 #include "./backward_references.h"
18 #include "./vp8enci.h"
20 #include "../dsp/lossless.h"
21 #include "../utils/bit_writer.h"
22 #include "../utils/huffman_encode.h"
23 #include "../utils/utils.h"
24 #include "../webp/format_constants.h"
26 #if defined(__cplusplus) || defined(c_plusplus)
30 #define PALETTE_KEY_RIGHT_SHIFT 22 // Key for 1K buffer.
31 #define MAX_HUFF_IMAGE_SIZE (16 * 1024 * 1024)
32 #define MAX_COLORS_FOR_GRAPH 64
34 // -----------------------------------------------------------------------------
37 static int CompareColors(const void* p1, const void* p2) {
38 const uint32_t a = *(const uint32_t*)p1;
39 const uint32_t b = *(const uint32_t*)p2;
41 return (a < b) ? -1 : 1;
44 // If number of colors in the image is less than or equal to MAX_PALETTE_SIZE,
45 // creates a palette and returns true, else returns false.
46 static int AnalyzeAndCreatePalette(const WebPPicture* const pic,
47 uint32_t palette[MAX_PALETTE_SIZE],
48 int* const palette_size) {
51 uint8_t in_use[MAX_PALETTE_SIZE * 4] = { 0 };
52 uint32_t colors[MAX_PALETTE_SIZE * 4];
53 static const uint32_t kHashMul = 0x1e35a7bd;
54 const uint32_t* argb = pic->argb;
55 const int width = pic->width;
56 const int height = pic->height;
57 uint32_t last_pix = ~argb[0]; // so we're sure that last_pix != argb[0]
59 for (y = 0; y < height; ++y) {
60 for (x = 0; x < width; ++x) {
61 if (argb[x] == last_pix) {
65 key = (kHashMul * last_pix) >> PALETTE_KEY_RIGHT_SHIFT;
68 colors[key] = last_pix;
71 if (num_colors > MAX_PALETTE_SIZE) {
75 } else if (colors[key] == last_pix) {
76 // The color is already there.
79 // Some other color sits there.
80 // Do linear conflict resolution.
82 key &= (MAX_PALETTE_SIZE * 4 - 1); // key mask for 1K buffer.
86 argb += pic->argb_stride;
89 // TODO(skal): could we reuse in_use[] to speed up EncodePalette()?
91 for (i = 0; i < (int)(sizeof(in_use) / sizeof(in_use[0])); ++i) {
93 palette[num_colors] = colors[i];
98 qsort(palette, num_colors, sizeof(*palette), CompareColors);
99 *palette_size = num_colors;
103 static int AnalyzeEntropy(const uint32_t* argb,
104 int width, int height, int argb_stride,
105 double* const nonpredicted_bits,
106 double* const predicted_bits) {
108 const uint32_t* last_line = NULL;
109 uint32_t last_pix = argb[0]; // so we're sure that pix_diff == 0
111 VP8LHistogram* nonpredicted = NULL;
112 VP8LHistogram* predicted =
113 (VP8LHistogram*)malloc(2 * sizeof(*predicted));
114 if (predicted == NULL) return 0;
115 nonpredicted = predicted + 1;
117 VP8LHistogramInit(predicted, 0);
118 VP8LHistogramInit(nonpredicted, 0);
119 for (y = 0; y < height; ++y) {
120 for (x = 0; x < width; ++x) {
121 const uint32_t pix = argb[x];
122 const uint32_t pix_diff = VP8LSubPixels(pix, last_pix);
123 if (pix_diff == 0) continue;
124 if (last_line != NULL && pix == last_line[x]) {
129 const PixOrCopy pix_token = PixOrCopyCreateLiteral(pix);
130 const PixOrCopy pix_diff_token = PixOrCopyCreateLiteral(pix_diff);
131 VP8LHistogramAddSinglePixOrCopy(nonpredicted, &pix_token);
132 VP8LHistogramAddSinglePixOrCopy(predicted, &pix_diff_token);
138 *nonpredicted_bits = VP8LHistogramEstimateBitsBulk(nonpredicted);
139 *predicted_bits = VP8LHistogramEstimateBitsBulk(predicted);
144 static int VP8LEncAnalyze(VP8LEncoder* const enc, WebPImageHint image_hint) {
145 const WebPPicture* const pic = enc->pic_;
146 assert(pic != NULL && pic->argb != NULL);
149 AnalyzeAndCreatePalette(pic, enc->palette_, &enc->palette_size_);
151 if (image_hint == WEBP_HINT_GRAPH) {
152 if (enc->use_palette_ && enc->palette_size_ < MAX_COLORS_FOR_GRAPH) {
153 enc->use_palette_ = 0;
157 if (!enc->use_palette_) {
158 if (image_hint == WEBP_HINT_PHOTO) {
159 enc->use_predict_ = 1;
160 enc->use_cross_color_ = 1;
162 double non_pred_entropy, pred_entropy;
163 if (!AnalyzeEntropy(pic->argb, pic->width, pic->height, pic->argb_stride,
164 &non_pred_entropy, &pred_entropy)) {
167 if (pred_entropy < 0.95 * non_pred_entropy) {
168 enc->use_predict_ = 1;
169 // TODO(vikasa): Observed some correlation of cross_color transform with
170 // predict. Need to investigate this further and add separate heuristic
171 // for setting use_cross_color flag.
172 enc->use_cross_color_ = 1;
180 static int GetHuffBitLengthsAndCodes(
181 const VP8LHistogramSet* const histogram_image,
182 HuffmanTreeCode* const huffman_codes) {
185 uint64_t total_length_size = 0;
186 uint8_t* mem_buf = NULL;
187 const int histogram_image_size = histogram_image->size;
189 // Iterate over all histograms and get the aggregate number of codes used.
190 for (i = 0; i < histogram_image_size; ++i) {
191 const VP8LHistogram* const histo = histogram_image->histograms[i];
192 HuffmanTreeCode* const codes = &huffman_codes[5 * i];
193 for (k = 0; k < 5; ++k) {
194 const int num_symbols = (k == 0) ? VP8LHistogramNumCodes(histo)
195 : (k == 4) ? NUM_DISTANCE_CODES
197 codes[k].num_symbols = num_symbols;
198 total_length_size += num_symbols;
202 // Allocate and Set Huffman codes.
206 mem_buf = (uint8_t*)WebPSafeCalloc(total_length_size,
207 sizeof(*lengths) + sizeof(*codes));
208 if (mem_buf == NULL) {
212 codes = (uint16_t*)mem_buf;
213 lengths = (uint8_t*)&codes[total_length_size];
214 for (i = 0; i < 5 * histogram_image_size; ++i) {
215 const int bit_length = huffman_codes[i].num_symbols;
216 huffman_codes[i].codes = codes;
217 huffman_codes[i].code_lengths = lengths;
219 lengths += bit_length;
223 // Create Huffman trees.
224 for (i = 0; ok && (i < histogram_image_size); ++i) {
225 HuffmanTreeCode* const codes = &huffman_codes[5 * i];
226 VP8LHistogram* const histo = histogram_image->histograms[i];
227 ok = ok && VP8LCreateHuffmanTree(histo->literal_, 15, codes + 0);
228 ok = ok && VP8LCreateHuffmanTree(histo->red_, 15, codes + 1);
229 ok = ok && VP8LCreateHuffmanTree(histo->blue_, 15, codes + 2);
230 ok = ok && VP8LCreateHuffmanTree(histo->alpha_, 15, codes + 3);
231 ok = ok && VP8LCreateHuffmanTree(histo->distance_, 15, codes + 4);
237 // If one VP8LCreateHuffmanTree() above fails, we need to clean up behind.
238 memset(huffman_codes, 0, 5 * histogram_image_size * sizeof(*huffman_codes));
243 static void StoreHuffmanTreeOfHuffmanTreeToBitMask(
244 VP8LBitWriter* const bw, const uint8_t* code_length_bitdepth) {
245 // RFC 1951 will calm you down if you are worried about this funny sequence.
246 // This sequence is tuned from that, but more weighted for lower symbol count,
247 // and more spiking histograms.
248 static const uint8_t kStorageOrder[CODE_LENGTH_CODES] = {
249 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
252 // Throw away trailing zeros:
253 int codes_to_store = CODE_LENGTH_CODES;
254 for (; codes_to_store > 4; --codes_to_store) {
255 if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
259 VP8LWriteBits(bw, 4, codes_to_store - 4);
260 for (i = 0; i < codes_to_store; ++i) {
261 VP8LWriteBits(bw, 3, code_length_bitdepth[kStorageOrder[i]]);
265 static void ClearHuffmanTreeIfOnlyOneSymbol(
266 HuffmanTreeCode* const huffman_code) {
269 for (k = 0; k < huffman_code->num_symbols; ++k) {
270 if (huffman_code->code_lengths[k] != 0) {
272 if (count > 1) return;
275 for (k = 0; k < huffman_code->num_symbols; ++k) {
276 huffman_code->code_lengths[k] = 0;
277 huffman_code->codes[k] = 0;
281 static void StoreHuffmanTreeToBitMask(
282 VP8LBitWriter* const bw,
283 const HuffmanTreeToken* const tokens, const int num_tokens,
284 const HuffmanTreeCode* const huffman_code) {
286 for (i = 0; i < num_tokens; ++i) {
287 const int ix = tokens[i].code;
288 const int extra_bits = tokens[i].extra_bits;
289 VP8LWriteBits(bw, huffman_code->code_lengths[ix], huffman_code->codes[ix]);
292 VP8LWriteBits(bw, 2, extra_bits);
295 VP8LWriteBits(bw, 3, extra_bits);
298 VP8LWriteBits(bw, 7, extra_bits);
304 static int StoreFullHuffmanCode(VP8LBitWriter* const bw,
305 const HuffmanTreeCode* const tree) {
307 uint8_t code_length_bitdepth[CODE_LENGTH_CODES] = { 0 };
308 uint16_t code_length_bitdepth_symbols[CODE_LENGTH_CODES] = { 0 };
309 const int max_tokens = tree->num_symbols;
311 HuffmanTreeCode huffman_code;
312 HuffmanTreeToken* const tokens =
313 (HuffmanTreeToken*)WebPSafeMalloc((uint64_t)max_tokens, sizeof(*tokens));
314 if (tokens == NULL) return 0;
316 huffman_code.num_symbols = CODE_LENGTH_CODES;
317 huffman_code.code_lengths = code_length_bitdepth;
318 huffman_code.codes = code_length_bitdepth_symbols;
320 VP8LWriteBits(bw, 1, 0);
321 num_tokens = VP8LCreateCompressedHuffmanTree(tree, tokens, max_tokens);
323 int histogram[CODE_LENGTH_CODES] = { 0 };
325 for (i = 0; i < num_tokens; ++i) {
326 ++histogram[tokens[i].code];
329 if (!VP8LCreateHuffmanTree(histogram, 7, &huffman_code)) {
334 StoreHuffmanTreeOfHuffmanTreeToBitMask(bw, code_length_bitdepth);
335 ClearHuffmanTreeIfOnlyOneSymbol(&huffman_code);
337 int trailing_zero_bits = 0;
338 int trimmed_length = num_tokens;
339 int write_trimmed_length;
343 const int ix = tokens[i].code;
344 if (ix == 0 || ix == 17 || ix == 18) {
345 --trimmed_length; // discount trailing zeros
346 trailing_zero_bits += code_length_bitdepth[ix];
348 trailing_zero_bits += 3;
349 } else if (ix == 18) {
350 trailing_zero_bits += 7;
356 write_trimmed_length = (trimmed_length > 1 && trailing_zero_bits > 12);
357 length = write_trimmed_length ? trimmed_length : num_tokens;
358 VP8LWriteBits(bw, 1, write_trimmed_length);
359 if (write_trimmed_length) {
360 const int nbits = VP8LBitsLog2Ceiling(trimmed_length - 1);
361 const int nbitpairs = (nbits == 0) ? 1 : (nbits + 1) / 2;
362 VP8LWriteBits(bw, 3, nbitpairs - 1);
363 assert(trimmed_length >= 2);
364 VP8LWriteBits(bw, nbitpairs * 2, trimmed_length - 2);
366 StoreHuffmanTreeToBitMask(bw, tokens, length, &huffman_code);
374 static int StoreHuffmanCode(VP8LBitWriter* const bw,
375 const HuffmanTreeCode* const huffman_code) {
378 int symbols[2] = { 0, 0 };
379 const int kMaxBits = 8;
380 const int kMaxSymbol = 1 << kMaxBits;
382 // Check whether it's a small tree.
383 for (i = 0; i < huffman_code->num_symbols && count < 3; ++i) {
384 if (huffman_code->code_lengths[i] != 0) {
385 if (count < 2) symbols[count] = i;
390 if (count == 0) { // emit minimal tree for empty cases
391 // bits: small tree marker: 1, count-1: 0, large 8-bit code: 0, code: 0
392 VP8LWriteBits(bw, 4, 0x01);
394 } else if (count <= 2 && symbols[0] < kMaxSymbol && symbols[1] < kMaxSymbol) {
395 VP8LWriteBits(bw, 1, 1); // Small tree marker to encode 1 or 2 symbols.
396 VP8LWriteBits(bw, 1, count - 1);
397 if (symbols[0] <= 1) {
398 VP8LWriteBits(bw, 1, 0); // Code bit for small (1 bit) symbol value.
399 VP8LWriteBits(bw, 1, symbols[0]);
401 VP8LWriteBits(bw, 1, 1);
402 VP8LWriteBits(bw, 8, symbols[0]);
405 VP8LWriteBits(bw, 8, symbols[1]);
409 return StoreFullHuffmanCode(bw, huffman_code);
413 static void WriteHuffmanCode(VP8LBitWriter* const bw,
414 const HuffmanTreeCode* const code,
416 const int depth = code->code_lengths[code_index];
417 const int symbol = code->codes[code_index];
418 VP8LWriteBits(bw, depth, symbol);
421 static void StoreImageToBitMask(
422 VP8LBitWriter* const bw, int width, int histo_bits,
423 const VP8LBackwardRefs* const refs,
424 const uint16_t* histogram_symbols,
425 const HuffmanTreeCode* const huffman_codes) {
426 // x and y trace the position in the image.
429 const int histo_xsize = histo_bits ? VP8LSubSampleSize(width, histo_bits) : 1;
431 for (i = 0; i < refs->size; ++i) {
432 const PixOrCopy* const v = &refs->refs[i];
433 const int histogram_ix = histogram_symbols[histo_bits ?
434 (y >> histo_bits) * histo_xsize +
435 (x >> histo_bits) : 0];
436 const HuffmanTreeCode* const codes = huffman_codes + 5 * histogram_ix;
437 if (PixOrCopyIsCacheIdx(v)) {
438 const int code = PixOrCopyCacheIdx(v);
439 const int literal_ix = 256 + NUM_LENGTH_CODES + code;
440 WriteHuffmanCode(bw, codes, literal_ix);
441 } else if (PixOrCopyIsLiteral(v)) {
442 static const int order[] = { 1, 2, 0, 3 };
444 for (k = 0; k < 4; ++k) {
445 const int code = PixOrCopyLiteral(v, order[k]);
446 WriteHuffmanCode(bw, codes + k, code);
452 PrefixEncode(v->len, &code, &n_bits, &bits);
453 WriteHuffmanCode(bw, codes, 256 + code);
454 VP8LWriteBits(bw, n_bits, bits);
456 distance = PixOrCopyDistance(v);
457 PrefixEncode(distance, &code, &n_bits, &bits);
458 WriteHuffmanCode(bw, codes + 4, code);
459 VP8LWriteBits(bw, n_bits, bits);
461 x += PixOrCopyLength(v);
469 // Special case of EncodeImageInternal() for cache-bits=0, histo_bits=31
470 static int EncodeImageNoHuffman(VP8LBitWriter* const bw,
471 const uint32_t* const argb,
472 int width, int height, int quality) {
475 VP8LBackwardRefs refs;
476 HuffmanTreeCode huffman_codes[5] = { { 0, NULL, NULL } };
477 const uint16_t histogram_symbols[1] = { 0 }; // only one tree, one symbol
478 VP8LHistogramSet* const histogram_image = VP8LAllocateHistogramSet(1, 0);
479 if (histogram_image == NULL) return 0;
481 // Calculate backward references from ARGB image.
482 if (!VP8LGetBackwardReferences(width, height, argb, quality, 0, 1, &refs)) {
485 // Build histogram image and symbols from backward references.
486 VP8LHistogramStoreRefs(&refs, histogram_image->histograms[0]);
488 // Create Huffman bit lengths and codes for each histogram image.
489 assert(histogram_image->size == 1);
490 if (!GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
494 // No color cache, no Huffman image.
495 VP8LWriteBits(bw, 1, 0);
497 // Store Huffman codes.
498 for (i = 0; i < 5; ++i) {
499 HuffmanTreeCode* const codes = &huffman_codes[i];
500 if (!StoreHuffmanCode(bw, codes)) {
503 ClearHuffmanTreeIfOnlyOneSymbol(codes);
506 // Store actual literals.
507 StoreImageToBitMask(bw, width, 0, &refs, histogram_symbols, huffman_codes);
511 free(histogram_image);
512 VP8LClearBackwardRefs(&refs);
513 free(huffman_codes[0].codes);
517 static int EncodeImageInternal(VP8LBitWriter* const bw,
518 const uint32_t* const argb,
519 int width, int height, int quality,
520 int cache_bits, int histogram_bits) {
522 const int use_2d_locality = 1;
523 const int use_color_cache = (cache_bits > 0);
524 const uint32_t histogram_image_xysize =
525 VP8LSubSampleSize(width, histogram_bits) *
526 VP8LSubSampleSize(height, histogram_bits);
527 VP8LHistogramSet* histogram_image =
528 VP8LAllocateHistogramSet(histogram_image_xysize, 0);
529 int histogram_image_size = 0;
530 size_t bit_array_size = 0;
531 HuffmanTreeCode* huffman_codes = NULL;
532 VP8LBackwardRefs refs;
533 uint16_t* const histogram_symbols =
534 (uint16_t*)WebPSafeMalloc((uint64_t)histogram_image_xysize,
535 sizeof(*histogram_symbols));
536 assert(histogram_bits >= MIN_HUFFMAN_BITS);
537 assert(histogram_bits <= MAX_HUFFMAN_BITS);
539 if (histogram_image == NULL || histogram_symbols == NULL) {
540 free(histogram_image);
541 free(histogram_symbols);
545 // Calculate backward references from ARGB image.
546 if (!VP8LGetBackwardReferences(width, height, argb, quality, cache_bits,
547 use_2d_locality, &refs)) {
550 // Build histogram image and symbols from backward references.
551 if (!VP8LGetHistoImageSymbols(width, height, &refs,
552 quality, histogram_bits, cache_bits,
554 histogram_symbols)) {
557 // Create Huffman bit lengths and codes for each histogram image.
558 histogram_image_size = histogram_image->size;
559 bit_array_size = 5 * histogram_image_size;
560 huffman_codes = (HuffmanTreeCode*)WebPSafeCalloc(bit_array_size,
561 sizeof(*huffman_codes));
562 if (huffman_codes == NULL ||
563 !GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
566 // Free combined histograms.
567 free(histogram_image);
568 histogram_image = NULL;
570 // Color Cache parameters.
571 VP8LWriteBits(bw, 1, use_color_cache);
572 if (use_color_cache) {
573 VP8LWriteBits(bw, 4, cache_bits);
576 // Huffman image + meta huffman.
578 const int write_histogram_image = (histogram_image_size > 1);
579 VP8LWriteBits(bw, 1, write_histogram_image);
580 if (write_histogram_image) {
581 uint32_t* const histogram_argb =
582 (uint32_t*)WebPSafeMalloc((uint64_t)histogram_image_xysize,
583 sizeof(*histogram_argb));
586 if (histogram_argb == NULL) goto Error;
587 for (i = 0; i < histogram_image_xysize; ++i) {
588 const int symbol_index = histogram_symbols[i] & 0xffff;
589 histogram_argb[i] = 0xff000000 | (symbol_index << 8);
590 if (symbol_index >= max_index) {
591 max_index = symbol_index + 1;
594 histogram_image_size = max_index;
596 VP8LWriteBits(bw, 3, histogram_bits - 2);
597 ok = EncodeImageNoHuffman(bw, histogram_argb,
598 VP8LSubSampleSize(width, histogram_bits),
599 VP8LSubSampleSize(height, histogram_bits),
601 free(histogram_argb);
606 // Store Huffman codes.
609 for (i = 0; i < 5 * histogram_image_size; ++i) {
610 HuffmanTreeCode* const codes = &huffman_codes[i];
611 if (!StoreHuffmanCode(bw, codes)) goto Error;
612 ClearHuffmanTreeIfOnlyOneSymbol(codes);
616 // Store actual literals.
617 StoreImageToBitMask(bw, width, histogram_bits, &refs,
618 histogram_symbols, huffman_codes);
622 free(histogram_image);
624 VP8LClearBackwardRefs(&refs);
625 if (huffman_codes != NULL) {
626 free(huffman_codes->codes);
629 free(histogram_symbols);
633 // -----------------------------------------------------------------------------
636 // Check if it would be a good idea to subtract green from red and blue. We
637 // only impact entropy in red/blue components, don't bother to look at others.
638 static int EvalAndApplySubtractGreen(VP8LEncoder* const enc,
639 int width, int height,
640 VP8LBitWriter* const bw) {
641 if (!enc->use_palette_) {
643 const uint32_t* const argb = enc->argb_;
644 double bit_cost_before, bit_cost_after;
645 VP8LHistogram* const histo = (VP8LHistogram*)malloc(sizeof(*histo));
646 if (histo == NULL) return 0;
648 VP8LHistogramInit(histo, 1);
649 for (i = 0; i < width * height; ++i) {
650 const uint32_t c = argb[i];
651 ++histo->red_[(c >> 16) & 0xff];
652 ++histo->blue_[(c >> 0) & 0xff];
654 bit_cost_before = VP8LHistogramEstimateBits(histo);
656 VP8LHistogramInit(histo, 1);
657 for (i = 0; i < width * height; ++i) {
658 const uint32_t c = argb[i];
659 const int green = (c >> 8) & 0xff;
660 ++histo->red_[((c >> 16) - green) & 0xff];
661 ++histo->blue_[((c >> 0) - green) & 0xff];
663 bit_cost_after = VP8LHistogramEstimateBits(histo);
666 // Check if subtracting green yields low entropy.
667 enc->use_subtract_green_ = (bit_cost_after < bit_cost_before);
668 if (enc->use_subtract_green_) {
669 VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
670 VP8LWriteBits(bw, 2, SUBTRACT_GREEN);
671 VP8LSubtractGreenFromBlueAndRed(enc->argb_, width * height);
677 static int ApplyPredictFilter(const VP8LEncoder* const enc,
678 int width, int height, int quality,
679 VP8LBitWriter* const bw) {
680 const int pred_bits = enc->transform_bits_;
681 const int transform_width = VP8LSubSampleSize(width, pred_bits);
682 const int transform_height = VP8LSubSampleSize(height, pred_bits);
684 VP8LResidualImage(width, height, pred_bits, enc->argb_, enc->argb_scratch_,
685 enc->transform_data_);
686 VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
687 VP8LWriteBits(bw, 2, PREDICTOR_TRANSFORM);
688 assert(pred_bits >= 2);
689 VP8LWriteBits(bw, 3, pred_bits - 2);
690 if (!EncodeImageNoHuffman(bw, enc->transform_data_,
691 transform_width, transform_height, quality)) {
697 static int ApplyCrossColorFilter(const VP8LEncoder* const enc,
698 int width, int height, int quality,
699 VP8LBitWriter* const bw) {
700 const int ccolor_transform_bits = enc->transform_bits_;
701 const int transform_width = VP8LSubSampleSize(width, ccolor_transform_bits);
702 const int transform_height = VP8LSubSampleSize(height, ccolor_transform_bits);
703 const int step = (quality == 0) ? 32 : 8;
705 VP8LColorSpaceTransform(width, height, ccolor_transform_bits, step,
706 enc->argb_, enc->transform_data_);
707 VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
708 VP8LWriteBits(bw, 2, CROSS_COLOR_TRANSFORM);
709 assert(ccolor_transform_bits >= 2);
710 VP8LWriteBits(bw, 3, ccolor_transform_bits - 2);
711 if (!EncodeImageNoHuffman(bw, enc->transform_data_,
712 transform_width, transform_height, quality)) {
718 // -----------------------------------------------------------------------------
720 static WebPEncodingError WriteRiffHeader(const WebPPicture* const pic,
721 size_t riff_size, size_t vp8l_size) {
722 uint8_t riff[RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE + VP8L_SIGNATURE_SIZE] = {
723 'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P',
724 'V', 'P', '8', 'L', 0, 0, 0, 0, VP8L_MAGIC_BYTE,
726 PutLE32(riff + TAG_SIZE, (uint32_t)riff_size);
727 PutLE32(riff + RIFF_HEADER_SIZE + TAG_SIZE, (uint32_t)vp8l_size);
728 if (!pic->writer(riff, sizeof(riff), pic)) {
729 return VP8_ENC_ERROR_BAD_WRITE;
734 static int WriteImageSize(const WebPPicture* const pic,
735 VP8LBitWriter* const bw) {
736 const int width = pic->width - 1;
737 const int height = pic->height - 1;
738 assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION);
740 VP8LWriteBits(bw, VP8L_IMAGE_SIZE_BITS, width);
741 VP8LWriteBits(bw, VP8L_IMAGE_SIZE_BITS, height);
745 static int WriteRealAlphaAndVersion(VP8LBitWriter* const bw, int has_alpha) {
746 VP8LWriteBits(bw, 1, has_alpha);
747 VP8LWriteBits(bw, VP8L_VERSION_BITS, VP8L_VERSION);
751 static WebPEncodingError WriteImage(const WebPPicture* const pic,
752 VP8LBitWriter* const bw,
753 size_t* const coded_size) {
754 WebPEncodingError err = VP8_ENC_OK;
755 const uint8_t* const webpll_data = VP8LBitWriterFinish(bw);
756 const size_t webpll_size = VP8LBitWriterNumBytes(bw);
757 const size_t vp8l_size = VP8L_SIGNATURE_SIZE + webpll_size;
758 const size_t pad = vp8l_size & 1;
759 const size_t riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8l_size + pad;
761 err = WriteRiffHeader(pic, riff_size, vp8l_size);
762 if (err != VP8_ENC_OK) goto Error;
764 if (!pic->writer(webpll_data, webpll_size, pic)) {
765 err = VP8_ENC_ERROR_BAD_WRITE;
770 const uint8_t pad_byte[1] = { 0 };
771 if (!pic->writer(pad_byte, 1, pic)) {
772 err = VP8_ENC_ERROR_BAD_WRITE;
776 *coded_size = CHUNK_HEADER_SIZE + riff_size;
783 // -----------------------------------------------------------------------------
785 // Allocates the memory for argb (W x H) buffer, 2 rows of context for
786 // prediction and transform data.
787 static WebPEncodingError AllocateTransformBuffer(VP8LEncoder* const enc,
788 int width, int height) {
789 WebPEncodingError err = VP8_ENC_OK;
790 const int tile_size = 1 << enc->transform_bits_;
791 const uint64_t image_size = width * height;
792 const uint64_t argb_scratch_size = tile_size * width + width;
793 const uint64_t transform_data_size =
794 (uint64_t)VP8LSubSampleSize(width, enc->transform_bits_) *
795 (uint64_t)VP8LSubSampleSize(height, enc->transform_bits_);
796 const uint64_t total_size =
797 image_size + argb_scratch_size + transform_data_size;
798 uint32_t* mem = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*mem));
800 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
805 enc->argb_scratch_ = mem;
806 mem += argb_scratch_size;
807 enc->transform_data_ = mem;
808 enc->current_width_ = width;
814 static void ApplyPalette(uint32_t* src, uint32_t* dst,
815 uint32_t src_stride, uint32_t dst_stride,
816 const uint32_t* palette, int palette_size,
817 int width, int height, int xbits, uint8_t* row) {
820 for (i = 0; i < palette_size; ++i) {
821 if ((palette[i] & 0xffff00ffu) != 0) {
828 uint8_t inv_palette[MAX_PALETTE_SIZE] = { 0 };
829 for (i = 0; i < palette_size; ++i) {
830 const int color = (palette[i] >> 8) & 0xff;
831 inv_palette[color] = i;
833 for (y = 0; y < height; ++y) {
834 for (x = 0; x < width; ++x) {
835 const int color = (src[x] >> 8) & 0xff;
836 row[x] = inv_palette[color];
838 VP8LBundleColorMap(row, width, xbits, dst);
843 // Use 1 pixel cache for ARGB pixels.
844 uint32_t last_pix = palette[0];
846 for (y = 0; y < height; ++y) {
847 for (x = 0; x < width; ++x) {
848 const uint32_t pix = src[x];
849 if (pix != last_pix) {
850 for (i = 0; i < palette_size; ++i) {
851 if (pix == palette[i]) {
860 VP8LBundleColorMap(row, width, xbits, dst);
867 // Note: Expects "enc->palette_" to be set properly.
868 // Also, "enc->palette_" will be modified after this call and should not be used
870 static WebPEncodingError EncodePalette(VP8LBitWriter* const bw,
871 VP8LEncoder* const enc, int quality) {
872 WebPEncodingError err = VP8_ENC_OK;
874 const WebPPicture* const pic = enc->pic_;
875 uint32_t* src = pic->argb;
877 const int width = pic->width;
878 const int height = pic->height;
879 uint32_t* const palette = enc->palette_;
880 const int palette_size = enc->palette_size_;
884 // Replace each input pixel by corresponding palette index.
885 // This is done line by line.
886 if (palette_size <= 4) {
887 xbits = (palette_size <= 2) ? 3 : 2;
889 xbits = (palette_size <= 16) ? 1 : 0;
892 err = AllocateTransformBuffer(enc, VP8LSubSampleSize(width, xbits), height);
893 if (err != VP8_ENC_OK) goto Error;
896 row = WebPSafeMalloc((uint64_t)width, sizeof(*row));
897 if (row == NULL) return VP8_ENC_ERROR_OUT_OF_MEMORY;
899 ApplyPalette(src, dst, pic->argb_stride, enc->current_width_,
900 palette, palette_size, width, height, xbits, row);
902 // Save palette to bitstream.
903 VP8LWriteBits(bw, 1, TRANSFORM_PRESENT);
904 VP8LWriteBits(bw, 2, COLOR_INDEXING_TRANSFORM);
905 assert(palette_size >= 1);
906 VP8LWriteBits(bw, 8, palette_size - 1);
907 for (i = palette_size - 1; i >= 1; --i) {
908 palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
910 if (!EncodeImageNoHuffman(bw, palette, palette_size, 1, quality)) {
911 err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
920 // -----------------------------------------------------------------------------
922 static int GetHistoBits(int method, int use_palette, int width, int height) {
923 const uint64_t hist_size = sizeof(VP8LHistogram);
924 // Make tile size a function of encoding method (Range: 0 to 6).
925 int histo_bits = (use_palette ? 9 : 7) - method;
927 const uint64_t huff_image_size = VP8LSubSampleSize(width, histo_bits) *
928 VP8LSubSampleSize(height, histo_bits) *
930 if (huff_image_size <= MAX_HUFF_IMAGE_SIZE) break;
933 return (histo_bits < MIN_HUFFMAN_BITS) ? MIN_HUFFMAN_BITS :
934 (histo_bits > MAX_HUFFMAN_BITS) ? MAX_HUFFMAN_BITS : histo_bits;
937 static void FinishEncParams(VP8LEncoder* const enc) {
938 const WebPConfig* const config = enc->config_;
939 const WebPPicture* const pic = enc->pic_;
940 const int method = config->method;
941 const float quality = config->quality;
942 const int use_palette = enc->use_palette_;
943 enc->transform_bits_ = (method < 4) ? 5 : (method > 4) ? 3 : 4;
944 enc->histo_bits_ = GetHistoBits(method, use_palette, pic->width, pic->height);
945 enc->cache_bits_ = (quality <= 25.f) ? 0 : 7;
948 // -----------------------------------------------------------------------------
951 static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
952 const WebPPicture* const picture) {
953 VP8LEncoder* const enc = (VP8LEncoder*)calloc(1, sizeof(*enc));
955 WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
958 enc->config_ = config;
963 static void VP8LEncoderDelete(VP8LEncoder* enc) {
968 // -----------------------------------------------------------------------------
971 WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
972 const WebPPicture* const picture,
973 VP8LBitWriter* const bw) {
974 WebPEncodingError err = VP8_ENC_OK;
975 const int quality = (int)config->quality;
976 const int width = picture->width;
977 const int height = picture->height;
978 VP8LEncoder* const enc = VP8LEncoderNew(config, picture);
979 const size_t byte_position = VP8LBitWriterNumBytes(bw);
982 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
986 // ---------------------------------------------------------------------------
987 // Analyze image (entropy, num_palettes etc)
989 if (!VP8LEncAnalyze(enc, config->image_hint)) {
990 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
994 FinishEncParams(enc);
996 if (enc->use_palette_) {
997 err = EncodePalette(bw, enc, quality);
998 if (err != VP8_ENC_OK) goto Error;
999 // Color cache is disabled for palette.
1000 enc->cache_bits_ = 0;
1003 // In case image is not packed.
1004 if (enc->argb_ == NULL) {
1006 err = AllocateTransformBuffer(enc, width, height);
1007 if (err != VP8_ENC_OK) goto Error;
1008 for (y = 0; y < height; ++y) {
1009 memcpy(enc->argb_ + y * width,
1010 picture->argb + y * picture->argb_stride,
1011 width * sizeof(*enc->argb_));
1013 enc->current_width_ = width;
1016 // ---------------------------------------------------------------------------
1017 // Apply transforms and write transform data.
1019 if (!EvalAndApplySubtractGreen(enc, enc->current_width_, height, bw)) {
1020 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1024 if (enc->use_predict_) {
1025 if (!ApplyPredictFilter(enc, enc->current_width_, height, quality, bw)) {
1026 err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1031 if (enc->use_cross_color_) {
1032 if (!ApplyCrossColorFilter(enc, enc->current_width_, height, quality, bw)) {
1033 err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1038 VP8LWriteBits(bw, 1, !TRANSFORM_PRESENT); // No more transforms.
1040 // ---------------------------------------------------------------------------
1041 // Estimate the color cache size.
1043 if (enc->cache_bits_ > 0) {
1044 if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
1045 height, &enc->cache_bits_)) {
1046 err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1051 // ---------------------------------------------------------------------------
1052 // Encode and write the transformed image.
1054 if (!EncodeImageInternal(bw, enc->argb_, enc->current_width_, height,
1055 quality, enc->cache_bits_, enc->histo_bits_)) {
1056 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1060 if (picture->stats != NULL) {
1061 WebPAuxStats* const stats = picture->stats;
1062 stats->lossless_features = 0;
1063 if (enc->use_predict_) stats->lossless_features |= 1;
1064 if (enc->use_cross_color_) stats->lossless_features |= 2;
1065 if (enc->use_subtract_green_) stats->lossless_features |= 4;
1066 if (enc->use_palette_) stats->lossless_features |= 8;
1067 stats->histogram_bits = enc->histo_bits_;
1068 stats->transform_bits = enc->transform_bits_;
1069 stats->cache_bits = enc->cache_bits_;
1070 stats->palette_size = enc->palette_size_;
1071 stats->lossless_size = (int)(VP8LBitWriterNumBytes(bw) - byte_position);
1075 VP8LEncoderDelete(enc);
1079 int VP8LEncodeImage(const WebPConfig* const config,
1080 const WebPPicture* const picture) {
1085 WebPEncodingError err = VP8_ENC_OK;
1088 if (picture == NULL) return 0;
1090 if (config == NULL || picture->argb == NULL) {
1091 err = VP8_ENC_ERROR_NULL_PARAMETER;
1092 WebPEncodingSetError(picture, err);
1096 width = picture->width;
1097 height = picture->height;
1098 if (!VP8LBitWriterInit(&bw, (width * height) >> 1)) {
1099 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1103 if (!WebPReportProgress(picture, 1, &percent)) {
1105 err = VP8_ENC_ERROR_USER_ABORT;
1108 // Reset stats (for pure lossless coding)
1109 if (picture->stats != NULL) {
1110 WebPAuxStats* const stats = picture->stats;
1111 memset(stats, 0, sizeof(*stats));
1112 stats->PSNR[0] = 99.f;
1113 stats->PSNR[1] = 99.f;
1114 stats->PSNR[2] = 99.f;
1115 stats->PSNR[3] = 99.f;
1116 stats->PSNR[4] = 99.f;
1119 // Write image size.
1120 if (!WriteImageSize(picture, &bw)) {
1121 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1125 has_alpha = WebPPictureHasTransparency(picture);
1126 // Write the non-trivial Alpha flag and lossless version.
1127 if (!WriteRealAlphaAndVersion(&bw, has_alpha)) {
1128 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1132 if (!WebPReportProgress(picture, 5, &percent)) goto UserAbort;
1134 // Encode main image stream.
1135 err = VP8LEncodeStream(config, picture, &bw);
1136 if (err != VP8_ENC_OK) goto Error;
1138 // TODO(skal): have a fine-grained progress report in VP8LEncodeStream().
1139 if (!WebPReportProgress(picture, 90, &percent)) goto UserAbort;
1141 // Finish the RIFF chunk.
1142 err = WriteImage(picture, &bw, &coded_size);
1143 if (err != VP8_ENC_OK) goto Error;
1145 if (!WebPReportProgress(picture, 100, &percent)) goto UserAbort;
1148 if (picture->stats != NULL) {
1149 picture->stats->coded_size += (int)coded_size;
1150 picture->stats->lossless_size = (int)coded_size;
1153 if (picture->extra_info != NULL) {
1154 const int mb_w = (width + 15) >> 4;
1155 const int mb_h = (height + 15) >> 4;
1156 memset(picture->extra_info, 0, mb_w * mb_h * sizeof(*picture->extra_info));
1160 if (bw.error_) err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1161 VP8LBitWriterDestroy(&bw);
1162 if (err != VP8_ENC_OK) {
1163 WebPEncodingSetError(picture, err);
1169 //------------------------------------------------------------------------------
1171 #if defined(__cplusplus) || defined(c_plusplus)