Home | History | Annotate | Download | only in mux
      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 // Internal header for mux library.
     11 //
     12 // Author: Urvang (urvang (at) google.com)
     13 
     14 #ifndef WEBP_MUX_MUXI_H_
     15 #define WEBP_MUX_MUXI_H_
     16 
     17 #include <stdlib.h>
     18 #include "../dec/vp8i.h"
     19 #include "../dec/vp8li.h"
     20 #include "../webp/mux.h"
     21 
     22 #if defined(__cplusplus) || defined(c_plusplus)
     23 extern "C" {
     24 #endif
     25 
     26 //------------------------------------------------------------------------------
     27 // Defines and constants.
     28 
     29 #define MUX_MAJ_VERSION 0
     30 #define MUX_MIN_VERSION 1
     31 #define MUX_REV_VERSION 1
     32 
     33 // Chunk object.
     34 typedef struct WebPChunk WebPChunk;
     35 struct WebPChunk {
     36   uint32_t        tag_;
     37   int             owner_;  // True if *data_ memory is owned internally.
     38                            // VP8X, ANIM, and other internally created chunks
     39                            // like ANMF/FRGM are always owned.
     40   WebPData        data_;
     41   WebPChunk*      next_;
     42 };
     43 
     44 // MuxImage object. Store a full WebP image (including ANMF/FRGM chunk, ALPH
     45 // chunk and VP8/VP8L chunk),
     46 typedef struct WebPMuxImage WebPMuxImage;
     47 struct WebPMuxImage {
     48   WebPChunk*  header_;      // Corresponds to WEBP_CHUNK_ANMF/WEBP_CHUNK_FRGM.
     49   WebPChunk*  alpha_;       // Corresponds to WEBP_CHUNK_ALPHA.
     50   WebPChunk*  img_;         // Corresponds to WEBP_CHUNK_IMAGE.
     51   int         is_partial_;  // True if only some of the chunks are filled.
     52   WebPMuxImage* next_;
     53 };
     54 
     55 // Main mux object. Stores data chunks.
     56 struct WebPMux {
     57   WebPMuxImage*   images_;
     58   WebPChunk*      iccp_;
     59   WebPChunk*      exif_;
     60   WebPChunk*      xmp_;
     61   WebPChunk*      anim_;
     62   WebPChunk*      vp8x_;
     63 
     64   WebPChunk*  unknown_;
     65 };
     66 
     67 // CHUNK_INDEX enum: used for indexing within 'kChunks' (defined below) only.
     68 // Note: the reason for having two enums ('WebPChunkId' and 'CHUNK_INDEX') is to
     69 // allow two different chunks to have the same id (e.g. WebPChunkId
     70 // 'WEBP_CHUNK_IMAGE' can correspond to CHUNK_INDEX 'IDX_VP8' or 'IDX_VP8L').
     71 typedef enum {
     72   IDX_VP8X = 0,
     73   IDX_ICCP,
     74   IDX_ANIM,
     75   IDX_ANMF,
     76   IDX_FRGM,
     77   IDX_ALPHA,
     78   IDX_VP8,
     79   IDX_VP8L,
     80   IDX_EXIF,
     81   IDX_XMP,
     82   IDX_UNKNOWN,
     83 
     84   IDX_NIL,
     85   IDX_LAST_CHUNK
     86 } CHUNK_INDEX;
     87 
     88 #define NIL_TAG 0x00000000u  // To signal void chunk.
     89 
     90 typedef struct {
     91   uint32_t      tag;
     92   WebPChunkId   id;
     93   uint32_t      size;
     94 } ChunkInfo;
     95 
     96 extern const ChunkInfo kChunks[IDX_LAST_CHUNK];
     97 
     98 //------------------------------------------------------------------------------
     99 // Chunk object management.
    100 
    101 // Initialize.
    102 void ChunkInit(WebPChunk* const chunk);
    103 
    104 // Get chunk index from chunk tag. Returns IDX_NIL if not found.
    105 CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag);
    106 
    107 // Get chunk id from chunk tag. Returns WEBP_CHUNK_NIL if not found.
    108 WebPChunkId ChunkGetIdFromTag(uint32_t tag);
    109 
    110 // Convert a fourcc string to a tag.
    111 uint32_t ChunkGetTagFromFourCC(const char fourcc[4]);
    112 
    113 // Get chunk index from fourcc. Returns IDX_UNKNOWN if given fourcc is unknown.
    114 CHUNK_INDEX ChunkGetIndexFromFourCC(const char fourcc[4]);
    115 
    116 // Search for nth chunk with given 'tag' in the chunk list.
    117 // nth = 0 means "last of the list".
    118 WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag);
    119 
    120 // Fill the chunk with the given data.
    121 WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
    122                              int copy_data, uint32_t tag);
    123 
    124 // Sets 'chunk' at nth position in the 'chunk_list'.
    125 // nth = 0 has the special meaning "last of the list".
    126 // On success ownership is transferred from 'chunk' to the 'chunk_list'.
    127 WebPMuxError ChunkSetNth(WebPChunk* chunk, WebPChunk** chunk_list,
    128                          uint32_t nth);
    129 
    130 // Releases chunk and returns chunk->next_.
    131 WebPChunk* ChunkRelease(WebPChunk* const chunk);
    132 
    133 // Deletes given chunk & returns chunk->next_.
    134 WebPChunk* ChunkDelete(WebPChunk* const chunk);
    135 
    136 // Returns size of the chunk including chunk header and padding byte (if any).
    137 static WEBP_INLINE size_t SizeWithPadding(size_t chunk_size) {
    138   return CHUNK_HEADER_SIZE + ((chunk_size + 1) & ~1U);
    139 }
    140 
    141 // Size of a chunk including header and padding.
    142 static WEBP_INLINE size_t ChunkDiskSize(const WebPChunk* chunk) {
    143   const size_t data_size = chunk->data_.size;
    144   assert(data_size < MAX_CHUNK_PAYLOAD);
    145   return SizeWithPadding(data_size);
    146 }
    147 
    148 // Total size of a list of chunks.
    149 size_t ChunksListDiskSize(const WebPChunk* chunk_list);
    150 
    151 // Write out the given list of chunks into 'dst'.
    152 uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst);
    153 
    154 // Get the width & height of image stored in 'image_chunk'.
    155 WebPMuxError MuxGetImageWidthHeight(const WebPChunk* const image_chunk,
    156                                     int* const width, int* const height);
    157 
    158 //------------------------------------------------------------------------------
    159 // MuxImage object management.
    160 
    161 // Initialize.
    162 void MuxImageInit(WebPMuxImage* const wpi);
    163 
    164 // Releases image 'wpi' and returns wpi->next.
    165 WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi);
    166 
    167 // Delete image 'wpi' and return the next image in the list or NULL.
    168 // 'wpi' can be NULL.
    169 WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi);
    170 
    171 // Delete all images in 'wpi_list'.
    172 void MuxImageDeleteAll(WebPMuxImage** const wpi_list);
    173 
    174 // Count number of images matching the given tag id in the 'wpi_list'.
    175 // If id == WEBP_CHUNK_NIL, all images will be matched.
    176 int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id);
    177 
    178 // Check if given ID corresponds to an image related chunk.
    179 static WEBP_INLINE int IsWPI(WebPChunkId id) {
    180   switch (id) {
    181     case WEBP_CHUNK_ANMF:
    182     case WEBP_CHUNK_FRGM:
    183     case WEBP_CHUNK_ALPHA:
    184     case WEBP_CHUNK_IMAGE:  return 1;
    185     default:        return 0;
    186   }
    187 }
    188 
    189 // Get a reference to appropriate chunk list within an image given chunk tag.
    190 static WEBP_INLINE WebPChunk** MuxImageGetListFromId(
    191     const WebPMuxImage* const wpi, WebPChunkId id) {
    192   assert(wpi != NULL);
    193   switch (id) {
    194     case WEBP_CHUNK_ANMF:
    195     case WEBP_CHUNK_FRGM:  return (WebPChunk**)&wpi->header_;
    196     case WEBP_CHUNK_ALPHA: return (WebPChunk**)&wpi->alpha_;
    197     case WEBP_CHUNK_IMAGE: return (WebPChunk**)&wpi->img_;
    198     default: return NULL;
    199   }
    200 }
    201 
    202 // Pushes 'wpi' at the end of 'wpi_list'.
    203 WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list);
    204 
    205 // Delete nth image in the image list.
    206 WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth);
    207 
    208 // Get nth image in the image list.
    209 WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
    210                             WebPMuxImage** wpi);
    211 
    212 // Total size of the given image.
    213 size_t MuxImageDiskSize(const WebPMuxImage* const wpi);
    214 
    215 // Total size of a list of images.
    216 size_t MuxImageListDiskSize(const WebPMuxImage* wpi_list);
    217 
    218 // Write out the given image into 'dst'.
    219 uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst);
    220 
    221 // Write out the given list of images into 'dst'.
    222 uint8_t* MuxImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst);
    223 
    224 //------------------------------------------------------------------------------
    225 // Helper methods for mux.
    226 
    227 // Checks if the given image list contains at least one lossless image.
    228 int MuxHasLosslessImages(const WebPMuxImage* images);
    229 
    230 // Write out RIFF header into 'data', given total data size 'size'.
    231 uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size);
    232 
    233 // Returns the list where chunk with given ID is to be inserted in mux.
    234 // Return value is NULL if this chunk should be inserted in mux->images_ list
    235 // or if 'id' is not known.
    236 WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id);
    237 
    238 // Validates that the given mux has a single image.
    239 WebPMuxError MuxValidateForImage(const WebPMux* const mux);
    240 
    241 // Validates the given mux object.
    242 WebPMuxError MuxValidate(const WebPMux* const mux);
    243 
    244 //------------------------------------------------------------------------------
    245 
    246 #if defined(__cplusplus) || defined(c_plusplus)
    247 }    // extern "C"
    248 #endif
    249 
    250 #endif  /* WEBP_MUX_MUXI_H_ */
    251