muxi.h 7.3 KB

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