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