OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Author: Jyrki Alakuijala (jyrki@google.com) | 10 // Author: Jyrki Alakuijala (jyrki@google.com) |
11 // | 11 // |
12 #ifdef HAVE_CONFIG_H | 12 #ifdef HAVE_CONFIG_H |
13 #include "../webp/config.h" | 13 #include "../webp/config.h" |
14 #endif | 14 #endif |
15 | 15 |
16 #include <math.h> | 16 #include <math.h> |
17 | 17 |
18 #include "./backward_references.h" | 18 #include "./backward_references.h" |
19 #include "./histogram.h" | 19 #include "./histogram.h" |
20 #include "../dsp/lossless.h" | 20 #include "../dsp/lossless.h" |
21 #include "../utils/utils.h" | 21 #include "../utils/utils.h" |
22 | 22 |
| 23 #define ALIGN_CST 15 |
| 24 #define DO_ALIGN(PTR) ((uintptr_t)((PTR) + ALIGN_CST) & ~ALIGN_CST) |
| 25 |
23 #define MAX_COST 1.e38 | 26 #define MAX_COST 1.e38 |
24 | 27 |
25 // Number of partitions for the three dominant (literal, red and blue) symbol | 28 // Number of partitions for the three dominant (literal, red and blue) symbol |
26 // costs. | 29 // costs. |
27 #define NUM_PARTITIONS 4 | 30 #define NUM_PARTITIONS 4 |
28 // The size of the bin-hash corresponding to the three dominant costs. | 31 // The size of the bin-hash corresponding to the three dominant costs. |
29 #define BIN_SIZE (NUM_PARTITIONS * NUM_PARTITIONS * NUM_PARTITIONS) | 32 #define BIN_SIZE (NUM_PARTITIONS * NUM_PARTITIONS * NUM_PARTITIONS) |
30 | 33 |
31 static void HistogramClear(VP8LHistogram* const p) { | 34 static void HistogramClear(VP8LHistogram* const p) { |
32 uint32_t* const literal = p->literal_; | 35 uint32_t* const literal = p->literal_; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 histo = (VP8LHistogram*)memory; | 97 histo = (VP8LHistogram*)memory; |
95 // literal_ won't necessary be aligned. | 98 // literal_ won't necessary be aligned. |
96 histo->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram)); | 99 histo->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram)); |
97 VP8LHistogramInit(histo, cache_bits); | 100 VP8LHistogramInit(histo, cache_bits); |
98 return histo; | 101 return histo; |
99 } | 102 } |
100 | 103 |
101 VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { | 104 VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { |
102 int i; | 105 int i; |
103 VP8LHistogramSet* set; | 106 VP8LHistogramSet* set; |
104 const size_t total_size = sizeof(*set) | 107 const int histo_size = VP8LGetHistogramSize(cache_bits); |
105 + sizeof(*set->histograms) * size | 108 const size_t total_size = |
106 + (size_t)VP8LGetHistogramSize(cache_bits) * size; | 109 sizeof(*set) + size * (sizeof(*set->histograms) + histo_size + ALIGN_CST); |
107 uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); | 110 uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); |
108 if (memory == NULL) return NULL; | 111 if (memory == NULL) return NULL; |
109 | 112 |
110 set = (VP8LHistogramSet*)memory; | 113 set = (VP8LHistogramSet*)memory; |
111 memory += sizeof(*set); | 114 memory += sizeof(*set); |
112 set->histograms = (VP8LHistogram**)memory; | 115 set->histograms = (VP8LHistogram**)memory; |
113 memory += size * sizeof(*set->histograms); | 116 memory += size * sizeof(*set->histograms); |
114 set->max_size = size; | 117 set->max_size = size; |
115 set->size = size; | 118 set->size = size; |
116 for (i = 0; i < size; ++i) { | 119 for (i = 0; i < size; ++i) { |
| 120 memory = (uint8_t*)DO_ALIGN(memory); |
117 set->histograms[i] = (VP8LHistogram*)memory; | 121 set->histograms[i] = (VP8LHistogram*)memory; |
118 // literal_ won't necessary be aligned. | 122 // literal_ won't necessary be aligned. |
119 set->histograms[i]->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram)); | 123 set->histograms[i]->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram)); |
120 VP8LHistogramInit(set->histograms[i], cache_bits); | 124 VP8LHistogramInit(set->histograms[i], cache_bits); |
121 // There's no padding/alignment between successive histograms. | 125 memory += histo_size; |
122 memory += VP8LGetHistogramSize(cache_bits); | |
123 } | 126 } |
124 return set; | 127 return set; |
125 } | 128 } |
126 | 129 |
127 // ----------------------------------------------------------------------------- | 130 // ----------------------------------------------------------------------------- |
128 | 131 |
129 void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo, | 132 void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo, |
130 const PixOrCopy* const v) { | 133 const PixOrCopy* const v) { |
131 if (PixOrCopyIsLiteral(v)) { | 134 if (PixOrCopyIsLiteral(v)) { |
132 ++histo->alpha_[PixOrCopyLiteral(v, 3)]; | 135 ++histo->alpha_[PixOrCopyLiteral(v, 3)]; |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 HistogramRemap(orig_histo, image_histo, histogram_symbols); | 735 HistogramRemap(orig_histo, image_histo, histogram_symbols); |
733 | 736 |
734 ok = 1; | 737 ok = 1; |
735 | 738 |
736 Error: | 739 Error: |
737 WebPSafeFree(bin_map); | 740 WebPSafeFree(bin_map); |
738 VP8LFreeHistogramSet(orig_histo); | 741 VP8LFreeHistogramSet(orig_histo); |
739 VP8LFreeHistogramSet(histos); | 742 VP8LFreeHistogramSet(histos); |
740 return ok; | 743 return ok; |
741 } | 744 } |
OLD | NEW |