mux.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // RIFF container manipulation for WebP images.
  11. //
  12. // Authors: Urvang (urvang@google.com)
  13. // Vikas (vikasa@google.com)
  14. // This API allows manipulation of WebP container images containing features
  15. // like color profile, metadata, animation and fragmented images.
  16. //
  17. // Code Example#1: Create a WebPMux object with image data, color profile and
  18. // XMP metadata.
  19. /*
  20. int copy_data = 0;
  21. WebPMux* mux = WebPMuxNew();
  22. // ... (Prepare image data).
  23. WebPMuxSetImage(mux, &image, copy_data);
  24. // ... (Prepare ICCP color profile data).
  25. WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);
  26. // ... (Prepare XMP metadata).
  27. WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data);
  28. // Get data from mux in WebP RIFF format.
  29. WebPMuxAssemble(mux, &output_data);
  30. WebPMuxDelete(mux);
  31. // ... (Consume output_data; e.g. write output_data.bytes to file).
  32. WebPDataClear(&output_data);
  33. */
  34. // Code Example#2: Get image and color profile data from a WebP file.
  35. /*
  36. int copy_data = 0;
  37. // ... (Read data from file).
  38. WebPMux* mux = WebPMuxCreate(&data, copy_data);
  39. WebPMuxGetFrame(mux, 1, &image);
  40. // ... (Consume image; e.g. call WebPDecode() to decode the data).
  41. WebPMuxGetChunk(mux, "ICCP", &icc_profile);
  42. // ... (Consume icc_data).
  43. WebPMuxDelete(mux);
  44. free(data);
  45. */
  46. #ifndef WEBP_WEBP_MUX_H_
  47. #define WEBP_WEBP_MUX_H_
  48. #include "./mux_types.h"
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. #define WEBP_MUX_ABI_VERSION 0x0101 // MAJOR(8b) + MINOR(8b)
  53. // Note: forward declaring enumerations is not allowed in (strict) C and C++,
  54. // the types are left here for reference.
  55. // typedef enum WebPMuxError WebPMuxError;
  56. // typedef enum WebPChunkId WebPChunkId;
  57. typedef struct WebPMux WebPMux; // main opaque object.
  58. typedef struct WebPMuxFrameInfo WebPMuxFrameInfo;
  59. typedef struct WebPMuxAnimParams WebPMuxAnimParams;
  60. // Error codes
  61. typedef enum WebPMuxError {
  62. WEBP_MUX_OK = 1,
  63. WEBP_MUX_NOT_FOUND = 0,
  64. WEBP_MUX_INVALID_ARGUMENT = -1,
  65. WEBP_MUX_BAD_DATA = -2,
  66. WEBP_MUX_MEMORY_ERROR = -3,
  67. WEBP_MUX_NOT_ENOUGH_DATA = -4
  68. } WebPMuxError;
  69. // IDs for different types of chunks.
  70. typedef enum WebPChunkId {
  71. WEBP_CHUNK_VP8X, // VP8X
  72. WEBP_CHUNK_ICCP, // ICCP
  73. WEBP_CHUNK_ANIM, // ANIM
  74. WEBP_CHUNK_ANMF, // ANMF
  75. WEBP_CHUNK_FRGM, // FRGM
  76. WEBP_CHUNK_ALPHA, // ALPH
  77. WEBP_CHUNK_IMAGE, // VP8/VP8L
  78. WEBP_CHUNK_EXIF, // EXIF
  79. WEBP_CHUNK_XMP, // XMP
  80. WEBP_CHUNK_UNKNOWN, // Other chunks.
  81. WEBP_CHUNK_NIL
  82. } WebPChunkId;
  83. //------------------------------------------------------------------------------
  84. // Returns the version number of the mux library, packed in hexadecimal using
  85. // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  86. WEBP_EXTERN(int) WebPGetMuxVersion(void);
  87. //------------------------------------------------------------------------------
  88. // Life of a Mux object
  89. // Internal, version-checked, entry point
  90. WEBP_EXTERN(WebPMux*) WebPNewInternal(int);
  91. // Creates an empty mux object.
  92. // Returns:
  93. // A pointer to the newly created empty mux object.
  94. // Or NULL in case of memory error.
  95. static WEBP_INLINE WebPMux* WebPMuxNew(void) {
  96. return WebPNewInternal(WEBP_MUX_ABI_VERSION);
  97. }
  98. // Deletes the mux object.
  99. // Parameters:
  100. // mux - (in/out) object to be deleted
  101. WEBP_EXTERN(void) WebPMuxDelete(WebPMux* mux);
  102. //------------------------------------------------------------------------------
  103. // Mux creation.
  104. // Internal, version-checked, entry point
  105. WEBP_EXTERN(WebPMux*) WebPMuxCreateInternal(const WebPData*, int, int);
  106. // Creates a mux object from raw data given in WebP RIFF format.
  107. // Parameters:
  108. // bitstream - (in) the bitstream data in WebP RIFF format
  109. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  110. // object and value 0 indicates data will NOT be copied.
  111. // Returns:
  112. // A pointer to the mux object created from given data - on success.
  113. // NULL - In case of invalid data or memory error.
  114. static WEBP_INLINE WebPMux* WebPMuxCreate(const WebPData* bitstream,
  115. int copy_data) {
  116. return WebPMuxCreateInternal(bitstream, copy_data, WEBP_MUX_ABI_VERSION);
  117. }
  118. //------------------------------------------------------------------------------
  119. // Non-image chunks.
  120. // Note: Only non-image related chunks should be managed through chunk APIs.
  121. // (Image related chunks are: "ANMF", "FRGM", "VP8 ", "VP8L" and "ALPH").
  122. // To add, get and delete images, use WebPMuxSetImage(), WebPMuxPushFrame(),
  123. // WebPMuxGetFrame() and WebPMuxDeleteFrame().
  124. // Adds a chunk with id 'fourcc' and data 'chunk_data' in the mux object.
  125. // Any existing chunk(s) with the same id will be removed.
  126. // Parameters:
  127. // mux - (in/out) object to which the chunk is to be added
  128. // fourcc - (in) a character array containing the fourcc of the given chunk;
  129. // e.g., "ICCP", "XMP ", "EXIF" etc.
  130. // chunk_data - (in) the chunk data to be added
  131. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  132. // object and value 0 indicates data will NOT be copied.
  133. // Returns:
  134. // WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL
  135. // or if fourcc corresponds to an image chunk.
  136. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  137. // WEBP_MUX_OK - on success.
  138. WEBP_EXTERN(WebPMuxError) WebPMuxSetChunk(
  139. WebPMux* mux, const char fourcc[4], const WebPData* chunk_data,
  140. int copy_data);
  141. // Gets a reference to the data of the chunk with id 'fourcc' in the mux object.
  142. // The caller should NOT free the returned data.
  143. // Parameters:
  144. // mux - (in) object from which the chunk data is to be fetched
  145. // fourcc - (in) a character array containing the fourcc of the chunk;
  146. // e.g., "ICCP", "XMP ", "EXIF" etc.
  147. // chunk_data - (out) returned chunk data
  148. // Returns:
  149. // WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL
  150. // or if fourcc corresponds to an image chunk.
  151. // WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given id.
  152. // WEBP_MUX_OK - on success.
  153. WEBP_EXTERN(WebPMuxError) WebPMuxGetChunk(
  154. const WebPMux* mux, const char fourcc[4], WebPData* chunk_data);
  155. // Deletes the chunk with the given 'fourcc' from the mux object.
  156. // Parameters:
  157. // mux - (in/out) object from which the chunk is to be deleted
  158. // fourcc - (in) a character array containing the fourcc of the chunk;
  159. // e.g., "ICCP", "XMP ", "EXIF" etc.
  160. // Returns:
  161. // WEBP_MUX_INVALID_ARGUMENT - if mux or fourcc is NULL
  162. // or if fourcc corresponds to an image chunk.
  163. // WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given fourcc.
  164. // WEBP_MUX_OK - on success.
  165. WEBP_EXTERN(WebPMuxError) WebPMuxDeleteChunk(
  166. WebPMux* mux, const char fourcc[4]);
  167. //------------------------------------------------------------------------------
  168. // Images.
  169. // Encapsulates data about a single frame/fragment.
  170. struct WebPMuxFrameInfo {
  171. WebPData bitstream; // image data: can be a raw VP8/VP8L bitstream
  172. // or a single-image WebP file.
  173. int x_offset; // x-offset of the frame.
  174. int y_offset; // y-offset of the frame.
  175. int duration; // duration of the frame (in milliseconds).
  176. WebPChunkId id; // frame type: should be one of WEBP_CHUNK_ANMF,
  177. // WEBP_CHUNK_FRGM or WEBP_CHUNK_IMAGE
  178. WebPMuxAnimDispose dispose_method; // Disposal method for the frame.
  179. WebPMuxAnimBlend blend_method; // Blend operation for the frame.
  180. uint32_t pad[1]; // padding for later use
  181. };
  182. // Sets the (non-animated and non-fragmented) image in the mux object.
  183. // Note: Any existing images (including frames/fragments) will be removed.
  184. // Parameters:
  185. // mux - (in/out) object in which the image is to be set
  186. // bitstream - (in) can be a raw VP8/VP8L bitstream or a single-image
  187. // WebP file (non-animated and non-fragmented)
  188. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  189. // object and value 0 indicates data will NOT be copied.
  190. // Returns:
  191. // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or bitstream is NULL.
  192. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  193. // WEBP_MUX_OK - on success.
  194. WEBP_EXTERN(WebPMuxError) WebPMuxSetImage(
  195. WebPMux* mux, const WebPData* bitstream, int copy_data);
  196. // Adds a frame at the end of the mux object.
  197. // Notes: (1) frame.id should be one of WEBP_CHUNK_ANMF or WEBP_CHUNK_FRGM
  198. // (2) For setting a non-animated non-fragmented image, use
  199. // WebPMuxSetImage() instead.
  200. // (3) Type of frame being pushed must be same as the frames in mux.
  201. // (4) As WebP only supports even offsets, any odd offset will be snapped
  202. // to an even location using: offset &= ~1
  203. // Parameters:
  204. // mux - (in/out) object to which the frame is to be added
  205. // frame - (in) frame data.
  206. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  207. // object and value 0 indicates data will NOT be copied.
  208. // Returns:
  209. // WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL
  210. // or if content of 'frame' is invalid.
  211. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  212. // WEBP_MUX_OK - on success.
  213. WEBP_EXTERN(WebPMuxError) WebPMuxPushFrame(
  214. WebPMux* mux, const WebPMuxFrameInfo* frame, int copy_data);
  215. // Gets the nth frame from the mux object.
  216. // The content of 'frame->bitstream' is allocated using malloc(), and NOT
  217. // owned by the 'mux' object. It MUST be deallocated by the caller by calling
  218. // WebPDataClear().
  219. // nth=0 has a special meaning - last position.
  220. // Parameters:
  221. // mux - (in) object from which the info is to be fetched
  222. // nth - (in) index of the frame in the mux object
  223. // frame - (out) data of the returned frame
  224. // Returns:
  225. // WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL.
  226. // WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object.
  227. // WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid.
  228. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  229. // WEBP_MUX_OK - on success.
  230. WEBP_EXTERN(WebPMuxError) WebPMuxGetFrame(
  231. const WebPMux* mux, uint32_t nth, WebPMuxFrameInfo* frame);
  232. // Deletes a frame from the mux object.
  233. // nth=0 has a special meaning - last position.
  234. // Parameters:
  235. // mux - (in/out) object from which a frame is to be deleted
  236. // nth - (in) The position from which the frame is to be deleted
  237. // Returns:
  238. // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL.
  239. // WEBP_MUX_NOT_FOUND - If there are less than nth frames in the mux object
  240. // before deletion.
  241. // WEBP_MUX_OK - on success.
  242. WEBP_EXTERN(WebPMuxError) WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth);
  243. //------------------------------------------------------------------------------
  244. // Animation.
  245. // Animation parameters.
  246. struct WebPMuxAnimParams {
  247. uint32_t bgcolor; // Background color of the canvas stored (in MSB order) as:
  248. // Bits 00 to 07: Alpha.
  249. // Bits 08 to 15: Red.
  250. // Bits 16 to 23: Green.
  251. // Bits 24 to 31: Blue.
  252. int loop_count; // Number of times to repeat the animation [0 = infinite].
  253. };
  254. // Sets the animation parameters in the mux object. Any existing ANIM chunks
  255. // will be removed.
  256. // Parameters:
  257. // mux - (in/out) object in which ANIM chunk is to be set/added
  258. // params - (in) animation parameters.
  259. // Returns:
  260. // WEBP_MUX_INVALID_ARGUMENT - if mux or params is NULL.
  261. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  262. // WEBP_MUX_OK - on success.
  263. WEBP_EXTERN(WebPMuxError) WebPMuxSetAnimationParams(
  264. WebPMux* mux, const WebPMuxAnimParams* params);
  265. // Gets the animation parameters from the mux object.
  266. // Parameters:
  267. // mux - (in) object from which the animation parameters to be fetched
  268. // params - (out) animation parameters extracted from the ANIM chunk
  269. // Returns:
  270. // WEBP_MUX_INVALID_ARGUMENT - if mux or params is NULL.
  271. // WEBP_MUX_NOT_FOUND - if ANIM chunk is not present in mux object.
  272. // WEBP_MUX_OK - on success.
  273. WEBP_EXTERN(WebPMuxError) WebPMuxGetAnimationParams(
  274. const WebPMux* mux, WebPMuxAnimParams* params);
  275. //------------------------------------------------------------------------------
  276. // Misc Utilities.
  277. #if WEBP_MUX_ABI_VERSION > 0x0101
  278. // Sets the canvas size for the mux object. The width and height can be
  279. // specified explicitly or left as zero (0, 0).
  280. // * When width and height are specified explicitly, then this frame bound is
  281. // enforced during subsequent calls to WebPMuxAssemble() and an error is
  282. // reported if any animated frame does not completely fit within the canvas.
  283. // * When unspecified (0, 0), the constructed canvas will get the frame bounds
  284. // from the bounding-box over all frames after calling WebPMuxAssemble().
  285. // Parameters:
  286. // mux - (in) object to which the canvas size is to be set
  287. // width - (in) canvas width
  288. // height - (in) canvas height
  289. // Returns:
  290. // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL; or
  291. // width or height are invalid or out of bounds
  292. // WEBP_MUX_OK - on success.
  293. WEBP_EXTERN(WebPMuxError) WebPMuxSetCanvasSize(WebPMux* mux,
  294. int width, int height);
  295. #endif
  296. // Gets the canvas size from the mux object.
  297. // Note: This method assumes that the VP8X chunk, if present, is up-to-date.
  298. // That is, the mux object hasn't been modified since the last call to
  299. // WebPMuxAssemble() or WebPMuxCreate().
  300. // Parameters:
  301. // mux - (in) object from which the canvas size is to be fetched
  302. // width - (out) canvas width
  303. // height - (out) canvas height
  304. // Returns:
  305. // WEBP_MUX_INVALID_ARGUMENT - if mux, width or height is NULL.
  306. // WEBP_MUX_BAD_DATA - if VP8X/VP8/VP8L chunk or canvas size is invalid.
  307. // WEBP_MUX_OK - on success.
  308. WEBP_EXTERN(WebPMuxError) WebPMuxGetCanvasSize(const WebPMux* mux,
  309. int* width, int* height);
  310. // Gets the feature flags from the mux object.
  311. // Note: This method assumes that the VP8X chunk, if present, is up-to-date.
  312. // That is, the mux object hasn't been modified since the last call to
  313. // WebPMuxAssemble() or WebPMuxCreate().
  314. // Parameters:
  315. // mux - (in) object from which the features are to be fetched
  316. // flags - (out) the flags specifying which features are present in the
  317. // mux object. This will be an OR of various flag values.
  318. // Enum 'WebPFeatureFlags' can be used to test individual flag values.
  319. // Returns:
  320. // WEBP_MUX_INVALID_ARGUMENT - if mux or flags is NULL.
  321. // WEBP_MUX_BAD_DATA - if VP8X/VP8/VP8L chunk or canvas size is invalid.
  322. // WEBP_MUX_OK - on success.
  323. WEBP_EXTERN(WebPMuxError) WebPMuxGetFeatures(const WebPMux* mux,
  324. uint32_t* flags);
  325. // Gets number of chunks with the given 'id' in the mux object.
  326. // Parameters:
  327. // mux - (in) object from which the info is to be fetched
  328. // id - (in) chunk id specifying the type of chunk
  329. // num_elements - (out) number of chunks with the given chunk id
  330. // Returns:
  331. // WEBP_MUX_INVALID_ARGUMENT - if mux, or num_elements is NULL.
  332. // WEBP_MUX_OK - on success.
  333. WEBP_EXTERN(WebPMuxError) WebPMuxNumChunks(const WebPMux* mux,
  334. WebPChunkId id, int* num_elements);
  335. // Assembles all chunks in WebP RIFF format and returns in 'assembled_data'.
  336. // This function also validates the mux object.
  337. // Note: The content of 'assembled_data' will be ignored and overwritten.
  338. // Also, the content of 'assembled_data' is allocated using malloc(), and NOT
  339. // owned by the 'mux' object. It MUST be deallocated by the caller by calling
  340. // WebPDataClear(). It's always safe to call WebPDataClear() upon return,
  341. // even in case of error.
  342. // Parameters:
  343. // mux - (in/out) object whose chunks are to be assembled
  344. // assembled_data - (out) assembled WebP data
  345. // Returns:
  346. // WEBP_MUX_BAD_DATA - if mux object is invalid.
  347. // WEBP_MUX_INVALID_ARGUMENT - if mux or assembled_data is NULL.
  348. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  349. // WEBP_MUX_OK - on success.
  350. WEBP_EXTERN(WebPMuxError) WebPMuxAssemble(WebPMux* mux,
  351. WebPData* assembled_data);
  352. //------------------------------------------------------------------------------
  353. #ifdef __cplusplus
  354. } // extern "C"
  355. #endif
  356. #endif /* WEBP_WEBP_MUX_H_ */