| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <assert.h> |
| 16 |
#include "src/mux/muxi.h" |
| 17 |
#include "src/utils/utils.h" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#define SWITCH_ID_LIST(INDEX, LIST) \ |
| 24 |
do { \ |
| 25 |
if (idx == (INDEX)) { \ |
| 26 |
const WebPChunk* const chunk = ChunkSearchList((LIST), nth, \ |
| 27 |
kChunks[(INDEX)].tag); \ |
| 28 |
if (chunk) { \ |
| 29 |
*data = chunk->data_; \ |
| 30 |
return WEBP_MUX_OK; \ |
| 31 |
} else { \ |
| 32 |
return WEBP_MUX_NOT_FOUND; \ |
| 33 |
} \ |
| 34 |
} \ |
| 35 |
} while (0) |
| 36 |
|
| 37 |
static WebPMuxError MuxGet(const WebPMux* const mux, CHUNK_INDEX idx, |
| 38 |
uint32_t nth, WebPData* const data) { |
| 39 |
assert(mux != NULL); |
| 40 |
assert(idx != IDX_LAST_CHUNK); |
| 41 |
assert(!IsWPI(kChunks[idx].id)); |
| 42 |
WebPDataInit(data); |
| 43 |
|
| 44 |
SWITCH_ID_LIST(IDX_VP8X, mux->vp8x_); |
| 45 |
SWITCH_ID_LIST(IDX_ICCP, mux->iccp_); |
| 46 |
SWITCH_ID_LIST(IDX_ANIM, mux->anim_); |
| 47 |
SWITCH_ID_LIST(IDX_EXIF, mux->exif_); |
| 48 |
SWITCH_ID_LIST(IDX_XMP, mux->xmp_); |
| 49 |
assert(idx != IDX_UNKNOWN); |
| 50 |
return WEBP_MUX_NOT_FOUND; |
| 51 |
} |
| 52 |
#undef SWITCH_ID_LIST |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static WebPMuxError ChunkVerifyAndAssign(WebPChunk* chunk, |
| 57 |
const uint8_t* data, size_t data_size, |
| 58 |
size_t riff_size, int copy_data) { |
| 59 |
uint32_t chunk_size; |
| 60 |
WebPData chunk_data; |
| 61 |
|
| 62 |
|
| 63 |
if (data_size < CHUNK_HEADER_SIZE) return WEBP_MUX_NOT_ENOUGH_DATA; |
| 64 |
chunk_size = GetLE32(data + TAG_SIZE); |
| 65 |
if (chunk_size > MAX_CHUNK_PAYLOAD) return WEBP_MUX_BAD_DATA; |
| 66 |
|
| 67 |
{ |
| 68 |
const size_t chunk_disk_size = SizeWithPadding(chunk_size); |
| 69 |
if (chunk_disk_size > riff_size) return WEBP_MUX_BAD_DATA; |
| 70 |
if (chunk_disk_size > data_size) return WEBP_MUX_NOT_ENOUGH_DATA; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
chunk_data.bytes = data + CHUNK_HEADER_SIZE; |
| 75 |
chunk_data.size = chunk_size; |
| 76 |
return ChunkAssignData(chunk, &chunk_data, copy_data, GetLE32(data + 0)); |
| 77 |
} |
| 78 |
|
| 79 |
int MuxImageFinalize(WebPMuxImage* const wpi) { |
| 80 |
const WebPChunk* const img = wpi->img_; |
| 81 |
const WebPData* const image = &img->data_; |
| 82 |
const int is_lossless = (img->tag_ == kChunks[IDX_VP8L].tag); |
| 83 |
int w, h; |
| 84 |
int vp8l_has_alpha = 0; |
| 85 |
const int ok = is_lossless ? |
| 86 |
VP8LGetInfo(image->bytes, image->size, &w, &h, &vp8l_has_alpha) : |
| 87 |
VP8GetInfo(image->bytes, image->size, image->size, &w, &h); |
| 88 |
assert(img != NULL); |
| 89 |
if (ok) { |
| 90 |
|
| 91 |
if (is_lossless && (wpi->alpha_ != NULL)) { |
| 92 |
ChunkDelete(wpi->alpha_); |
| 93 |
wpi->alpha_ = NULL; |
| 94 |
} |
| 95 |
wpi->width_ = w; |
| 96 |
wpi->height_ = h; |
| 97 |
wpi->has_alpha_ = vp8l_has_alpha || (wpi->alpha_ != NULL); |
| 98 |
} |
| 99 |
return ok; |
| 100 |
} |
| 101 |
|
| 102 |
static int MuxImageParse(const WebPChunk* const chunk, int copy_data, |
| 103 |
WebPMuxImage* const wpi) { |
| 104 |
const uint8_t* bytes = chunk->data_.bytes; |
| 105 |
size_t size = chunk->data_.size; |
| 106 |
const uint8_t* const last = (bytes == NULL) ? NULL : bytes + size; |
| 107 |
WebPChunk subchunk; |
| 108 |
size_t subchunk_size; |
| 109 |
WebPChunk** unknown_chunk_list = &wpi->unknown_; |
| 110 |
ChunkInit(&subchunk); |
| 111 |
|
| 112 |
assert(chunk->tag_ == kChunks[IDX_ANMF].tag); |
| 113 |
assert(!wpi->is_partial_); |
| 114 |
|
| 115 |
|
| 116 |
{ |
| 117 |
const size_t hdr_size = ANMF_CHUNK_SIZE; |
| 118 |
const WebPData temp = { bytes, hdr_size }; |
| 119 |
|
| 120 |
|
| 121 |
if (size < hdr_size) goto Fail; |
| 122 |
if (ChunkAssignData(&subchunk, &temp, copy_data, |
| 123 |
chunk->tag_) != WEBP_MUX_OK) { |
| 124 |
goto Fail; |
| 125 |
} |
| 126 |
} |
| 127 |
if (ChunkSetHead(&subchunk, &wpi->header_) != WEBP_MUX_OK) goto Fail; |
| 128 |
wpi->is_partial_ = 1; |
| 129 |
|
| 130 |
|
| 131 |
subchunk_size = ChunkDiskSize(&subchunk) - CHUNK_HEADER_SIZE; |
| 132 |
bytes += subchunk_size; |
| 133 |
size -= subchunk_size; |
| 134 |
|
| 135 |
while (bytes != last) { |
| 136 |
ChunkInit(&subchunk); |
| 137 |
if (ChunkVerifyAndAssign(&subchunk, bytes, size, size, |
| 138 |
copy_data) != WEBP_MUX_OK) { |
| 139 |
goto Fail; |
| 140 |
} |
| 141 |
switch (ChunkGetIdFromTag(subchunk.tag_)) { |
| 142 |
case WEBP_CHUNK_ALPHA: |
| 143 |
if (wpi->alpha_ != NULL) goto Fail; |
| 144 |
if (ChunkSetHead(&subchunk, &wpi->alpha_) != WEBP_MUX_OK) goto Fail; |
| 145 |
wpi->is_partial_ = 1; |
| 146 |
break; |
| 147 |
case WEBP_CHUNK_IMAGE: |
| 148 |
if (wpi->img_ != NULL) goto Fail; |
| 149 |
if (ChunkSetHead(&subchunk, &wpi->img_) != WEBP_MUX_OK) goto Fail; |
| 150 |
if (!MuxImageFinalize(wpi)) goto Fail; |
| 151 |
wpi->is_partial_ = 0; |
| 152 |
break; |
| 153 |
case WEBP_CHUNK_UNKNOWN: |
| 154 |
if (wpi->is_partial_) { |
| 155 |
goto Fail; |
| 156 |
|
| 157 |
} |
| 158 |
if (ChunkAppend(&subchunk, &unknown_chunk_list) != WEBP_MUX_OK) { |
| 159 |
goto Fail; |
| 160 |
} |
| 161 |
break; |
| 162 |
default: |
| 163 |
goto Fail; |
| 164 |
} |
| 165 |
subchunk_size = ChunkDiskSize(&subchunk); |
| 166 |
bytes += subchunk_size; |
| 167 |
size -= subchunk_size; |
| 168 |
} |
| 169 |
if (wpi->is_partial_) goto Fail; |
| 170 |
return 1; |
| 171 |
|
| 172 |
Fail: |
| 173 |
ChunkRelease(&subchunk); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
WebPMux* WebPMuxCreateInternal(const WebPData* bitstream, int copy_data, |
| 181 |
int version) { |
| 182 |
size_t riff_size; |
| 183 |
uint32_t tag; |
| 184 |
const uint8_t* end; |
| 185 |
WebPMux* mux = NULL; |
| 186 |
WebPMuxImage* wpi = NULL; |
| 187 |
const uint8_t* data; |
| 188 |
size_t size; |
| 189 |
WebPChunk chunk; |
| 190 |
|
| 191 |
|
| 192 |
WebPChunk** chunk_list_ends[WEBP_CHUNK_NIL + 1] = { NULL }; |
| 193 |
ChunkInit(&chunk); |
| 194 |
|
| 195 |
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_MUX_ABI_VERSION)) { |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
if (bitstream == NULL) return NULL; |
| 199 |
|
| 200 |
data = bitstream->bytes; |
| 201 |
size = bitstream->size; |
| 202 |
|
| 203 |
if (data == NULL) return NULL; |
| 204 |
if (size < RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE) return NULL; |
| 205 |
if (GetLE32(data + 0) != MKFOURCC('R', 'I', 'F', 'F') || |
| 206 |
GetLE32(data + CHUNK_HEADER_SIZE) != MKFOURCC('W', 'E', 'B', 'P')) { |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
mux = WebPMuxNew(); |
| 211 |
if (mux == NULL) return NULL; |
| 212 |
|
| 213 |
tag = GetLE32(data + RIFF_HEADER_SIZE); |
| 214 |
if (tag != kChunks[IDX_VP8].tag && |
| 215 |
tag != kChunks[IDX_VP8L].tag && |
| 216 |
tag != kChunks[IDX_VP8X].tag) { |
| 217 |
goto Err; |
| 218 |
} |
| 219 |
|
| 220 |
riff_size = GetLE32(data + TAG_SIZE); |
| 221 |
if (riff_size > MAX_CHUNK_PAYLOAD) goto Err; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
riff_size = SizeWithPadding(riff_size); |
| 226 |
if (riff_size < CHUNK_HEADER_SIZE) goto Err; |
| 227 |
if (riff_size > size) goto Err; |
| 228 |
|
| 229 |
if (size > riff_size + CHUNK_HEADER_SIZE) { |
| 230 |
size = riff_size + CHUNK_HEADER_SIZE; |
| 231 |
} |
| 232 |
|
| 233 |
end = data + size; |
| 234 |
data += RIFF_HEADER_SIZE; |
| 235 |
size -= RIFF_HEADER_SIZE; |
| 236 |
|
| 237 |
wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*wpi)); |
| 238 |
if (wpi == NULL) goto Err; |
| 239 |
MuxImageInit(wpi); |
| 240 |
|
| 241 |
|
| 242 |
while (data != end) { |
| 243 |
size_t data_size; |
| 244 |
WebPChunkId id; |
| 245 |
if (ChunkVerifyAndAssign(&chunk, data, size, riff_size, |
| 246 |
copy_data) != WEBP_MUX_OK) { |
| 247 |
goto Err; |
| 248 |
} |
| 249 |
data_size = ChunkDiskSize(&chunk); |
| 250 |
id = ChunkGetIdFromTag(chunk.tag_); |
| 251 |
switch (id) { |
| 252 |
case WEBP_CHUNK_ALPHA: |
| 253 |
if (wpi->alpha_ != NULL) goto Err; |
| 254 |
if (ChunkSetHead(&chunk, &wpi->alpha_) != WEBP_MUX_OK) goto Err; |
| 255 |
wpi->is_partial_ = 1; |
| 256 |
break; |
| 257 |
case WEBP_CHUNK_IMAGE: |
| 258 |
if (ChunkSetHead(&chunk, &wpi->img_) != WEBP_MUX_OK) goto Err; |
| 259 |
if (!MuxImageFinalize(wpi)) goto Err; |
| 260 |
wpi->is_partial_ = 0; |
| 261 |
PushImage: |
| 262 |
|
| 263 |
if (MuxImagePush(wpi, &mux->images_) != WEBP_MUX_OK) goto Err; |
| 264 |
MuxImageInit(wpi); |
| 265 |
break; |
| 266 |
case WEBP_CHUNK_ANMF: |
| 267 |
if (wpi->is_partial_) goto Err; |
| 268 |
if (!MuxImageParse(&chunk, copy_data, wpi)) goto Err; |
| 269 |
ChunkRelease(&chunk); |
| 270 |
goto PushImage; |
| 271 |
default: |
| 272 |
if (wpi->is_partial_) goto Err; |
| 273 |
|
| 274 |
if (chunk_list_ends[id] == NULL) { |
| 275 |
chunk_list_ends[id] = |
| 276 |
MuxGetChunkListFromId(mux, id); |
| 277 |
} |
| 278 |
if (ChunkAppend(&chunk, &chunk_list_ends[id]) != WEBP_MUX_OK) goto Err; |
| 279 |
if (id == WEBP_CHUNK_VP8X) { |
| 280 |
if (data_size < CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE) goto Err; |
| 281 |
mux->canvas_width_ = GetLE24(data + 12) + 1; |
| 282 |
mux->canvas_height_ = GetLE24(data + 15) + 1; |
| 283 |
} |
| 284 |
break; |
| 285 |
} |
| 286 |
data += data_size; |
| 287 |
size -= data_size; |
| 288 |
ChunkInit(&chunk); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
if (wpi->is_partial_) goto Err; |
| 293 |
|
| 294 |
|
| 295 |
if (MuxValidate(mux) != WEBP_MUX_OK) goto Err; |
| 296 |
|
| 297 |
MuxImageDelete(wpi); |
| 298 |
return mux; |
| 299 |
|
| 300 |
Err: |
| 301 |
ChunkRelease(&chunk); |
| 302 |
MuxImageDelete(wpi); |
| 303 |
WebPMuxDelete(mux); |
| 304 |
return NULL; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
static WebPMuxError ValidateForSingleImage(const WebPMux* const mux) { |
| 312 |
const int num_images = MuxImageCount(mux->images_, WEBP_CHUNK_IMAGE); |
| 313 |
const int num_frames = MuxImageCount(mux->images_, WEBP_CHUNK_ANMF); |
| 314 |
|
| 315 |
if (num_images == 0) { |
| 316 |
|
| 317 |
return WEBP_MUX_NOT_FOUND; |
| 318 |
} else if (num_images == 1 && num_frames == 0) { |
| 319 |
|
| 320 |
return WEBP_MUX_OK; |
| 321 |
} else { |
| 322 |
|
| 323 |
return WEBP_MUX_INVALID_ARGUMENT; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
static WebPMuxError MuxGetCanvasInfo(const WebPMux* const mux, |
| 330 |
int* width, int* height, uint32_t* flags) { |
| 331 |
int w, h; |
| 332 |
uint32_t f = 0; |
| 333 |
WebPData data; |
| 334 |
assert(mux != NULL); |
| 335 |
|
| 336 |
|
| 337 |
if (MuxGet(mux, IDX_VP8X, 1, &data) == WEBP_MUX_OK) { |
| 338 |
if (data.size < VP8X_CHUNK_SIZE) return WEBP_MUX_BAD_DATA; |
| 339 |
f = GetLE32(data.bytes + 0); |
| 340 |
w = GetLE24(data.bytes + 4) + 1; |
| 341 |
h = GetLE24(data.bytes + 7) + 1; |
| 342 |
} else { |
| 343 |
const WebPMuxImage* const wpi = mux->images_; |
| 344 |
|
| 345 |
w = mux->canvas_width_; |
| 346 |
h = mux->canvas_height_; |
| 347 |
if (w == 0 && h == 0 && ValidateForSingleImage(mux) == WEBP_MUX_OK) { |
| 348 |
|
| 349 |
assert(wpi != NULL); |
| 350 |
w = wpi->width_; |
| 351 |
h = wpi->height_; |
| 352 |
} |
| 353 |
if (wpi != NULL) { |
| 354 |
if (wpi->has_alpha_) f |= ALPHA_FLAG; |
| 355 |
} |
| 356 |
} |
| 357 |
if (w * (uint64_t)h >= MAX_IMAGE_AREA) return WEBP_MUX_BAD_DATA; |
| 358 |
|
| 359 |
if (width != NULL) *width = w; |
| 360 |
if (height != NULL) *height = h; |
| 361 |
if (flags != NULL) *flags = f; |
| 362 |
return WEBP_MUX_OK; |
| 363 |
} |
| 364 |
|
| 365 |
WebPMuxError WebPMuxGetCanvasSize(const WebPMux* mux, int* width, int* height) { |
| 366 |
if (mux == NULL || width == NULL || height == NULL) { |
| 367 |
return WEBP_MUX_INVALID_ARGUMENT; |
| 368 |
} |
| 369 |
return MuxGetCanvasInfo(mux, width, height, NULL); |
| 370 |
} |
| 371 |
|
| 372 |
WebPMuxError WebPMuxGetFeatures(const WebPMux* mux, uint32_t* flags) { |
| 373 |
if (mux == NULL || flags == NULL) return WEBP_MUX_INVALID_ARGUMENT; |
| 374 |
return MuxGetCanvasInfo(mux, NULL, NULL, flags); |
| 375 |
} |
| 376 |
|
| 377 |
static uint8_t* EmitVP8XChunk(uint8_t* const dst, int width, |
| 378 |
int height, uint32_t flags) { |
| 379 |
const size_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE; |
| 380 |
assert(width >= 1 && height >= 1); |
| 381 |
assert(width <= MAX_CANVAS_SIZE && height <= MAX_CANVAS_SIZE); |
| 382 |
assert(width * (uint64_t)height < MAX_IMAGE_AREA); |
| 383 |
PutLE32(dst, MKFOURCC('V', 'P', '8', 'X')); |
| 384 |
PutLE32(dst + TAG_SIZE, VP8X_CHUNK_SIZE); |
| 385 |
PutLE32(dst + CHUNK_HEADER_SIZE, flags); |
| 386 |
PutLE24(dst + CHUNK_HEADER_SIZE + 4, width - 1); |
| 387 |
PutLE24(dst + CHUNK_HEADER_SIZE + 7, height - 1); |
| 388 |
return dst + vp8x_size; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
static WebPMuxError SynthesizeBitstream(const WebPMuxImage* const wpi, |
| 393 |
WebPData* const bitstream) { |
| 394 |
uint8_t* dst; |
| 395 |
|
| 396 |
|
| 397 |
const int need_vp8x = (wpi->alpha_ != NULL); |
| 398 |
const size_t vp8x_size = need_vp8x ? CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE : 0; |
| 399 |
const size_t alpha_size = need_vp8x ? ChunkDiskSize(wpi->alpha_) : 0; |
| 400 |
|
| 401 |
const size_t size = RIFF_HEADER_SIZE + vp8x_size + alpha_size + |
| 402 |
ChunkDiskSize(wpi->img_); |
| 403 |
uint8_t* const data = (uint8_t*)WebPSafeMalloc(1ULL, size); |
| 404 |
if (data == NULL) return WEBP_MUX_MEMORY_ERROR; |
| 405 |
|
| 406 |
|
| 407 |
assert(wpi->alpha_ == NULL || wpi->alpha_->next_ == NULL); |
| 408 |
assert(wpi->img_ != NULL && wpi->img_->next_ == NULL); |
| 409 |
|
| 410 |
|
| 411 |
dst = MuxEmitRiffHeader(data, size); |
| 412 |
|
| 413 |
if (need_vp8x) { |
| 414 |
dst = EmitVP8XChunk(dst, wpi->width_, wpi->height_, ALPHA_FLAG); |
| 415 |
dst = ChunkListEmit(wpi->alpha_, dst); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
dst = ChunkListEmit(wpi->img_, dst); |
| 420 |
assert(dst == data + size); |
| 421 |
|
| 422 |
|
| 423 |
bitstream->bytes = data; |
| 424 |
bitstream->size = size; |
| 425 |
return WEBP_MUX_OK; |
| 426 |
} |
| 427 |
|
| 428 |
WebPMuxError WebPMuxGetChunk(const WebPMux* mux, const char fourcc[4], |
| 429 |
WebPData* chunk_data) { |
| 430 |
CHUNK_INDEX idx; |
| 431 |
if (mux == NULL || fourcc == NULL || chunk_data == NULL) { |
| 432 |
return WEBP_MUX_INVALID_ARGUMENT; |
| 433 |
} |
| 434 |
idx = ChunkGetIndexFromFourCC(fourcc); |
| 435 |
assert(idx != IDX_LAST_CHUNK); |
| 436 |
if (IsWPI(kChunks[idx].id)) { |
| 437 |
return WEBP_MUX_INVALID_ARGUMENT; |
| 438 |
} else if (idx != IDX_UNKNOWN) { |
| 439 |
return MuxGet(mux, idx, 1, chunk_data); |
| 440 |
} else { |
| 441 |
const WebPChunk* const chunk = |
| 442 |
ChunkSearchList(mux->unknown_, 1, ChunkGetTagFromFourCC(fourcc)); |
| 443 |
if (chunk == NULL) return WEBP_MUX_NOT_FOUND; |
| 444 |
*chunk_data = chunk->data_; |
| 445 |
return WEBP_MUX_OK; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
static WebPMuxError MuxGetImageInternal(const WebPMuxImage* const wpi, |
| 450 |
WebPMuxFrameInfo* const info) { |
| 451 |
|
| 452 |
info->x_offset = 0; |
| 453 |
info->y_offset = 0; |
| 454 |
info->duration = 1; |
| 455 |
info->dispose_method = WEBP_MUX_DISPOSE_NONE; |
| 456 |
info->blend_method = WEBP_MUX_BLEND; |
| 457 |
|
| 458 |
info->id = ChunkGetIdFromTag(wpi->img_->tag_); |
| 459 |
return SynthesizeBitstream(wpi, &info->bitstream); |
| 460 |
} |
| 461 |
|
| 462 |
static WebPMuxError MuxGetFrameInternal(const WebPMuxImage* const wpi, |
| 463 |
WebPMuxFrameInfo* const frame) { |
| 464 |
const int is_frame = (wpi->header_->tag_ == kChunks[IDX_ANMF].tag); |
| 465 |
const WebPData* frame_data; |
| 466 |
if (!is_frame) return WEBP_MUX_INVALID_ARGUMENT; |
| 467 |
assert(wpi->header_ != NULL); |
| 468 |
|
| 469 |
frame_data = &wpi->header_->data_; |
| 470 |
if (frame_data->size < kChunks[IDX_ANMF].size) return WEBP_MUX_BAD_DATA; |
| 471 |
|
| 472 |
frame->x_offset = 2 * GetLE24(frame_data->bytes + 0); |
| 473 |
frame->y_offset = 2 * GetLE24(frame_data->bytes + 3); |
| 474 |
{ |
| 475 |
const uint8_t bits = frame_data->bytes[15]; |
| 476 |
frame->duration = GetLE24(frame_data->bytes + 12); |
| 477 |
frame->dispose_method = |
| 478 |
(bits & 1) ? WEBP_MUX_DISPOSE_BACKGROUND : WEBP_MUX_DISPOSE_NONE; |
| 479 |
frame->blend_method = (bits & 2) ? WEBP_MUX_NO_BLEND : WEBP_MUX_BLEND; |
| 480 |
} |
| 481 |
frame->id = ChunkGetIdFromTag(wpi->header_->tag_); |
| 482 |
return SynthesizeBitstream(wpi, &frame->bitstream); |
| 483 |
} |
| 484 |
|
| 485 |
WebPMuxError WebPMuxGetFrame( |
| 486 |
const WebPMux* mux, uint32_t nth, WebPMuxFrameInfo* frame) { |
| 487 |
WebPMuxError err; |
| 488 |
WebPMuxImage* wpi; |
| 489 |
|
| 490 |
if (mux == NULL || frame == NULL) { |
| 491 |
return WEBP_MUX_INVALID_ARGUMENT; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
err = MuxImageGetNth((const WebPMuxImage**)&mux->images_, nth, &wpi); |
| 496 |
if (err != WEBP_MUX_OK) return err; |
| 497 |
|
| 498 |
|
| 499 |
if (wpi->header_ == NULL) { |
| 500 |
return MuxGetImageInternal(wpi, frame); |
| 501 |
} else { |
| 502 |
return MuxGetFrameInternal(wpi, frame); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
WebPMuxError WebPMuxGetAnimationParams(const WebPMux* mux, |
| 507 |
WebPMuxAnimParams* params) { |
| 508 |
WebPData anim; |
| 509 |
WebPMuxError err; |
| 510 |
|
| 511 |
if (mux == NULL || params == NULL) return WEBP_MUX_INVALID_ARGUMENT; |
| 512 |
|
| 513 |
err = MuxGet(mux, IDX_ANIM, 1, &anim); |
| 514 |
if (err != WEBP_MUX_OK) return err; |
| 515 |
if (anim.size < kChunks[WEBP_CHUNK_ANIM].size) return WEBP_MUX_BAD_DATA; |
| 516 |
params->bgcolor = GetLE32(anim.bytes); |
| 517 |
params->loop_count = GetLE16(anim.bytes + 4); |
| 518 |
|
| 519 |
return WEBP_MUX_OK; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
static CHUNK_INDEX ChunkGetIndexFromId(WebPChunkId id) { |
| 524 |
int i; |
| 525 |
for (i = 0; kChunks[i].id != WEBP_CHUNK_NIL; ++i) { |
| 526 |
if (id == kChunks[i].id) return (CHUNK_INDEX)i; |
| 527 |
} |
| 528 |
return IDX_NIL; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
static int CountChunks(const WebPChunk* const chunk_list, uint32_t tag) { |
| 534 |
int count = 0; |
| 535 |
const WebPChunk* current; |
| 536 |
for (current = chunk_list; current != NULL; current = current->next_) { |
| 537 |
if (tag == NIL_TAG || current->tag_ == tag) { |
| 538 |
count++; |
| 539 |
} |
| 540 |
} |
| 541 |
return count; |
| 542 |
} |
| 543 |
|
| 544 |
WebPMuxError WebPMuxNumChunks(const WebPMux* mux, |
| 545 |
WebPChunkId id, int* num_elements) { |
| 546 |
if (mux == NULL || num_elements == NULL) { |
| 547 |
return WEBP_MUX_INVALID_ARGUMENT; |
| 548 |
} |
| 549 |
|
| 550 |
if (IsWPI(id)) { |
| 551 |
*num_elements = MuxImageCount(mux->images_, id); |
| 552 |
} else { |
| 553 |
WebPChunk* const* chunk_list = MuxGetChunkListFromId(mux, id); |
| 554 |
const CHUNK_INDEX idx = ChunkGetIndexFromId(id); |
| 555 |
*num_elements = CountChunks(*chunk_list, kChunks[idx].tag); |
| 556 |
} |
| 557 |
|
| 558 |
return WEBP_MUX_OK; |
| 559 |
} |
| 560 |
|
| 561 |
|