OLD | NEW |
1 // Copyright 2011 Google Inc. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
7 // | 7 // |
8 // Everything about WebPDecBuffer | 8 // Everything about WebPDecBuffer |
9 // | 9 // |
10 // Author: Skal (pascal.massimino@gmail.com) | 10 // Author: Skal (pascal.massimino@gmail.com) |
11 | 11 |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include "vp8i.h" | 13 |
14 #include "webpi.h" | 14 #include "./vp8i.h" |
| 15 #include "./webpi.h" |
| 16 #include "../utils/utils.h" |
15 | 17 |
16 #if defined(__cplusplus) || defined(c_plusplus) | 18 #if defined(__cplusplus) || defined(c_plusplus) |
17 extern "C" { | 19 extern "C" { |
18 #endif | 20 #endif |
19 | 21 |
20 //------------------------------------------------------------------------------ | 22 //------------------------------------------------------------------------------ |
21 // WebPDecBuffer | 23 // WebPDecBuffer |
22 | 24 |
23 // Number of bytes per pixel for the different color-spaces. | 25 // Number of bytes per pixel for the different color-spaces. |
24 static const int kModeBpp[MODE_LAST] = { 3, 4, 3, 4, 4, 2, 2, 1, 1 }; | 26 static const int kModeBpp[MODE_LAST] = { |
| 27 3, 4, 3, 4, 4, 2, 2, |
| 28 4, 4, 4, 2, // pre-multiplied modes |
| 29 1, 1 }; |
| 30 |
| 31 // Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE. |
| 32 // Convert to an integer to handle both the unsigned/signed enum cases |
| 33 // without the need for casting to remove type limit warnings. |
| 34 static int IsValidColorspace(int webp_csp_mode) { |
| 35 return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST); |
| 36 } |
25 | 37 |
26 static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) { | 38 static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) { |
27 int ok = 1; | 39 int ok = 1; |
28 WEBP_CSP_MODE mode = buffer->colorspace; | 40 const WEBP_CSP_MODE mode = buffer->colorspace; |
29 const int width = buffer->width; | 41 const int width = buffer->width; |
30 const int height = buffer->height; | 42 const int height = buffer->height; |
31 if (mode >= MODE_YUV) { // YUV checks | 43 if (!IsValidColorspace(mode)) { |
| 44 ok = 0; |
| 45 } else if (!WebPIsRGBMode(mode)) { // YUV checks |
32 const WebPYUVABuffer* const buf = &buffer->u.YUVA; | 46 const WebPYUVABuffer* const buf = &buffer->u.YUVA; |
33 const uint64_t y_size = (uint64_t)buf->y_stride * height; | 47 const uint64_t y_size = (uint64_t)buf->y_stride * height; |
34 const uint64_t u_size = (uint64_t)buf->u_stride * ((height + 1) / 2); | 48 const uint64_t u_size = (uint64_t)buf->u_stride * ((height + 1) / 2); |
35 const uint64_t v_size = (uint64_t)buf->v_stride * ((height + 1) / 2); | 49 const uint64_t v_size = (uint64_t)buf->v_stride * ((height + 1) / 2); |
36 const uint64_t a_size = (uint64_t)buf->a_stride * height; | 50 const uint64_t a_size = (uint64_t)buf->a_stride * height; |
37 ok &= (y_size <= buf->y_size); | 51 ok &= (y_size <= buf->y_size); |
38 ok &= (u_size <= buf->u_size); | 52 ok &= (u_size <= buf->u_size); |
39 ok &= (v_size <= buf->v_size); | 53 ok &= (v_size <= buf->v_size); |
40 ok &= (a_size <= buf->a_size); | |
41 ok &= (buf->y_stride >= width); | 54 ok &= (buf->y_stride >= width); |
42 ok &= (buf->u_stride >= (width + 1) / 2); | 55 ok &= (buf->u_stride >= (width + 1) / 2); |
43 ok &= (buf->v_stride >= (width + 1) / 2); | 56 ok &= (buf->v_stride >= (width + 1) / 2); |
44 if (buf->a) { | 57 ok &= (buf->y != NULL); |
| 58 ok &= (buf->u != NULL); |
| 59 ok &= (buf->v != NULL); |
| 60 if (mode == MODE_YUVA) { |
45 ok &= (buf->a_stride >= width); | 61 ok &= (buf->a_stride >= width); |
| 62 ok &= (a_size <= buf->a_size); |
| 63 ok &= (buf->a != NULL); |
46 } | 64 } |
47 } else { // RGB checks | 65 } else { // RGB checks |
48 const WebPRGBABuffer* const buf = &buffer->u.RGBA; | 66 const WebPRGBABuffer* const buf = &buffer->u.RGBA; |
49 const uint64_t size = (uint64_t)buf->stride * height; | 67 const uint64_t size = (uint64_t)buf->stride * height; |
50 ok &= (size <= buf->size); | 68 ok &= (size <= buf->size); |
51 ok &= (buf->stride >= width * kModeBpp[mode]); | 69 ok &= (buf->stride >= width * kModeBpp[mode]); |
| 70 ok &= (buf->rgba != NULL); |
52 } | 71 } |
53 return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM; | 72 return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM; |
54 } | 73 } |
55 | 74 |
56 static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) { | 75 static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) { |
57 const int w = buffer->width; | 76 const int w = buffer->width; |
58 const int h = buffer->height; | 77 const int h = buffer->height; |
| 78 const WEBP_CSP_MODE mode = buffer->colorspace; |
59 | 79 |
60 if (w <= 0 || h <= 0) { | 80 if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) { |
61 return VP8_STATUS_INVALID_PARAM; | 81 return VP8_STATUS_INVALID_PARAM; |
62 } | 82 } |
63 | 83 |
64 if (!buffer->is_external_memory && buffer->private_memory == NULL) { | 84 if (!buffer->is_external_memory && buffer->private_memory == NULL) { |
65 uint8_t* output; | 85 uint8_t* output; |
66 WEBP_CSP_MODE mode = buffer->colorspace; | |
67 int stride; | |
68 int uv_stride = 0, a_stride = 0; | 86 int uv_stride = 0, a_stride = 0; |
69 uint64_t size, uv_size = 0, a_size = 0, total_size; | 87 uint64_t uv_size = 0, a_size = 0, total_size; |
70 // We need memory and it hasn't been allocated yet. | 88 // We need memory and it hasn't been allocated yet. |
71 // => initialize output buffer, now that dimensions are known. | 89 // => initialize output buffer, now that dimensions are known. |
72 stride = w * kModeBpp[mode]; | 90 const int stride = w * kModeBpp[mode]; |
73 size = (uint64_t)stride * h; | 91 const uint64_t size = (uint64_t)stride * h; |
74 | 92 |
75 if (mode >= MODE_YUV) { | 93 if (!WebPIsRGBMode(mode)) { |
76 uv_stride = (w + 1) / 2; | 94 uv_stride = (w + 1) / 2; |
77 uv_size = (uint64_t)uv_stride * ((h + 1) / 2); | 95 uv_size = (uint64_t)uv_stride * ((h + 1) / 2); |
78 if (mode == MODE_YUVA) { | 96 if (mode == MODE_YUVA) { |
79 a_stride = w; | 97 a_stride = w; |
80 a_size = (uint64_t)a_stride * h; | 98 a_size = (uint64_t)a_stride * h; |
81 } | 99 } |
82 } | 100 } |
83 total_size = size + 2 * uv_size + a_size; | 101 total_size = size + 2 * uv_size + a_size; |
84 | 102 |
85 // Security/sanity checks | 103 // Security/sanity checks |
86 if (((size_t)total_size != total_size) || (total_size >= (1ULL << 40))) { | 104 output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output)); |
87 return VP8_STATUS_INVALID_PARAM; | |
88 } | |
89 | |
90 buffer->private_memory = output = (uint8_t*)malloc((size_t)total_size); | |
91 if (output == NULL) { | 105 if (output == NULL) { |
92 return VP8_STATUS_OUT_OF_MEMORY; | 106 return VP8_STATUS_OUT_OF_MEMORY; |
93 } | 107 } |
| 108 buffer->private_memory = output; |
94 | 109 |
95 if (mode >= MODE_YUV) { // YUVA initialization | 110 if (!WebPIsRGBMode(mode)) { // YUVA initialization |
96 WebPYUVABuffer* const buf = &buffer->u.YUVA; | 111 WebPYUVABuffer* const buf = &buffer->u.YUVA; |
97 buf->y = output; | 112 buf->y = output; |
98 buf->y_stride = stride; | 113 buf->y_stride = stride; |
99 buf->y_size = (size_t)size; | 114 buf->y_size = (size_t)size; |
100 buf->u = output + size; | 115 buf->u = output + size; |
101 buf->u_stride = uv_stride; | 116 buf->u_stride = uv_stride; |
102 buf->u_size = (size_t)uv_size; | 117 buf->u_size = (size_t)uv_size; |
103 buf->v = output + size + uv_size; | 118 buf->v = output + size + uv_size; |
104 buf->v_stride = uv_stride; | 119 buf->v_stride = uv_stride; |
105 buf->v_size = (size_t)uv_size; | 120 buf->v_size = (size_t)uv_size; |
(...skipping 27 matching lines...) Expand all Loading... |
133 if (x < 0 || y < 0 || cw <= 0 || ch <= 0 || x + cw > w || y + ch > h) { | 148 if (x < 0 || y < 0 || cw <= 0 || ch <= 0 || x + cw > w || y + ch > h) { |
134 return VP8_STATUS_INVALID_PARAM; // out of frame boundary. | 149 return VP8_STATUS_INVALID_PARAM; // out of frame boundary. |
135 } | 150 } |
136 w = cw; | 151 w = cw; |
137 h = ch; | 152 h = ch; |
138 } | 153 } |
139 if (options->use_scaling) { | 154 if (options->use_scaling) { |
140 if (options->scaled_width <= 0 || options->scaled_height <= 0) { | 155 if (options->scaled_width <= 0 || options->scaled_height <= 0) { |
141 return VP8_STATUS_INVALID_PARAM; | 156 return VP8_STATUS_INVALID_PARAM; |
142 } | 157 } |
143 w = options->scaled_width; | 158 w = options->scaled_width; |
144 h = options->scaled_height; | 159 h = options->scaled_height; |
145 } | 160 } |
146 } | 161 } |
147 out->width = w; | 162 out->width = w; |
148 out->height = h; | 163 out->height = h; |
149 | 164 |
150 // Then, allocate buffer for real | 165 // Then, allocate buffer for real |
151 return AllocateBuffer(out); | 166 return AllocateBuffer(out); |
152 } | 167 } |
153 | 168 |
154 //------------------------------------------------------------------------------ | 169 //------------------------------------------------------------------------------ |
155 // constructors / destructors | 170 // constructors / destructors |
156 | 171 |
157 int WebPInitDecBufferInternal(WebPDecBuffer* const buffer, int version) { | 172 int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) { |
158 if (version != WEBP_DECODER_ABI_VERSION) return 0; // version mismatch | 173 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { |
159 if (!buffer) return 0; | 174 return 0; // version mismatch |
| 175 } |
| 176 if (buffer == NULL) return 0; |
160 memset(buffer, 0, sizeof(*buffer)); | 177 memset(buffer, 0, sizeof(*buffer)); |
161 return 1; | 178 return 1; |
162 } | 179 } |
163 | 180 |
164 void WebPFreeDecBuffer(WebPDecBuffer* const buffer) { | 181 void WebPFreeDecBuffer(WebPDecBuffer* buffer) { |
165 if (buffer) { | 182 if (buffer != NULL) { |
166 if (!buffer->is_external_memory) | 183 if (!buffer->is_external_memory) |
167 free(buffer->private_memory); | 184 free(buffer->private_memory); |
168 buffer->private_memory = NULL; | 185 buffer->private_memory = NULL; |
169 } | 186 } |
170 } | 187 } |
171 | 188 |
172 void WebPCopyDecBuffer(const WebPDecBuffer* const src, | 189 void WebPCopyDecBuffer(const WebPDecBuffer* const src, |
173 WebPDecBuffer* const dst) { | 190 WebPDecBuffer* const dst) { |
174 if (src && dst) { | 191 if (src != NULL && dst != NULL) { |
175 *dst = *src; | 192 *dst = *src; |
176 if (src->private_memory) { | 193 if (src->private_memory != NULL) { |
177 dst->is_external_memory = 1; // dst buffer doesn't own the memory. | 194 dst->is_external_memory = 1; // dst buffer doesn't own the memory. |
178 dst->private_memory = NULL; | 195 dst->private_memory = NULL; |
179 } | 196 } |
180 } | 197 } |
181 } | 198 } |
182 | 199 |
183 // Copy and transfer ownership from src to dst (beware of parameter order!) | 200 // Copy and transfer ownership from src to dst (beware of parameter order!) |
184 void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) { | 201 void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) { |
185 if (src && dst) { | 202 if (src != NULL && dst != NULL) { |
186 *dst = *src; | 203 *dst = *src; |
187 if (src->private_memory) { | 204 if (src->private_memory != NULL) { |
188 src->is_external_memory = 1; // src relinquishes ownership | 205 src->is_external_memory = 1; // src relinquishes ownership |
189 src->private_memory = NULL; | 206 src->private_memory = NULL; |
190 } | 207 } |
191 } | 208 } |
192 } | 209 } |
193 | 210 |
194 //------------------------------------------------------------------------------ | 211 //------------------------------------------------------------------------------ |
195 | 212 |
196 #if defined(__cplusplus) || defined(c_plusplus) | 213 #if defined(__cplusplus) || defined(c_plusplus) |
197 } // extern "C" | 214 } // extern "C" |
198 #endif | 215 #endif |
OLD | NEW |