muxi.h 7.6 KB

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