xref: /aosp_15_r20/external/webp/src/mux/muxread.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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 ([email protected])
13 //          Vikas ([email protected])
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   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 
MuxGet(const WebPMux * const mux,CHUNK_INDEX idx,uint32_t nth,WebPData * const data)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 // Fill the chunk with the given data (includes chunk header bytes), after some
55 // verifications.
ChunkVerifyAndAssign(WebPChunk * chunk,const uint8_t * data,size_t data_size,size_t riff_size,int copy_data)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   // Correctness checks.
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   // Data assignment.
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 
MuxImageFinalize(WebPMuxImage * const wpi)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     // Ignore ALPH chunk accompanying VP8L.
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 
MuxImageParse(const WebPChunk * const chunk,int copy_data,WebPMuxImage * const wpi)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   // ANMF.
116   {
117     const size_t hdr_size = ANMF_CHUNK_SIZE;
118     const WebPData temp = { bytes, hdr_size };
119     // Each of ANMF chunk contain a header at the beginning. So, its size should
120     // be at least 'hdr_size'.
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;  // Waiting for ALPH and/or VP8/VP8L chunks.
129 
130   // Rest of the chunks.
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;  // Consecutive ALPH chunks.
144         if (ChunkSetHead(&subchunk, &wpi->alpha_) != WEBP_MUX_OK) goto Fail;
145         wpi->is_partial_ = 1;  // Waiting for a VP8 chunk.
146         break;
147       case WEBP_CHUNK_IMAGE:
148         if (wpi->img_ != NULL) goto Fail;  // Only 1 image chunk allowed.
149         if (ChunkSetHead(&subchunk, &wpi->img_) != WEBP_MUX_OK) goto Fail;
150         if (!MuxImageFinalize(wpi)) goto Fail;
151         wpi->is_partial_ = 0;  // wpi is completely filled.
152         break;
153       case WEBP_CHUNK_UNKNOWN:
154         if (wpi->is_partial_) {
155           goto Fail;  // Encountered an unknown chunk
156                       // before some image chunks.
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 // Create a mux object from WebP-RIFF data.
179 
WebPMuxCreateInternal(const WebPData * bitstream,int copy_data,int version)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   // Stores the end of the chunk lists so that it is faster to append data to
191   // their ends.
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;  // version mismatch
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;  // First chunk should be VP8, VP8L or VP8X.
218   }
219 
220   riff_size = GetLE32(data + TAG_SIZE);
221   if (riff_size > MAX_CHUNK_PAYLOAD) goto Err;
222 
223   // Note this padding is historical and differs from demux.c which does not
224   // pad the file size.
225   riff_size = SizeWithPadding(riff_size);
226   if (riff_size < CHUNK_HEADER_SIZE) goto Err;
227   if (riff_size > size) goto Err;
228   // There's no point in reading past the end of the RIFF chunk.
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   // Loop over chunks.
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;  // Consecutive ALPH chunks.
254         if (ChunkSetHead(&chunk, &wpi->alpha_) != WEBP_MUX_OK) goto Err;
255         wpi->is_partial_ = 1;  // Waiting for a VP8 chunk.
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;  // wpi is completely filled.
261  PushImage:
262         // Add this to mux->images_ list.
263         if (MuxImagePush(wpi, &mux->images_) != WEBP_MUX_OK) goto Err;
264         MuxImageInit(wpi);  // Reset for reading next image.
265         break;
266       case WEBP_CHUNK_ANMF:
267         if (wpi->is_partial_) goto Err;  // Previous wpi is still incomplete.
268         if (!MuxImageParse(&chunk, copy_data, wpi)) goto Err;
269         ChunkRelease(&chunk);
270         goto PushImage;
271       default:  // A non-image chunk.
272         if (wpi->is_partial_) goto Err;  // Encountered a non-image chunk before
273                                          // getting all chunks of an image.
274         if (chunk_list_ends[id] == NULL) {
275           chunk_list_ends[id] =
276               MuxGetChunkListFromId(mux, id);  // List to add this chunk.
277         }
278         if (ChunkAppend(&chunk, &chunk_list_ends[id]) != WEBP_MUX_OK) goto Err;
279         if (id == WEBP_CHUNK_VP8X) {  // grab global specs
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   // Incomplete image.
292   if (wpi->is_partial_) goto Err;
293 
294   // Validate mux if complete.
295   if (MuxValidate(mux) != WEBP_MUX_OK) goto Err;
296 
297   MuxImageDelete(wpi);
298   return mux;  // All OK;
299 
300  Err:  // Something bad happened.
301   ChunkRelease(&chunk);
302   MuxImageDelete(wpi);
303   WebPMuxDelete(mux);
304   return NULL;
305 }
306 
307 //------------------------------------------------------------------------------
308 // Get API(s).
309 
310 // Validates that the given mux has a single image.
ValidateForSingleImage(const WebPMux * const mux)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     // No images in mux.
317     return WEBP_MUX_NOT_FOUND;
318   } else if (num_images == 1 && num_frames == 0) {
319     // Valid case (single image).
320     return WEBP_MUX_OK;
321   } else {
322     // Frame case OR an invalid mux.
323     return WEBP_MUX_INVALID_ARGUMENT;
324   }
325 }
326 
327 // Get the canvas width, height and flags after validating that VP8X/VP8/VP8L
328 // chunk and canvas size are valid.
MuxGetCanvasInfo(const WebPMux * const mux,int * width,int * height,uint32_t * flags)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   // Check if VP8X chunk is present.
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     // Grab user-forced canvas size as default.
345     w = mux->canvas_width_;
346     h = mux->canvas_height_;
347     if (w == 0 && h == 0 && ValidateForSingleImage(mux) == WEBP_MUX_OK) {
348       // single image and not forced canvas size => use dimension of first frame
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 
WebPMuxGetCanvasSize(const WebPMux * mux,int * width,int * height)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 
WebPMuxGetFeatures(const WebPMux * mux,uint32_t * flags)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 
EmitVP8XChunk(uint8_t * const dst,int width,int height,uint32_t flags)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 // Assemble a single image WebP bitstream from 'wpi'.
SynthesizeBitstream(const WebPMuxImage * const wpi,WebPData * const bitstream)392 static WebPMuxError SynthesizeBitstream(const WebPMuxImage* const wpi,
393                                         WebPData* const bitstream) {
394   uint8_t* dst;
395 
396   // Allocate data.
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   // Note: No need to output ANMF chunk for a single image.
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   // There should be at most one alpha_ chunk and exactly one img_ chunk.
407   assert(wpi->alpha_ == NULL || wpi->alpha_->next_ == NULL);
408   assert(wpi->img_ != NULL && wpi->img_->next_ == NULL);
409 
410   // Main RIFF header.
411   dst = MuxEmitRiffHeader(data, size);
412 
413   if (need_vp8x) {
414     dst = EmitVP8XChunk(dst, wpi->width_, wpi->height_, ALPHA_FLAG);  // VP8X.
415     dst = ChunkListEmit(wpi->alpha_, dst);       // ALPH.
416   }
417 
418   // Bitstream.
419   dst = ChunkListEmit(wpi->img_, dst);
420   assert(dst == data + size);
421 
422   // Output.
423   bitstream->bytes = data;
424   bitstream->size = size;
425   return WEBP_MUX_OK;
426 }
427 
WebPMuxGetChunk(const WebPMux * mux,const char fourcc[4],WebPData * chunk_data)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)) {     // An image chunk.
437     return WEBP_MUX_INVALID_ARGUMENT;
438   } else if (idx != IDX_UNKNOWN) {  // A known chunk type.
439     return MuxGet(mux, idx, 1, chunk_data);
440   } else {                          // An unknown chunk type.
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 
MuxGetImageInternal(const WebPMuxImage * const wpi,WebPMuxFrameInfo * const info)449 static WebPMuxError MuxGetImageInternal(const WebPMuxImage* const wpi,
450                                         WebPMuxFrameInfo* const info) {
451   // Set some defaults for unrelated fields.
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   // Extract data for related fields.
458   info->id = ChunkGetIdFromTag(wpi->img_->tag_);
459   return SynthesizeBitstream(wpi, &info->bitstream);
460 }
461 
MuxGetFrameInternal(const WebPMuxImage * const wpi,WebPMuxFrameInfo * const frame)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);  // Already checked by WebPMuxGetFrame().
468   // Get frame chunk.
469   frame_data = &wpi->header_->data_;
470   if (frame_data->size < kChunks[IDX_ANMF].size) return WEBP_MUX_BAD_DATA;
471   // Extract info.
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 
WebPMuxGetFrame(const WebPMux * mux,uint32_t nth,WebPMuxFrameInfo * frame)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   // Get the nth WebPMuxImage.
495   err = MuxImageGetNth((const WebPMuxImage**)&mux->images_, nth, &wpi);
496   if (err != WEBP_MUX_OK) return err;
497 
498   // Get frame info.
499   if (wpi->header_ == NULL) {
500     return MuxGetImageInternal(wpi, frame);
501   } else {
502     return MuxGetFrameInternal(wpi, frame);
503   }
504 }
505 
WebPMuxGetAnimationParams(const WebPMux * mux,WebPMuxAnimParams * params)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 // Get chunk index from chunk id. Returns IDX_NIL if not found.
ChunkGetIndexFromId(WebPChunkId id)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 // Count number of chunks matching 'tag' in the 'chunk_list'.
532 // If tag == NIL_TAG, any tag will be matched.
CountChunks(const WebPChunk * const chunk_list,uint32_t tag)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++;  // Count chunks whose tags match.
539     }
540   }
541   return count;
542 }
543 
WebPMuxNumChunks(const WebPMux * mux,WebPChunkId id,int * num_elements)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 //------------------------------------------------------------------------------
562