| OLD | NEW |
| (Empty) |
| 1 // Copyright 2010 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 decoding functions for WEBP images. | |
| 11 // | |
| 12 // Author: Skal (pascal.massimino@gmail.com) | |
| 13 | |
| 14 #include <stdlib.h> | |
| 15 | |
| 16 #include "./vp8i.h" | |
| 17 #include "./vp8li.h" | |
| 18 #include "./webpi.h" | |
| 19 #include "../utils/utils.h" | |
| 20 #include "../webp/mux_types.h" // ALPHA_FLAG | |
| 21 | |
| 22 //------------------------------------------------------------------------------ | |
| 23 // RIFF layout is: | |
| 24 // Offset tag | |
| 25 // 0...3 "RIFF" 4-byte tag | |
| 26 // 4...7 size of image data (including metadata) starting at offset 8 | |
| 27 // 8...11 "WEBP" our form-type signature | |
| 28 // The RIFF container (12 bytes) is followed by appropriate chunks: | |
| 29 // 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format | |
| 30 // 16..19 size of the raw VP8 image data, starting at offset 20 | |
| 31 // 20.... the VP8 bytes | |
| 32 // Or, | |
| 33 // 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format | |
| 34 // 16..19 size of the raw VP8L image data, starting at offset 20 | |
| 35 // 20.... the VP8L bytes | |
| 36 // Or, | |
| 37 // 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk. | |
| 38 // 16..19 size of the VP8X chunk starting at offset 20. | |
| 39 // 20..23 VP8X flags bit-map corresponding to the chunk-types present. | |
| 40 // 24..26 Width of the Canvas Image. | |
| 41 // 27..29 Height of the Canvas Image. | |
| 42 // There can be extra chunks after the "VP8X" chunk (ICCP, FRGM, ANMF, VP8, | |
| 43 // VP8L, XMP, EXIF ...) | |
| 44 // All sizes are in little-endian order. | |
| 45 // Note: chunk data size must be padded to multiple of 2 when written. | |
| 46 | |
| 47 // Validates the RIFF container (if detected) and skips over it. | |
| 48 // If a RIFF container is detected, returns: | |
| 49 // VP8_STATUS_BITSTREAM_ERROR for invalid header, | |
| 50 // VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true, | |
| 51 // and VP8_STATUS_OK otherwise. | |
| 52 // In case there are not enough bytes (partial RIFF container), return 0 for | |
| 53 // *riff_size. Else return the RIFF size extracted from the header. | |
| 54 static VP8StatusCode ParseRIFF(const uint8_t** const data, | |
| 55 size_t* const data_size, int have_all_data, | |
| 56 size_t* const riff_size) { | |
| 57 assert(data != NULL); | |
| 58 assert(data_size != NULL); | |
| 59 assert(riff_size != NULL); | |
| 60 | |
| 61 *riff_size = 0; // Default: no RIFF present. | |
| 62 if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) { | |
| 63 if (memcmp(*data + 8, "WEBP", TAG_SIZE)) { | |
| 64 return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature. | |
| 65 } else { | |
| 66 const uint32_t size = GetLE32(*data + TAG_SIZE); | |
| 67 // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn"). | |
| 68 if (size < TAG_SIZE + CHUNK_HEADER_SIZE) { | |
| 69 return VP8_STATUS_BITSTREAM_ERROR; | |
| 70 } | |
| 71 if (size > MAX_CHUNK_PAYLOAD) { | |
| 72 return VP8_STATUS_BITSTREAM_ERROR; | |
| 73 } | |
| 74 if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) { | |
| 75 return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream. | |
| 76 } | |
| 77 // We have a RIFF container. Skip it. | |
| 78 *riff_size = size; | |
| 79 *data += RIFF_HEADER_SIZE; | |
| 80 *data_size -= RIFF_HEADER_SIZE; | |
| 81 } | |
| 82 } | |
| 83 return VP8_STATUS_OK; | |
| 84 } | |
| 85 | |
| 86 // Validates the VP8X header and skips over it. | |
| 87 // Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header, | |
| 88 // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and | |
| 89 // VP8_STATUS_OK otherwise. | |
| 90 // If a VP8X chunk is found, found_vp8x is set to true and *width_ptr, | |
| 91 // *height_ptr and *flags_ptr are set to the corresponding values extracted | |
| 92 // from the VP8X chunk. | |
| 93 static VP8StatusCode ParseVP8X(const uint8_t** const data, | |
| 94 size_t* const data_size, | |
| 95 int* const found_vp8x, | |
| 96 int* const width_ptr, int* const height_ptr, | |
| 97 uint32_t* const flags_ptr) { | |
| 98 const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE; | |
| 99 assert(data != NULL); | |
| 100 assert(data_size != NULL); | |
| 101 assert(found_vp8x != NULL); | |
| 102 | |
| 103 *found_vp8x = 0; | |
| 104 | |
| 105 if (*data_size < CHUNK_HEADER_SIZE) { | |
| 106 return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data. | |
| 107 } | |
| 108 | |
| 109 if (!memcmp(*data, "VP8X", TAG_SIZE)) { | |
| 110 int width, height; | |
| 111 uint32_t flags; | |
| 112 const uint32_t chunk_size = GetLE32(*data + TAG_SIZE); | |
| 113 if (chunk_size != VP8X_CHUNK_SIZE) { | |
| 114 return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size. | |
| 115 } | |
| 116 | |
| 117 // Verify if enough data is available to validate the VP8X chunk. | |
| 118 if (*data_size < vp8x_size) { | |
| 119 return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data. | |
| 120 } | |
| 121 flags = GetLE32(*data + 8); | |
| 122 width = 1 + GetLE24(*data + 12); | |
| 123 height = 1 + GetLE24(*data + 15); | |
| 124 if (width * (uint64_t)height >= MAX_IMAGE_AREA) { | |
| 125 return VP8_STATUS_BITSTREAM_ERROR; // image is too large | |
| 126 } | |
| 127 | |
| 128 if (flags_ptr != NULL) *flags_ptr = flags; | |
| 129 if (width_ptr != NULL) *width_ptr = width; | |
| 130 if (height_ptr != NULL) *height_ptr = height; | |
| 131 // Skip over VP8X header bytes. | |
| 132 *data += vp8x_size; | |
| 133 *data_size -= vp8x_size; | |
| 134 *found_vp8x = 1; | |
| 135 } | |
| 136 return VP8_STATUS_OK; | |
| 137 } | |
| 138 | |
| 139 // Skips to the next VP8/VP8L chunk header in the data given the size of the | |
| 140 // RIFF chunk 'riff_size'. | |
| 141 // Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered, | |
| 142 // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and | |
| 143 // VP8_STATUS_OK otherwise. | |
| 144 // If an alpha chunk is found, *alpha_data and *alpha_size are set | |
| 145 // appropriately. | |
| 146 static VP8StatusCode ParseOptionalChunks(const uint8_t** const data, | |
| 147 size_t* const data_size, | |
| 148 size_t const riff_size, | |
| 149 const uint8_t** const alpha_data, | |
| 150 size_t* const alpha_size) { | |
| 151 const uint8_t* buf; | |
| 152 size_t buf_size; | |
| 153 uint32_t total_size = TAG_SIZE + // "WEBP". | |
| 154 CHUNK_HEADER_SIZE + // "VP8Xnnnn". | |
| 155 VP8X_CHUNK_SIZE; // data. | |
| 156 assert(data != NULL); | |
| 157 assert(data_size != NULL); | |
| 158 buf = *data; | |
| 159 buf_size = *data_size; | |
| 160 | |
| 161 assert(alpha_data != NULL); | |
| 162 assert(alpha_size != NULL); | |
| 163 *alpha_data = NULL; | |
| 164 *alpha_size = 0; | |
| 165 | |
| 166 while (1) { | |
| 167 uint32_t chunk_size; | |
| 168 uint32_t disk_chunk_size; // chunk_size with padding | |
| 169 | |
| 170 *data = buf; | |
| 171 *data_size = buf_size; | |
| 172 | |
| 173 if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data. | |
| 174 return VP8_STATUS_NOT_ENOUGH_DATA; | |
| 175 } | |
| 176 | |
| 177 chunk_size = GetLE32(buf + TAG_SIZE); | |
| 178 if (chunk_size > MAX_CHUNK_PAYLOAD) { | |
| 179 return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size. | |
| 180 } | |
| 181 // For odd-sized chunk-payload, there's one byte padding at the end. | |
| 182 disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1; | |
| 183 total_size += disk_chunk_size; | |
| 184 | |
| 185 // Check that total bytes skipped so far does not exceed riff_size. | |
| 186 if (riff_size > 0 && (total_size > riff_size)) { | |
| 187 return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size. | |
| 188 } | |
| 189 | |
| 190 // Start of a (possibly incomplete) VP8/VP8L chunk implies that we have | |
| 191 // parsed all the optional chunks. | |
| 192 // Note: This check must occur before the check 'buf_size < disk_chunk_size' | |
| 193 // below to allow incomplete VP8/VP8L chunks. | |
| 194 if (!memcmp(buf, "VP8 ", TAG_SIZE) || | |
| 195 !memcmp(buf, "VP8L", TAG_SIZE)) { | |
| 196 return VP8_STATUS_OK; | |
| 197 } | |
| 198 | |
| 199 if (buf_size < disk_chunk_size) { // Insufficient data. | |
| 200 return VP8_STATUS_NOT_ENOUGH_DATA; | |
| 201 } | |
| 202 | |
| 203 if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header. | |
| 204 *alpha_data = buf + CHUNK_HEADER_SIZE; | |
| 205 *alpha_size = chunk_size; | |
| 206 } | |
| 207 | |
| 208 // We have a full and valid chunk; skip it. | |
| 209 buf += disk_chunk_size; | |
| 210 buf_size -= disk_chunk_size; | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 // Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it. | |
| 215 // Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than | |
| 216 // riff_size) VP8/VP8L header, | |
| 217 // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and | |
| 218 // VP8_STATUS_OK otherwise. | |
| 219 // If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes | |
| 220 // extracted from the VP8/VP8L chunk header. | |
| 221 // The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data. | |
| 222 static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr, | |
| 223 size_t* const data_size, int have_all_data, | |
| 224 size_t riff_size, size_t* const chunk_size, | |
| 225 int* const is_lossless) { | |
| 226 const uint8_t* const data = *data_ptr; | |
| 227 const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE); | |
| 228 const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE); | |
| 229 const uint32_t minimal_size = | |
| 230 TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR | |
| 231 // "WEBP" + "VP8Lnnnn" | |
| 232 assert(data != NULL); | |
| 233 assert(data_size != NULL); | |
| 234 assert(chunk_size != NULL); | |
| 235 assert(is_lossless != NULL); | |
| 236 | |
| 237 if (*data_size < CHUNK_HEADER_SIZE) { | |
| 238 return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data. | |
| 239 } | |
| 240 | |
| 241 if (is_vp8 || is_vp8l) { | |
| 242 // Bitstream contains VP8/VP8L header. | |
| 243 const uint32_t size = GetLE32(data + TAG_SIZE); | |
| 244 if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) { | |
| 245 return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information. | |
| 246 } | |
| 247 if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) { | |
| 248 return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream. | |
| 249 } | |
| 250 // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header. | |
| 251 *chunk_size = size; | |
| 252 *data_ptr += CHUNK_HEADER_SIZE; | |
| 253 *data_size -= CHUNK_HEADER_SIZE; | |
| 254 *is_lossless = is_vp8l; | |
| 255 } else { | |
| 256 // Raw VP8/VP8L bitstream (no header). | |
| 257 *is_lossless = VP8LCheckSignature(data, *data_size); | |
| 258 *chunk_size = *data_size; | |
| 259 } | |
| 260 | |
| 261 return VP8_STATUS_OK; | |
| 262 } | |
| 263 | |
| 264 //------------------------------------------------------------------------------ | |
| 265 | |
| 266 // Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on | |
| 267 // 'data'. All the output parameters may be NULL. If 'headers' is NULL only the | |
| 268 // minimal amount will be read to fetch the remaining parameters. | |
| 269 // If 'headers' is non-NULL this function will attempt to locate both alpha | |
| 270 // data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L). | |
| 271 // Note: The following chunk sequences (before the raw VP8/VP8L data) are | |
| 272 // considered valid by this function: | |
| 273 // RIFF + VP8(L) | |
| 274 // RIFF + VP8X + (optional chunks) + VP8(L) | |
| 275 // ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose. | |
| 276 // VP8(L) <-- Not a valid WebP format: only allowed for internal purpose. | |
| 277 static VP8StatusCode ParseHeadersInternal(const uint8_t* data, | |
| 278 size_t data_size, | |
| 279 int* const width, | |
| 280 int* const height, | |
| 281 int* const has_alpha, | |
| 282 int* const has_animation, | |
| 283 int* const format, | |
| 284 WebPHeaderStructure* const headers) { | |
| 285 int canvas_width = 0; | |
| 286 int canvas_height = 0; | |
| 287 int image_width = 0; | |
| 288 int image_height = 0; | |
| 289 int found_riff = 0; | |
| 290 int found_vp8x = 0; | |
| 291 int animation_present = 0; | |
| 292 int fragments_present = 0; | |
| 293 const int have_all_data = (headers != NULL) ? headers->have_all_data : 0; | |
| 294 | |
| 295 VP8StatusCode status; | |
| 296 WebPHeaderStructure hdrs; | |
| 297 | |
| 298 if (data == NULL || data_size < RIFF_HEADER_SIZE) { | |
| 299 return VP8_STATUS_NOT_ENOUGH_DATA; | |
| 300 } | |
| 301 memset(&hdrs, 0, sizeof(hdrs)); | |
| 302 hdrs.data = data; | |
| 303 hdrs.data_size = data_size; | |
| 304 | |
| 305 // Skip over RIFF header. | |
| 306 status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size); | |
| 307 if (status != VP8_STATUS_OK) { | |
| 308 return status; // Wrong RIFF header / insufficient data. | |
| 309 } | |
| 310 found_riff = (hdrs.riff_size > 0); | |
| 311 | |
| 312 // Skip over VP8X. | |
| 313 { | |
| 314 uint32_t flags = 0; | |
| 315 status = ParseVP8X(&data, &data_size, &found_vp8x, | |
| 316 &canvas_width, &canvas_height, &flags); | |
| 317 if (status != VP8_STATUS_OK) { | |
| 318 return status; // Wrong VP8X / insufficient data. | |
| 319 } | |
| 320 animation_present = !!(flags & ANIMATION_FLAG); | |
| 321 fragments_present = !!(flags & FRAGMENTS_FLAG); | |
| 322 if (!found_riff && found_vp8x) { | |
| 323 // Note: This restriction may be removed in the future, if it becomes | |
| 324 // necessary to send VP8X chunk to the decoder. | |
| 325 return VP8_STATUS_BITSTREAM_ERROR; | |
| 326 } | |
| 327 if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG); | |
| 328 if (has_animation != NULL) *has_animation = animation_present; | |
| 329 if (format != NULL) *format = 0; // default = undefined | |
| 330 | |
| 331 if (found_vp8x && (animation_present || fragments_present) && | |
| 332 headers == NULL) { | |
| 333 if (width != NULL) *width = canvas_width; | |
| 334 if (height != NULL) *height = canvas_height; | |
| 335 return VP8_STATUS_OK; // Just return features from VP8X header. | |
| 336 } | |
| 337 } | |
| 338 | |
| 339 if (data_size < TAG_SIZE) return VP8_STATUS_NOT_ENOUGH_DATA; | |
| 340 | |
| 341 // Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH". | |
| 342 if ((found_riff && found_vp8x) || | |
| 343 (!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) { | |
| 344 status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size, | |
| 345 &hdrs.alpha_data, &hdrs.alpha_data_size); | |
| 346 if (status != VP8_STATUS_OK) { | |
| 347 return status; // Found an invalid chunk size / insufficient data. | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 // Skip over VP8/VP8L header. | |
| 352 status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size, | |
| 353 &hdrs.compressed_size, &hdrs.is_lossless); | |
| 354 if (status != VP8_STATUS_OK) { | |
| 355 return status; // Wrong VP8/VP8L chunk-header / insufficient data. | |
| 356 } | |
| 357 if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) { | |
| 358 return VP8_STATUS_BITSTREAM_ERROR; | |
| 359 } | |
| 360 | |
| 361 if (format != NULL && !(animation_present || fragments_present)) { | |
| 362 *format = hdrs.is_lossless ? 2 : 1; | |
| 363 } | |
| 364 | |
| 365 if (!hdrs.is_lossless) { | |
| 366 if (data_size < VP8_FRAME_HEADER_SIZE) { | |
| 367 return VP8_STATUS_NOT_ENOUGH_DATA; | |
| 368 } | |
| 369 // Validates raw VP8 data. | |
| 370 if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size, | |
| 371 &image_width, &image_height)) { | |
| 372 return VP8_STATUS_BITSTREAM_ERROR; | |
| 373 } | |
| 374 } else { | |
| 375 if (data_size < VP8L_FRAME_HEADER_SIZE) { | |
| 376 return VP8_STATUS_NOT_ENOUGH_DATA; | |
| 377 } | |
| 378 // Validates raw VP8L data. | |
| 379 if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) { | |
| 380 return VP8_STATUS_BITSTREAM_ERROR; | |
| 381 } | |
| 382 } | |
| 383 // Validates image size coherency. | |
| 384 if (found_vp8x) { | |
| 385 if (canvas_width != image_width || canvas_height != image_height) { | |
| 386 return VP8_STATUS_BITSTREAM_ERROR; | |
| 387 } | |
| 388 } | |
| 389 if (width != NULL) *width = image_width; | |
| 390 if (height != NULL) *height = image_height; | |
| 391 if (has_alpha != NULL) { | |
| 392 // If the data did not contain a VP8X/VP8L chunk the only definitive way | |
| 393 // to set this is by looking for alpha data (from an ALPH chunk). | |
| 394 *has_alpha |= (hdrs.alpha_data != NULL); | |
| 395 } | |
| 396 if (headers != NULL) { | |
| 397 *headers = hdrs; | |
| 398 headers->offset = data - headers->data; | |
| 399 assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD); | |
| 400 assert(headers->offset == headers->data_size - data_size); | |
| 401 } | |
| 402 return VP8_STATUS_OK; // Return features from VP8 header. | |
| 403 } | |
| 404 | |
| 405 VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) { | |
| 406 // status is marked volatile as a workaround for a clang-3.8 (aarch64) bug | |
| 407 volatile VP8StatusCode status; | |
| 408 int has_animation = 0; | |
| 409 assert(headers != NULL); | |
| 410 // fill out headers, ignore width/height/has_alpha. | |
| 411 status = ParseHeadersInternal(headers->data, headers->data_size, | |
| 412 NULL, NULL, NULL, &has_animation, | |
| 413 NULL, headers); | |
| 414 if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) { | |
| 415 // TODO(jzern): full support of animation frames will require API additions. | |
| 416 if (has_animation) { | |
| 417 status = VP8_STATUS_UNSUPPORTED_FEATURE; | |
| 418 } | |
| 419 } | |
| 420 return status; | |
| 421 } | |
| 422 | |
| 423 //------------------------------------------------------------------------------ | |
| 424 // WebPDecParams | |
| 425 | |
| 426 void WebPResetDecParams(WebPDecParams* const params) { | |
| 427 if (params != NULL) { | |
| 428 memset(params, 0, sizeof(*params)); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 //------------------------------------------------------------------------------ | |
| 433 // "Into" decoding variants | |
| 434 | |
| 435 // Main flow | |
| 436 static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size, | |
| 437 WebPDecParams* const params) { | |
| 438 VP8StatusCode status; | |
| 439 VP8Io io; | |
| 440 WebPHeaderStructure headers; | |
| 441 | |
| 442 headers.data = data; | |
| 443 headers.data_size = data_size; | |
| 444 headers.have_all_data = 1; | |
| 445 status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks. | |
| 446 if (status != VP8_STATUS_OK) { | |
| 447 return status; | |
| 448 } | |
| 449 | |
| 450 assert(params != NULL); | |
| 451 VP8InitIo(&io); | |
| 452 io.data = headers.data + headers.offset; | |
| 453 io.data_size = headers.data_size - headers.offset; | |
| 454 WebPInitCustomIo(params, &io); // Plug the I/O functions. | |
| 455 | |
| 456 if (!headers.is_lossless) { | |
| 457 VP8Decoder* const dec = VP8New(); | |
| 458 if (dec == NULL) { | |
| 459 return VP8_STATUS_OUT_OF_MEMORY; | |
| 460 } | |
| 461 dec->alpha_data_ = headers.alpha_data; | |
| 462 dec->alpha_data_size_ = headers.alpha_data_size; | |
| 463 | |
| 464 // Decode bitstream header, update io->width/io->height. | |
| 465 if (!VP8GetHeaders(dec, &io)) { | |
| 466 status = dec->status_; // An error occurred. Grab error status. | |
| 467 } else { | |
| 468 // Allocate/check output buffers. | |
| 469 status = WebPAllocateDecBuffer(io.width, io.height, params->options, | |
| 470 params->output); | |
| 471 if (status == VP8_STATUS_OK) { // Decode | |
| 472 // This change must be done before calling VP8Decode() | |
| 473 dec->mt_method_ = VP8GetThreadMethod(params->options, &headers, | |
| 474 io.width, io.height); | |
| 475 VP8InitDithering(params->options, dec); | |
| 476 if (!VP8Decode(dec, &io)) { | |
| 477 status = dec->status_; | |
| 478 } | |
| 479 } | |
| 480 } | |
| 481 VP8Delete(dec); | |
| 482 } else { | |
| 483 VP8LDecoder* const dec = VP8LNew(); | |
| 484 if (dec == NULL) { | |
| 485 return VP8_STATUS_OUT_OF_MEMORY; | |
| 486 } | |
| 487 if (!VP8LDecodeHeader(dec, &io)) { | |
| 488 status = dec->status_; // An error occurred. Grab error status. | |
| 489 } else { | |
| 490 // Allocate/check output buffers. | |
| 491 status = WebPAllocateDecBuffer(io.width, io.height, params->options, | |
| 492 params->output); | |
| 493 if (status == VP8_STATUS_OK) { // Decode | |
| 494 if (!VP8LDecodeImage(dec)) { | |
| 495 status = dec->status_; | |
| 496 } | |
| 497 } | |
| 498 } | |
| 499 VP8LDelete(dec); | |
| 500 } | |
| 501 | |
| 502 if (status != VP8_STATUS_OK) { | |
| 503 WebPFreeDecBuffer(params->output); | |
| 504 } else { | |
| 505 if (params->options != NULL && params->options->flip) { | |
| 506 // This restores the original stride values if options->flip was used | |
| 507 // during the call to WebPAllocateDecBuffer above. | |
| 508 status = WebPFlipBuffer(params->output); | |
| 509 } | |
| 510 } | |
| 511 return status; | |
| 512 } | |
| 513 | |
| 514 // Helpers | |
| 515 static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace, | |
| 516 const uint8_t* const data, | |
| 517 size_t data_size, | |
| 518 uint8_t* const rgba, | |
| 519 int stride, size_t size) { | |
| 520 WebPDecParams params; | |
| 521 WebPDecBuffer buf; | |
| 522 if (rgba == NULL) { | |
| 523 return NULL; | |
| 524 } | |
| 525 WebPInitDecBuffer(&buf); | |
| 526 WebPResetDecParams(¶ms); | |
| 527 params.output = &buf; | |
| 528 buf.colorspace = colorspace; | |
| 529 buf.u.RGBA.rgba = rgba; | |
| 530 buf.u.RGBA.stride = stride; | |
| 531 buf.u.RGBA.size = size; | |
| 532 buf.is_external_memory = 1; | |
| 533 if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { | |
| 534 return NULL; | |
| 535 } | |
| 536 return rgba; | |
| 537 } | |
| 538 | |
| 539 uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size, | |
| 540 uint8_t* output, size_t size, int stride) { | |
| 541 return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size); | |
| 542 } | |
| 543 | |
| 544 uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size, | |
| 545 uint8_t* output, size_t size, int stride) { | |
| 546 return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size); | |
| 547 } | |
| 548 | |
| 549 uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size, | |
| 550 uint8_t* output, size_t size, int stride) { | |
| 551 return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size); | |
| 552 } | |
| 553 | |
| 554 uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size, | |
| 555 uint8_t* output, size_t size, int stride) { | |
| 556 return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size); | |
| 557 } | |
| 558 | |
| 559 uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size, | |
| 560 uint8_t* output, size_t size, int stride) { | |
| 561 return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size); | |
| 562 } | |
| 563 | |
| 564 uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size, | |
| 565 uint8_t* luma, size_t luma_size, int luma_stride, | |
| 566 uint8_t* u, size_t u_size, int u_stride, | |
| 567 uint8_t* v, size_t v_size, int v_stride) { | |
| 568 WebPDecParams params; | |
| 569 WebPDecBuffer output; | |
| 570 if (luma == NULL) return NULL; | |
| 571 WebPInitDecBuffer(&output); | |
| 572 WebPResetDecParams(¶ms); | |
| 573 params.output = &output; | |
| 574 output.colorspace = MODE_YUV; | |
| 575 output.u.YUVA.y = luma; | |
| 576 output.u.YUVA.y_stride = luma_stride; | |
| 577 output.u.YUVA.y_size = luma_size; | |
| 578 output.u.YUVA.u = u; | |
| 579 output.u.YUVA.u_stride = u_stride; | |
| 580 output.u.YUVA.u_size = u_size; | |
| 581 output.u.YUVA.v = v; | |
| 582 output.u.YUVA.v_stride = v_stride; | |
| 583 output.u.YUVA.v_size = v_size; | |
| 584 output.is_external_memory = 1; | |
| 585 if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { | |
| 586 return NULL; | |
| 587 } | |
| 588 return luma; | |
| 589 } | |
| 590 | |
| 591 //------------------------------------------------------------------------------ | |
| 592 | |
| 593 static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data, | |
| 594 size_t data_size, int* const width, int* const height, | |
| 595 WebPDecBuffer* const keep_info) { | |
| 596 WebPDecParams params; | |
| 597 WebPDecBuffer output; | |
| 598 | |
| 599 WebPInitDecBuffer(&output); | |
| 600 WebPResetDecParams(¶ms); | |
| 601 params.output = &output; | |
| 602 output.colorspace = mode; | |
| 603 | |
| 604 // Retrieve (and report back) the required dimensions from bitstream. | |
| 605 if (!WebPGetInfo(data, data_size, &output.width, &output.height)) { | |
| 606 return NULL; | |
| 607 } | |
| 608 if (width != NULL) *width = output.width; | |
| 609 if (height != NULL) *height = output.height; | |
| 610 | |
| 611 // Decode | |
| 612 if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { | |
| 613 return NULL; | |
| 614 } | |
| 615 if (keep_info != NULL) { // keep track of the side-info | |
| 616 WebPCopyDecBuffer(&output, keep_info); | |
| 617 } | |
| 618 // return decoded samples (don't clear 'output'!) | |
| 619 return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y; | |
| 620 } | |
| 621 | |
| 622 uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, | |
| 623 int* width, int* height) { | |
| 624 return Decode(MODE_RGB, data, data_size, width, height, NULL); | |
| 625 } | |
| 626 | |
| 627 uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size, | |
| 628 int* width, int* height) { | |
| 629 return Decode(MODE_RGBA, data, data_size, width, height, NULL); | |
| 630 } | |
| 631 | |
| 632 uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size, | |
| 633 int* width, int* height) { | |
| 634 return Decode(MODE_ARGB, data, data_size, width, height, NULL); | |
| 635 } | |
| 636 | |
| 637 uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size, | |
| 638 int* width, int* height) { | |
| 639 return Decode(MODE_BGR, data, data_size, width, height, NULL); | |
| 640 } | |
| 641 | |
| 642 uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size, | |
| 643 int* width, int* height) { | |
| 644 return Decode(MODE_BGRA, data, data_size, width, height, NULL); | |
| 645 } | |
| 646 | |
| 647 uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size, | |
| 648 int* width, int* height, uint8_t** u, uint8_t** v, | |
| 649 int* stride, int* uv_stride) { | |
| 650 WebPDecBuffer output; // only to preserve the side-infos | |
| 651 uint8_t* const out = Decode(MODE_YUV, data, data_size, | |
| 652 width, height, &output); | |
| 653 | |
| 654 if (out != NULL) { | |
| 655 const WebPYUVABuffer* const buf = &output.u.YUVA; | |
| 656 *u = buf->u; | |
| 657 *v = buf->v; | |
| 658 *stride = buf->y_stride; | |
| 659 *uv_stride = buf->u_stride; | |
| 660 assert(buf->u_stride == buf->v_stride); | |
| 661 } | |
| 662 return out; | |
| 663 } | |
| 664 | |
| 665 static void DefaultFeatures(WebPBitstreamFeatures* const features) { | |
| 666 assert(features != NULL); | |
| 667 memset(features, 0, sizeof(*features)); | |
| 668 } | |
| 669 | |
| 670 static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size, | |
| 671 WebPBitstreamFeatures* const features) { | |
| 672 if (features == NULL || data == NULL) { | |
| 673 return VP8_STATUS_INVALID_PARAM; | |
| 674 } | |
| 675 DefaultFeatures(features); | |
| 676 | |
| 677 // Only parse enough of the data to retrieve the features. | |
| 678 return ParseHeadersInternal(data, data_size, | |
| 679 &features->width, &features->height, | |
| 680 &features->has_alpha, &features->has_animation, | |
| 681 &features->format, NULL); | |
| 682 } | |
| 683 | |
| 684 //------------------------------------------------------------------------------ | |
| 685 // WebPGetInfo() | |
| 686 | |
| 687 int WebPGetInfo(const uint8_t* data, size_t data_size, | |
| 688 int* width, int* height) { | |
| 689 WebPBitstreamFeatures features; | |
| 690 | |
| 691 if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) { | |
| 692 return 0; | |
| 693 } | |
| 694 | |
| 695 if (width != NULL) { | |
| 696 *width = features.width; | |
| 697 } | |
| 698 if (height != NULL) { | |
| 699 *height = features.height; | |
| 700 } | |
| 701 | |
| 702 return 1; | |
| 703 } | |
| 704 | |
| 705 //------------------------------------------------------------------------------ | |
| 706 // Advance decoding API | |
| 707 | |
| 708 int WebPInitDecoderConfigInternal(WebPDecoderConfig* config, | |
| 709 int version) { | |
| 710 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { | |
| 711 return 0; // version mismatch | |
| 712 } | |
| 713 if (config == NULL) { | |
| 714 return 0; | |
| 715 } | |
| 716 memset(config, 0, sizeof(*config)); | |
| 717 DefaultFeatures(&config->input); | |
| 718 WebPInitDecBuffer(&config->output); | |
| 719 return 1; | |
| 720 } | |
| 721 | |
| 722 VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size, | |
| 723 WebPBitstreamFeatures* features, | |
| 724 int version) { | |
| 725 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { | |
| 726 return VP8_STATUS_INVALID_PARAM; // version mismatch | |
| 727 } | |
| 728 if (features == NULL) { | |
| 729 return VP8_STATUS_INVALID_PARAM; | |
| 730 } | |
| 731 return GetFeatures(data, data_size, features); | |
| 732 } | |
| 733 | |
| 734 VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size, | |
| 735 WebPDecoderConfig* config) { | |
| 736 WebPDecParams params; | |
| 737 VP8StatusCode status; | |
| 738 | |
| 739 if (config == NULL) { | |
| 740 return VP8_STATUS_INVALID_PARAM; | |
| 741 } | |
| 742 | |
| 743 status = GetFeatures(data, data_size, &config->input); | |
| 744 if (status != VP8_STATUS_OK) { | |
| 745 if (status == VP8_STATUS_NOT_ENOUGH_DATA) { | |
| 746 return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error. | |
| 747 } | |
| 748 return status; | |
| 749 } | |
| 750 | |
| 751 WebPResetDecParams(¶ms); | |
| 752 params.options = &config->options; | |
| 753 params.output = &config->output; | |
| 754 if (WebPAvoidSlowMemory(params.output, &config->input)) { | |
| 755 // decoding to slow memory: use a temporary in-mem buffer to decode into. | |
| 756 WebPDecBuffer in_mem_buffer; | |
| 757 WebPInitDecBuffer(&in_mem_buffer); | |
| 758 in_mem_buffer.colorspace = config->output.colorspace; | |
| 759 in_mem_buffer.width = config->input.width; | |
| 760 in_mem_buffer.height = config->input.height; | |
| 761 params.output = &in_mem_buffer; | |
| 762 status = DecodeInto(data, data_size, ¶ms); | |
| 763 if (status == VP8_STATUS_OK) { // do the slow-copy | |
| 764 status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output); | |
| 765 } | |
| 766 WebPFreeDecBuffer(&in_mem_buffer); | |
| 767 } else { | |
| 768 status = DecodeInto(data, data_size, ¶ms); | |
| 769 } | |
| 770 | |
| 771 return status; | |
| 772 } | |
| 773 | |
| 774 //------------------------------------------------------------------------------ | |
| 775 // Cropping and rescaling. | |
| 776 | |
| 777 int WebPIoInitFromOptions(const WebPDecoderOptions* const options, | |
| 778 VP8Io* const io, WEBP_CSP_MODE src_colorspace) { | |
| 779 const int W = io->width; | |
| 780 const int H = io->height; | |
| 781 int x = 0, y = 0, w = W, h = H; | |
| 782 | |
| 783 // Cropping | |
| 784 io->use_cropping = (options != NULL) && (options->use_cropping > 0); | |
| 785 if (io->use_cropping) { | |
| 786 w = options->crop_width; | |
| 787 h = options->crop_height; | |
| 788 x = options->crop_left; | |
| 789 y = options->crop_top; | |
| 790 if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420 | |
| 791 x &= ~1; | |
| 792 y &= ~1; | |
| 793 } | |
| 794 if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) { | |
| 795 return 0; // out of frame boundary error | |
| 796 } | |
| 797 } | |
| 798 io->crop_left = x; | |
| 799 io->crop_top = y; | |
| 800 io->crop_right = x + w; | |
| 801 io->crop_bottom = y + h; | |
| 802 io->mb_w = w; | |
| 803 io->mb_h = h; | |
| 804 | |
| 805 // Scaling | |
| 806 io->use_scaling = (options != NULL) && (options->use_scaling > 0); | |
| 807 if (io->use_scaling) { | |
| 808 int scaled_width = options->scaled_width; | |
| 809 int scaled_height = options->scaled_height; | |
| 810 if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) { | |
| 811 return 0; | |
| 812 } | |
| 813 io->scaled_width = scaled_width; | |
| 814 io->scaled_height = scaled_height; | |
| 815 } | |
| 816 | |
| 817 // Filter | |
| 818 io->bypass_filtering = (options != NULL) && options->bypass_filtering; | |
| 819 | |
| 820 // Fancy upsampler | |
| 821 #ifdef FANCY_UPSAMPLING | |
| 822 io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling); | |
| 823 #endif | |
| 824 | |
| 825 if (io->use_scaling) { | |
| 826 // disable filter (only for large downscaling ratio). | |
| 827 io->bypass_filtering = (io->scaled_width < W * 3 / 4) && | |
| 828 (io->scaled_height < H * 3 / 4); | |
| 829 io->fancy_upsampling = 0; | |
| 830 } | |
| 831 return 1; | |
| 832 } | |
| 833 | |
| 834 //------------------------------------------------------------------------------ | |
| OLD | NEW |