go-libwebp

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

muxi.h (7820B)


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