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