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