go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

muxread.c (19728B)


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