| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <stdlib.h> |
| 15 |
|
| 16 |
#include "src/dec/vp8_dec.h" |
| 17 |
#include "src/dec/vp8i_dec.h" |
| 18 |
#include "src/dec/vp8li_dec.h" |
| 19 |
#include "src/dec/webpi_dec.h" |
| 20 |
#include "src/utils/utils.h" |
| 21 |
#include "src/webp/mux_types.h" |
| 22 |
#include "src/webp/decode.h" |
| 23 |
#include "src/webp/types.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static VP8StatusCode ParseRIFF(const uint8_t** const data, |
| 58 |
size_t* const data_size, int have_all_data, |
| 59 |
size_t* const riff_size) { |
| 60 |
assert(data != NULL); |
| 61 |
assert(data_size != NULL); |
| 62 |
assert(riff_size != NULL); |
| 63 |
|
| 64 |
*riff_size = 0; |
| 65 |
if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) { |
| 66 |
if (memcmp(*data + 8, "WEBP", TAG_SIZE)) { |
| 67 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 68 |
} else { |
| 69 |
const uint32_t size = GetLE32(*data + TAG_SIZE); |
| 70 |
|
| 71 |
if (size < TAG_SIZE + CHUNK_HEADER_SIZE) { |
| 72 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 73 |
} |
| 74 |
if (size > MAX_CHUNK_PAYLOAD) { |
| 75 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 76 |
} |
| 77 |
if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) { |
| 78 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 79 |
} |
| 80 |
|
| 81 |
*riff_size = size; |
| 82 |
*data += RIFF_HEADER_SIZE; |
| 83 |
*data_size -= RIFF_HEADER_SIZE; |
| 84 |
} |
| 85 |
} |
| 86 |
return VP8_STATUS_OK; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
static VP8StatusCode ParseVP8X(const uint8_t** const data, |
| 97 |
size_t* const data_size, |
| 98 |
int* const found_vp8x, |
| 99 |
int* const width_ptr, int* const height_ptr, |
| 100 |
uint32_t* const flags_ptr) { |
| 101 |
const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE; |
| 102 |
assert(data != NULL); |
| 103 |
assert(data_size != NULL); |
| 104 |
assert(found_vp8x != NULL); |
| 105 |
|
| 106 |
*found_vp8x = 0; |
| 107 |
|
| 108 |
if (*data_size < CHUNK_HEADER_SIZE) { |
| 109 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 110 |
} |
| 111 |
|
| 112 |
if (!memcmp(*data, "VP8X", TAG_SIZE)) { |
| 113 |
int width, height; |
| 114 |
uint32_t flags; |
| 115 |
const uint32_t chunk_size = GetLE32(*data + TAG_SIZE); |
| 116 |
if (chunk_size != VP8X_CHUNK_SIZE) { |
| 117 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if (*data_size < vp8x_size) { |
| 122 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 123 |
} |
| 124 |
flags = GetLE32(*data + 8); |
| 125 |
width = 1 + GetLE24(*data + 12); |
| 126 |
height = 1 + GetLE24(*data + 15); |
| 127 |
if (width * (uint64_t)height >= MAX_IMAGE_AREA) { |
| 128 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 129 |
} |
| 130 |
|
| 131 |
if (flags_ptr != NULL) *flags_ptr = flags; |
| 132 |
if (width_ptr != NULL) *width_ptr = width; |
| 133 |
if (height_ptr != NULL) *height_ptr = height; |
| 134 |
|
| 135 |
*data += vp8x_size; |
| 136 |
*data_size -= vp8x_size; |
| 137 |
*found_vp8x = 1; |
| 138 |
} |
| 139 |
return VP8_STATUS_OK; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static VP8StatusCode ParseOptionalChunks(const uint8_t** const data, |
| 150 |
size_t* const data_size, |
| 151 |
size_t const riff_size, |
| 152 |
const uint8_t** const alpha_data, |
| 153 |
size_t* const alpha_size) { |
| 154 |
const uint8_t* buf; |
| 155 |
size_t buf_size; |
| 156 |
uint32_t total_size = TAG_SIZE + |
| 157 |
CHUNK_HEADER_SIZE + |
| 158 |
VP8X_CHUNK_SIZE; |
| 159 |
assert(data != NULL); |
| 160 |
assert(data_size != NULL); |
| 161 |
buf = *data; |
| 162 |
buf_size = *data_size; |
| 163 |
|
| 164 |
assert(alpha_data != NULL); |
| 165 |
assert(alpha_size != NULL); |
| 166 |
*alpha_data = NULL; |
| 167 |
*alpha_size = 0; |
| 168 |
|
| 169 |
while (1) { |
| 170 |
uint32_t chunk_size; |
| 171 |
uint32_t disk_chunk_size; |
| 172 |
|
| 173 |
*data = buf; |
| 174 |
*data_size = buf_size; |
| 175 |
|
| 176 |
if (buf_size < CHUNK_HEADER_SIZE) { |
| 177 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 178 |
} |
| 179 |
|
| 180 |
chunk_size = GetLE32(buf + TAG_SIZE); |
| 181 |
if (chunk_size > MAX_CHUNK_PAYLOAD) { |
| 182 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 183 |
} |
| 184 |
|
| 185 |
disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1u; |
| 186 |
total_size += disk_chunk_size; |
| 187 |
|
| 188 |
|
| 189 |
if (riff_size > 0 && (total_size > riff_size)) { |
| 190 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if (!memcmp(buf, "VP8 ", TAG_SIZE) || |
| 198 |
!memcmp(buf, "VP8L", TAG_SIZE)) { |
| 199 |
return VP8_STATUS_OK; |
| 200 |
} |
| 201 |
|
| 202 |
if (buf_size < disk_chunk_size) { |
| 203 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 204 |
} |
| 205 |
|
| 206 |
if (!memcmp(buf, "ALPH", TAG_SIZE)) { |
| 207 |
*alpha_data = buf + CHUNK_HEADER_SIZE; |
| 208 |
*alpha_size = chunk_size; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
buf += disk_chunk_size; |
| 213 |
buf_size -= disk_chunk_size; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr, |
| 226 |
size_t* const data_size, int have_all_data, |
| 227 |
size_t riff_size, size_t* const chunk_size, |
| 228 |
int* const is_lossless) { |
| 229 |
const uint8_t* const data = *data_ptr; |
| 230 |
const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE); |
| 231 |
const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE); |
| 232 |
const uint32_t minimal_size = |
| 233 |
TAG_SIZE + CHUNK_HEADER_SIZE; |
| 234 |
|
| 235 |
assert(data != NULL); |
| 236 |
assert(data_size != NULL); |
| 237 |
assert(chunk_size != NULL); |
| 238 |
assert(is_lossless != NULL); |
| 239 |
|
| 240 |
if (*data_size < CHUNK_HEADER_SIZE) { |
| 241 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 242 |
} |
| 243 |
|
| 244 |
if (is_vp8 || is_vp8l) { |
| 245 |
|
| 246 |
const uint32_t size = GetLE32(data + TAG_SIZE); |
| 247 |
if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) { |
| 248 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 249 |
} |
| 250 |
if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) { |
| 251 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 252 |
} |
| 253 |
|
| 254 |
*chunk_size = size; |
| 255 |
*data_ptr += CHUNK_HEADER_SIZE; |
| 256 |
*data_size -= CHUNK_HEADER_SIZE; |
| 257 |
*is_lossless = is_vp8l; |
| 258 |
} else { |
| 259 |
|
| 260 |
*is_lossless = VP8LCheckSignature(data, *data_size); |
| 261 |
*chunk_size = *data_size; |
| 262 |
} |
| 263 |
|
| 264 |
return VP8_STATUS_OK; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static VP8StatusCode ParseHeadersInternal(const uint8_t* data, |
| 281 |
size_t data_size, |
| 282 |
int* const width, |
| 283 |
int* const height, |
| 284 |
int* const has_alpha, |
| 285 |
int* const has_animation, |
| 286 |
int* const format, |
| 287 |
WebPHeaderStructure* const headers) { |
| 288 |
int canvas_width = 0; |
| 289 |
int canvas_height = 0; |
| 290 |
int image_width = 0; |
| 291 |
int image_height = 0; |
| 292 |
int found_riff = 0; |
| 293 |
int found_vp8x = 0; |
| 294 |
int animation_present = 0; |
| 295 |
const int have_all_data = (headers != NULL) ? headers->have_all_data : 0; |
| 296 |
|
| 297 |
VP8StatusCode status; |
| 298 |
WebPHeaderStructure hdrs; |
| 299 |
|
| 300 |
if (data == NULL || data_size < RIFF_HEADER_SIZE) { |
| 301 |
return VP8_STATUS_NOT_ENOUGH_DATA; |
| 302 |
} |
| 303 |
memset(&hdrs, 0, sizeof(hdrs)); |
| 304 |
hdrs.data = data; |
| 305 |
hdrs.data_size = data_size; |
| 306 |
|
| 307 |
|
| 308 |
status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size); |
| 309 |
if (status != VP8_STATUS_OK) { |
| 310 |
return status; |
| 311 |
} |
| 312 |
found_riff = (hdrs.riff_size > 0); |
| 313 |
|
| 314 |
|
| 315 |
{ |
| 316 |
uint32_t flags = 0; |
| 317 |
status = ParseVP8X(&data, &data_size, &found_vp8x, |
| 318 |
&canvas_width, &canvas_height, &flags); |
| 319 |
if (status != VP8_STATUS_OK) { |
| 320 |
return status; |
| 321 |
} |
| 322 |
animation_present = !!(flags & ANIMATION_FLAG); |
| 323 |
if (!found_riff && found_vp8x) { |
| 324 |
|
| 325 |
|
| 326 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 327 |
} |
| 328 |
if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG); |
| 329 |
if (has_animation != NULL) *has_animation = animation_present; |
| 330 |
if (format != NULL) *format = 0; |
| 331 |
|
| 332 |
image_width = canvas_width; |
| 333 |
image_height = canvas_height; |
| 334 |
if (found_vp8x && animation_present && headers == NULL) { |
| 335 |
status = VP8_STATUS_OK; |
| 336 |
goto ReturnWidthHeight; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (data_size < TAG_SIZE) { |
| 341 |
status = VP8_STATUS_NOT_ENOUGH_DATA; |
| 342 |
goto ReturnWidthHeight; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
if ((found_riff && found_vp8x) || |
| 347 |
(!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) { |
| 348 |
status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size, |
| 349 |
&hdrs.alpha_data, &hdrs.alpha_data_size); |
| 350 |
if (status != VP8_STATUS_OK) { |
| 351 |
goto ReturnWidthHeight; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size, |
| 357 |
&hdrs.compressed_size, &hdrs.is_lossless); |
| 358 |
if (status != VP8_STATUS_OK) { |
| 359 |
goto ReturnWidthHeight; |
| 360 |
} |
| 361 |
if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) { |
| 362 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 363 |
} |
| 364 |
|
| 365 |
if (format != NULL && !animation_present) { |
| 366 |
*format = hdrs.is_lossless ? 2 : 1; |
| 367 |
} |
| 368 |
|
| 369 |
if (!hdrs.is_lossless) { |
| 370 |
if (data_size < VP8_FRAME_HEADER_SIZE) { |
| 371 |
status = VP8_STATUS_NOT_ENOUGH_DATA; |
| 372 |
goto ReturnWidthHeight; |
| 373 |
} |
| 374 |
|
| 375 |
if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size, |
| 376 |
&image_width, &image_height)) { |
| 377 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 378 |
} |
| 379 |
} else { |
| 380 |
if (data_size < VP8L_FRAME_HEADER_SIZE) { |
| 381 |
status = VP8_STATUS_NOT_ENOUGH_DATA; |
| 382 |
goto ReturnWidthHeight; |
| 383 |
} |
| 384 |
|
| 385 |
if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) { |
| 386 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
if (found_vp8x) { |
| 391 |
if (canvas_width != image_width || canvas_height != image_height) { |
| 392 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 393 |
} |
| 394 |
} |
| 395 |
if (headers != NULL) { |
| 396 |
*headers = hdrs; |
| 397 |
headers->offset = data - headers->data; |
| 398 |
assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD); |
| 399 |
assert(headers->offset == headers->data_size - data_size); |
| 400 |
} |
| 401 |
ReturnWidthHeight: |
| 402 |
if (status == VP8_STATUS_OK || |
| 403 |
(status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) { |
| 404 |
if (has_alpha != NULL) { |
| 405 |
|
| 406 |
|
| 407 |
*has_alpha |= (hdrs.alpha_data != NULL); |
| 408 |
} |
| 409 |
if (width != NULL) *width = image_width; |
| 410 |
if (height != NULL) *height = image_height; |
| 411 |
return VP8_STATUS_OK; |
| 412 |
} else { |
| 413 |
return status; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) { |
| 418 |
|
| 419 |
volatile VP8StatusCode status; |
| 420 |
int has_animation = 0; |
| 421 |
assert(headers != NULL); |
| 422 |
|
| 423 |
status = ParseHeadersInternal(headers->data, headers->data_size, |
| 424 |
NULL, NULL, NULL, &has_animation, |
| 425 |
NULL, headers); |
| 426 |
if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) { |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
if (has_animation) { |
| 431 |
status = VP8_STATUS_UNSUPPORTED_FEATURE; |
| 432 |
} |
| 433 |
} |
| 434 |
return status; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
void WebPResetDecParams(WebPDecParams* const params) { |
| 441 |
if (params != NULL) { |
| 442 |
memset(params, 0, sizeof(*params)); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
WEBP_NODISCARD static VP8StatusCode DecodeInto(const uint8_t* const data, |
| 451 |
size_t data_size, |
| 452 |
WebPDecParams* const params) { |
| 453 |
VP8StatusCode status; |
| 454 |
VP8Io io; |
| 455 |
WebPHeaderStructure headers; |
| 456 |
|
| 457 |
headers.data = data; |
| 458 |
headers.data_size = data_size; |
| 459 |
headers.have_all_data = 1; |
| 460 |
status = WebPParseHeaders(&headers); |
| 461 |
if (status != VP8_STATUS_OK) { |
| 462 |
return status; |
| 463 |
} |
| 464 |
|
| 465 |
assert(params != NULL); |
| 466 |
if (!VP8InitIo(&io)) { |
| 467 |
return VP8_STATUS_INVALID_PARAM; |
| 468 |
} |
| 469 |
io.data = headers.data + headers.offset; |
| 470 |
io.data_size = headers.data_size - headers.offset; |
| 471 |
WebPInitCustomIo(params, &io); |
| 472 |
|
| 473 |
if (!headers.is_lossless) { |
| 474 |
VP8Decoder* const dec = VP8New(); |
| 475 |
if (dec == NULL) { |
| 476 |
return VP8_STATUS_OUT_OF_MEMORY; |
| 477 |
} |
| 478 |
dec->alpha_data_ = headers.alpha_data; |
| 479 |
dec->alpha_data_size_ = headers.alpha_data_size; |
| 480 |
|
| 481 |
|
| 482 |
if (!VP8GetHeaders(dec, &io)) { |
| 483 |
status = dec->status_; |
| 484 |
} else { |
| 485 |
|
| 486 |
status = WebPAllocateDecBuffer(io.width, io.height, params->options, |
| 487 |
params->output); |
| 488 |
if (status == VP8_STATUS_OK) { |
| 489 |
|
| 490 |
dec->mt_method_ = VP8GetThreadMethod(params->options, &headers, |
| 491 |
io.width, io.height); |
| 492 |
VP8InitDithering(params->options, dec); |
| 493 |
if (!VP8Decode(dec, &io)) { |
| 494 |
status = dec->status_; |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
VP8Delete(dec); |
| 499 |
} else { |
| 500 |
VP8LDecoder* const dec = VP8LNew(); |
| 501 |
if (dec == NULL) { |
| 502 |
return VP8_STATUS_OUT_OF_MEMORY; |
| 503 |
} |
| 504 |
if (!VP8LDecodeHeader(dec, &io)) { |
| 505 |
status = dec->status_; |
| 506 |
} else { |
| 507 |
|
| 508 |
status = WebPAllocateDecBuffer(io.width, io.height, params->options, |
| 509 |
params->output); |
| 510 |
if (status == VP8_STATUS_OK) { |
| 511 |
if (!VP8LDecodeImage(dec)) { |
| 512 |
status = dec->status_; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
VP8LDelete(dec); |
| 517 |
} |
| 518 |
|
| 519 |
if (status != VP8_STATUS_OK) { |
| 520 |
WebPFreeDecBuffer(params->output); |
| 521 |
} else { |
| 522 |
if (params->options != NULL && params->options->flip) { |
| 523 |
|
| 524 |
|
| 525 |
status = WebPFlipBuffer(params->output); |
| 526 |
} |
| 527 |
} |
| 528 |
return status; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
WEBP_NODISCARD static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace, |
| 533 |
const uint8_t* const data, |
| 534 |
size_t data_size, |
| 535 |
uint8_t* const rgba, |
| 536 |
int stride, size_t size) { |
| 537 |
WebPDecParams params; |
| 538 |
WebPDecBuffer buf; |
| 539 |
if (rgba == NULL || !WebPInitDecBuffer(&buf)) { |
| 540 |
return NULL; |
| 541 |
} |
| 542 |
WebPResetDecParams(¶ms); |
| 543 |
params.output = &buf; |
| 544 |
buf.colorspace = colorspace; |
| 545 |
buf.u.RGBA.rgba = rgba; |
| 546 |
buf.u.RGBA.stride = stride; |
| 547 |
buf.u.RGBA.size = size; |
| 548 |
buf.is_external_memory = 1; |
| 549 |
if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { |
| 550 |
return NULL; |
| 551 |
} |
| 552 |
return rgba; |
| 553 |
} |
| 554 |
|
| 555 |
uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size, |
| 556 |
uint8_t* output, size_t size, int stride) { |
| 557 |
return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size); |
| 558 |
} |
| 559 |
|
| 560 |
uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size, |
| 561 |
uint8_t* output, size_t size, int stride) { |
| 562 |
return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size); |
| 563 |
} |
| 564 |
|
| 565 |
uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size, |
| 566 |
uint8_t* output, size_t size, int stride) { |
| 567 |
return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size); |
| 568 |
} |
| 569 |
|
| 570 |
uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size, |
| 571 |
uint8_t* output, size_t size, int stride) { |
| 572 |
return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size); |
| 573 |
} |
| 574 |
|
| 575 |
uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size, |
| 576 |
uint8_t* output, size_t size, int stride) { |
| 577 |
return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size); |
| 578 |
} |
| 579 |
|
| 580 |
uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size, |
| 581 |
uint8_t* luma, size_t luma_size, int luma_stride, |
| 582 |
uint8_t* u, size_t u_size, int u_stride, |
| 583 |
uint8_t* v, size_t v_size, int v_stride) { |
| 584 |
WebPDecParams params; |
| 585 |
WebPDecBuffer output; |
| 586 |
if (luma == NULL || !WebPInitDecBuffer(&output)) return NULL; |
| 587 |
WebPResetDecParams(¶ms); |
| 588 |
params.output = &output; |
| 589 |
output.colorspace = MODE_YUV; |
| 590 |
output.u.YUVA.y = luma; |
| 591 |
output.u.YUVA.y_stride = luma_stride; |
| 592 |
output.u.YUVA.y_size = luma_size; |
| 593 |
output.u.YUVA.u = u; |
| 594 |
output.u.YUVA.u_stride = u_stride; |
| 595 |
output.u.YUVA.u_size = u_size; |
| 596 |
output.u.YUVA.v = v; |
| 597 |
output.u.YUVA.v_stride = v_stride; |
| 598 |
output.u.YUVA.v_size = v_size; |
| 599 |
output.is_external_memory = 1; |
| 600 |
if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { |
| 601 |
return NULL; |
| 602 |
} |
| 603 |
return luma; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
WEBP_NODISCARD static uint8_t* Decode(WEBP_CSP_MODE mode, |
| 609 |
const uint8_t* const data, |
| 610 |
size_t data_size, int* const width, |
| 611 |
int* const height, |
| 612 |
WebPDecBuffer* const keep_info) { |
| 613 |
WebPDecParams params; |
| 614 |
WebPDecBuffer output; |
| 615 |
|
| 616 |
if (!WebPInitDecBuffer(&output)) { |
| 617 |
return NULL; |
| 618 |
} |
| 619 |
WebPResetDecParams(¶ms); |
| 620 |
params.output = &output; |
| 621 |
output.colorspace = mode; |
| 622 |
|
| 623 |
|
| 624 |
if (!WebPGetInfo(data, data_size, &output.width, &output.height)) { |
| 625 |
return NULL; |
| 626 |
} |
| 627 |
if (width != NULL) *width = output.width; |
| 628 |
if (height != NULL) *height = output.height; |
| 629 |
|
| 630 |
|
| 631 |
if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { |
| 632 |
return NULL; |
| 633 |
} |
| 634 |
if (keep_info != NULL) { |
| 635 |
WebPCopyDecBuffer(&output, keep_info); |
| 636 |
} |
| 637 |
|
| 638 |
return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y; |
| 639 |
} |
| 640 |
|
| 641 |
uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, |
| 642 |
int* width, int* height) { |
| 643 |
return Decode(MODE_RGB, data, data_size, width, height, NULL); |
| 644 |
} |
| 645 |
|
| 646 |
uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size, |
| 647 |
int* width, int* height) { |
| 648 |
return Decode(MODE_RGBA, data, data_size, width, height, NULL); |
| 649 |
} |
| 650 |
|
| 651 |
uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size, |
| 652 |
int* width, int* height) { |
| 653 |
return Decode(MODE_ARGB, data, data_size, width, height, NULL); |
| 654 |
} |
| 655 |
|
| 656 |
uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size, |
| 657 |
int* width, int* height) { |
| 658 |
return Decode(MODE_BGR, data, data_size, width, height, NULL); |
| 659 |
} |
| 660 |
|
| 661 |
uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size, |
| 662 |
int* width, int* height) { |
| 663 |
return Decode(MODE_BGRA, data, data_size, width, height, NULL); |
| 664 |
} |
| 665 |
|
| 666 |
uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size, |
| 667 |
int* width, int* height, uint8_t** u, uint8_t** v, |
| 668 |
int* stride, int* uv_stride) { |
| 669 |
|
| 670 |
if (u == NULL || v == NULL || stride == NULL || uv_stride == NULL) { |
| 671 |
return NULL; |
| 672 |
} |
| 673 |
|
| 674 |
{ |
| 675 |
WebPDecBuffer output; |
| 676 |
uint8_t* const out = Decode(MODE_YUV, data, data_size, |
| 677 |
width, height, &output); |
| 678 |
|
| 679 |
if (out != NULL) { |
| 680 |
const WebPYUVABuffer* const buf = &output.u.YUVA; |
| 681 |
*u = buf->u; |
| 682 |
*v = buf->v; |
| 683 |
*stride = buf->y_stride; |
| 684 |
*uv_stride = buf->u_stride; |
| 685 |
assert(buf->u_stride == buf->v_stride); |
| 686 |
} |
| 687 |
return out; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
static void DefaultFeatures(WebPBitstreamFeatures* const features) { |
| 692 |
assert(features != NULL); |
| 693 |
memset(features, 0, sizeof(*features)); |
| 694 |
} |
| 695 |
|
| 696 |
static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size, |
| 697 |
WebPBitstreamFeatures* const features) { |
| 698 |
if (features == NULL || data == NULL) { |
| 699 |
return VP8_STATUS_INVALID_PARAM; |
| 700 |
} |
| 701 |
DefaultFeatures(features); |
| 702 |
|
| 703 |
|
| 704 |
return ParseHeadersInternal(data, data_size, |
| 705 |
&features->width, &features->height, |
| 706 |
&features->has_alpha, &features->has_animation, |
| 707 |
&features->format, NULL); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
int WebPGetInfo(const uint8_t* data, size_t data_size, |
| 714 |
int* width, int* height) { |
| 715 |
WebPBitstreamFeatures features; |
| 716 |
|
| 717 |
if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) { |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
if (width != NULL) { |
| 722 |
*width = features.width; |
| 723 |
} |
| 724 |
if (height != NULL) { |
| 725 |
*height = features.height; |
| 726 |
} |
| 727 |
|
| 728 |
return 1; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
int WebPInitDecoderConfigInternal(WebPDecoderConfig* config, |
| 735 |
int version) { |
| 736 |
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { |
| 737 |
return 0; |
| 738 |
} |
| 739 |
if (config == NULL) { |
| 740 |
return 0; |
| 741 |
} |
| 742 |
memset(config, 0, sizeof(*config)); |
| 743 |
DefaultFeatures(&config->input); |
| 744 |
if (!WebPInitDecBuffer(&config->output)) { |
| 745 |
return 0; |
| 746 |
} |
| 747 |
return 1; |
| 748 |
} |
| 749 |
|
| 750 |
VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size, |
| 751 |
WebPBitstreamFeatures* features, |
| 752 |
int version) { |
| 753 |
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { |
| 754 |
return VP8_STATUS_INVALID_PARAM; |
| 755 |
} |
| 756 |
if (features == NULL) { |
| 757 |
return VP8_STATUS_INVALID_PARAM; |
| 758 |
} |
| 759 |
return GetFeatures(data, data_size, features); |
| 760 |
} |
| 761 |
|
| 762 |
VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size, |
| 763 |
WebPDecoderConfig* config) { |
| 764 |
WebPDecParams params; |
| 765 |
VP8StatusCode status; |
| 766 |
|
| 767 |
if (config == NULL) { |
| 768 |
return VP8_STATUS_INVALID_PARAM; |
| 769 |
} |
| 770 |
|
| 771 |
status = GetFeatures(data, data_size, &config->input); |
| 772 |
if (status != VP8_STATUS_OK) { |
| 773 |
if (status == VP8_STATUS_NOT_ENOUGH_DATA) { |
| 774 |
return VP8_STATUS_BITSTREAM_ERROR; |
| 775 |
} |
| 776 |
return status; |
| 777 |
} |
| 778 |
|
| 779 |
WebPResetDecParams(¶ms); |
| 780 |
params.options = &config->options; |
| 781 |
params.output = &config->output; |
| 782 |
if (WebPAvoidSlowMemory(params.output, &config->input)) { |
| 783 |
|
| 784 |
WebPDecBuffer in_mem_buffer; |
| 785 |
if (!WebPInitDecBuffer(&in_mem_buffer)) { |
| 786 |
return VP8_STATUS_INVALID_PARAM; |
| 787 |
} |
| 788 |
in_mem_buffer.colorspace = config->output.colorspace; |
| 789 |
in_mem_buffer.width = config->input.width; |
| 790 |
in_mem_buffer.height = config->input.height; |
| 791 |
params.output = &in_mem_buffer; |
| 792 |
status = DecodeInto(data, data_size, ¶ms); |
| 793 |
if (status == VP8_STATUS_OK) { |
| 794 |
status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output); |
| 795 |
} |
| 796 |
WebPFreeDecBuffer(&in_mem_buffer); |
| 797 |
} else { |
| 798 |
status = DecodeInto(data, data_size, ¶ms); |
| 799 |
} |
| 800 |
|
| 801 |
return status; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
int WebPCheckCropDimensions(int image_width, int image_height, |
| 808 |
int x, int y, int w, int h) { |
| 809 |
return !(x < 0 || y < 0 || w <= 0 || h <= 0 || |
| 810 |
x >= image_width || w > image_width || w > image_width - x || |
| 811 |
y >= image_height || h > image_height || h > image_height - y); |
| 812 |
} |
| 813 |
|
| 814 |
int WebPIoInitFromOptions(const WebPDecoderOptions* const options, |
| 815 |
VP8Io* const io, WEBP_CSP_MODE src_colorspace) { |
| 816 |
const int W = io->width; |
| 817 |
const int H = io->height; |
| 818 |
int x = 0, y = 0, w = W, h = H; |
| 819 |
|
| 820 |
|
| 821 |
io->use_cropping = (options != NULL) && options->use_cropping; |
| 822 |
if (io->use_cropping) { |
| 823 |
w = options->crop_width; |
| 824 |
h = options->crop_height; |
| 825 |
x = options->crop_left; |
| 826 |
y = options->crop_top; |
| 827 |
if (!WebPIsRGBMode(src_colorspace)) { |
| 828 |
x &= ~1; |
| 829 |
y &= ~1; |
| 830 |
} |
| 831 |
if (!WebPCheckCropDimensions(W, H, x, y, w, h)) { |
| 832 |
return 0; |
| 833 |
} |
| 834 |
} |
| 835 |
io->crop_left = x; |
| 836 |
io->crop_top = y; |
| 837 |
io->crop_right = x + w; |
| 838 |
io->crop_bottom = y + h; |
| 839 |
io->mb_w = w; |
| 840 |
io->mb_h = h; |
| 841 |
|
| 842 |
|
| 843 |
io->use_scaling = (options != NULL) && options->use_scaling; |
| 844 |
if (io->use_scaling) { |
| 845 |
int scaled_width = options->scaled_width; |
| 846 |
int scaled_height = options->scaled_height; |
| 847 |
if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) { |
| 848 |
return 0; |
| 849 |
} |
| 850 |
io->scaled_width = scaled_width; |
| 851 |
io->scaled_height = scaled_height; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
io->bypass_filtering = (options != NULL) && options->bypass_filtering; |
| 856 |
|
| 857 |
|
| 858 |
#ifdef FANCY_UPSAMPLING |
| 859 |
io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling); |
| 860 |
#endif |
| 861 |
|
| 862 |
if (io->use_scaling) { |
| 863 |
|
| 864 |
io->bypass_filtering |= (io->scaled_width < W * 3 / 4) && |
| 865 |
(io->scaled_height < H * 3 / 4); |
| 866 |
io->fancy_upsampling = 0; |
| 867 |
} |
| 868 |
return 1; |
| 869 |
} |
| 870 |
|
| 871 |
|