mux.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 and encoding for WebP images.
  11. //
  12. // Authors: Urvang (urvang@google.com)
  13. // Vikas (vikasa@google.com)
  14. #ifndef WEBP_WEBP_MUX_H_
  15. #define WEBP_WEBP_MUX_H_
  16. #include "./mux_types.h"
  17. #include "./types.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define WEBP_MUX_ABI_VERSION 0x0109 // MAJOR(8b) + MINOR(8b)
  22. //------------------------------------------------------------------------------
  23. // Mux API
  24. //
  25. // This API allows manipulation of WebP container images containing features
  26. // like color profile, metadata, animation.
  27. //
  28. // Code Example#1: Create a WebPMux object with image data, color profile and
  29. // XMP metadata.
  30. /*
  31. int copy_data = 0;
  32. WebPMux* mux = WebPMuxNew();
  33. // ... (Prepare image data).
  34. WebPMuxSetImage(mux, &image, copy_data);
  35. // ... (Prepare ICCP color profile data).
  36. WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);
  37. // ... (Prepare XMP metadata).
  38. WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data);
  39. // Get data from mux in WebP RIFF format.
  40. WebPMuxAssemble(mux, &output_data);
  41. WebPMuxDelete(mux);
  42. // ... (Consume output_data; e.g. write output_data.bytes to file).
  43. WebPDataClear(&output_data);
  44. */
  45. // Code Example#2: Get image and color profile data from a WebP file.
  46. /*
  47. int copy_data = 0;
  48. // ... (Read data from file).
  49. WebPMux* mux = WebPMuxCreate(&data, copy_data);
  50. WebPMuxGetFrame(mux, 1, &image);
  51. // ... (Consume image; e.g. call WebPDecode() to decode the data).
  52. WebPMuxGetChunk(mux, "ICCP", &icc_profile);
  53. // ... (Consume icc_data).
  54. WebPMuxDelete(mux);
  55. WebPFree(data);
  56. */
  57. // Note: forward declaring enumerations is not allowed in (strict) C and C++,
  58. // the types are left here for reference.
  59. // typedef enum WebPMuxError WebPMuxError;
  60. // typedef enum WebPChunkId WebPChunkId;
  61. typedef struct WebPMux WebPMux; // main opaque object.
  62. typedef struct WebPMuxFrameInfo WebPMuxFrameInfo;
  63. typedef struct WebPMuxAnimParams WebPMuxAnimParams;
  64. typedef struct WebPAnimEncoderOptions WebPAnimEncoderOptions;
  65. // Error codes
  66. typedef enum WEBP_NODISCARD WebPMuxError {
  67. WEBP_MUX_OK = 1,
  68. WEBP_MUX_NOT_FOUND = 0,
  69. WEBP_MUX_INVALID_ARGUMENT = -1,
  70. WEBP_MUX_BAD_DATA = -2,
  71. WEBP_MUX_MEMORY_ERROR = -3,
  72. WEBP_MUX_NOT_ENOUGH_DATA = -4
  73. } WebPMuxError;
  74. // IDs for different types of chunks.
  75. typedef enum WebPChunkId {
  76. WEBP_CHUNK_VP8X, // VP8X
  77. WEBP_CHUNK_ICCP, // ICCP
  78. WEBP_CHUNK_ANIM, // ANIM
  79. WEBP_CHUNK_ANMF, // ANMF
  80. WEBP_CHUNK_DEPRECATED, // (deprecated from FRGM)
  81. WEBP_CHUNK_ALPHA, // ALPH
  82. WEBP_CHUNK_IMAGE, // VP8/VP8L
  83. WEBP_CHUNK_EXIF, // EXIF
  84. WEBP_CHUNK_XMP, // XMP
  85. WEBP_CHUNK_UNKNOWN, // Other chunks.
  86. WEBP_CHUNK_NIL
  87. } WebPChunkId;
  88. //------------------------------------------------------------------------------
  89. // Returns the version number of the mux library, packed in hexadecimal using
  90. // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  91. WEBP_EXTERN int WebPGetMuxVersion(void);
  92. //------------------------------------------------------------------------------
  93. // Life of a Mux object
  94. // Internal, version-checked, entry point
  95. WEBP_NODISCARD WEBP_EXTERN WebPMux* WebPNewInternal(int);
  96. // Creates an empty mux object.
  97. // Returns:
  98. // A pointer to the newly created empty mux object.
  99. // Or NULL in case of memory error.
  100. WEBP_NODISCARD static WEBP_INLINE WebPMux* WebPMuxNew(void) {
  101. return WebPNewInternal(WEBP_MUX_ABI_VERSION);
  102. }
  103. // Deletes the mux object.
  104. // Parameters:
  105. // mux - (in/out) object to be deleted
  106. WEBP_EXTERN void WebPMuxDelete(WebPMux* mux);
  107. //------------------------------------------------------------------------------
  108. // Mux creation.
  109. // Internal, version-checked, entry point
  110. WEBP_NODISCARD WEBP_EXTERN WebPMux* WebPMuxCreateInternal(const WebPData*, int,
  111. int);
  112. // Creates a mux object from raw data given in WebP RIFF format.
  113. // Parameters:
  114. // bitstream - (in) the bitstream data in WebP RIFF format
  115. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  116. // object and value 0 indicates data will NOT be copied. If the
  117. // data is not copied, it must exist for the lifetime of the
  118. // mux object.
  119. // Returns:
  120. // A pointer to the mux object created from given data - on success.
  121. // NULL - In case of invalid data or memory error.
  122. WEBP_NODISCARD static WEBP_INLINE WebPMux* WebPMuxCreate(
  123. const WebPData* bitstream, int copy_data) {
  124. return WebPMuxCreateInternal(bitstream, copy_data, WEBP_MUX_ABI_VERSION);
  125. }
  126. //------------------------------------------------------------------------------
  127. // Non-image chunks.
  128. // Note: Only non-image related chunks should be managed through chunk APIs.
  129. // (Image related chunks are: "ANMF", "VP8 ", "VP8L" and "ALPH").
  130. // To add, get and delete images, use WebPMuxSetImage(), WebPMuxPushFrame(),
  131. // WebPMuxGetFrame() and WebPMuxDeleteFrame().
  132. // Adds a chunk with id 'fourcc' and data 'chunk_data' in the mux object.
  133. // Any existing chunk(s) with the same id will be removed.
  134. // Parameters:
  135. // mux - (in/out) object to which the chunk is to be added
  136. // fourcc - (in) a character array containing the fourcc of the given chunk;
  137. // e.g., "ICCP", "XMP ", "EXIF" etc.
  138. // chunk_data - (in) the chunk data to be added
  139. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  140. // object and value 0 indicates data will NOT be copied. If the
  141. // data is not copied, it must exist until a call to
  142. // WebPMuxAssemble() is made.
  143. // Returns:
  144. // WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL
  145. // or if fourcc corresponds to an image chunk.
  146. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  147. // WEBP_MUX_OK - on success.
  148. WEBP_EXTERN WebPMuxError WebPMuxSetChunk(
  149. WebPMux* mux, const char fourcc[4], const WebPData* chunk_data,
  150. int copy_data);
  151. // Gets a reference to the data of the chunk with id 'fourcc' in the mux object.
  152. // The caller should NOT free the returned data.
  153. // Parameters:
  154. // mux - (in) object from which the chunk data is to be fetched
  155. // fourcc - (in) a character array containing the fourcc of the chunk;
  156. // e.g., "ICCP", "XMP ", "EXIF" etc.
  157. // chunk_data - (out) returned chunk data
  158. // Returns:
  159. // WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL
  160. // or if fourcc corresponds to an image chunk.
  161. // WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given id.
  162. // WEBP_MUX_OK - on success.
  163. WEBP_EXTERN WebPMuxError WebPMuxGetChunk(
  164. const WebPMux* mux, const char fourcc[4], WebPData* chunk_data);
  165. // Deletes the chunk with the given 'fourcc' from the mux object.
  166. // Parameters:
  167. // mux - (in/out) object from which the chunk is to be deleted
  168. // fourcc - (in) a character array containing the fourcc of the chunk;
  169. // e.g., "ICCP", "XMP ", "EXIF" etc.
  170. // Returns:
  171. // WEBP_MUX_INVALID_ARGUMENT - if mux or fourcc is NULL
  172. // or if fourcc corresponds to an image chunk.
  173. // WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given fourcc.
  174. // WEBP_MUX_OK - on success.
  175. WEBP_EXTERN WebPMuxError WebPMuxDeleteChunk(
  176. WebPMux* mux, const char fourcc[4]);
  177. //------------------------------------------------------------------------------
  178. // Images.
  179. // Encapsulates data about a single frame.
  180. struct WebPMuxFrameInfo {
  181. WebPData bitstream; // image data: can be a raw VP8/VP8L bitstream
  182. // or a single-image WebP file.
  183. int x_offset; // x-offset of the frame.
  184. int y_offset; // y-offset of the frame.
  185. int duration; // duration of the frame (in milliseconds).
  186. WebPChunkId id; // frame type: should be one of WEBP_CHUNK_ANMF
  187. // or WEBP_CHUNK_IMAGE
  188. WebPMuxAnimDispose dispose_method; // Disposal method for the frame.
  189. WebPMuxAnimBlend blend_method; // Blend operation for the frame.
  190. uint32_t pad[1]; // padding for later use
  191. };
  192. // Sets the (non-animated) image in the mux object.
  193. // Note: Any existing images (including frames) will be removed.
  194. // Parameters:
  195. // mux - (in/out) object in which the image is to be set
  196. // bitstream - (in) can be a raw VP8/VP8L bitstream or a single-image
  197. // WebP file (non-animated)
  198. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  199. // object and value 0 indicates data will NOT be copied. If the
  200. // data is not copied, it must exist until a call to
  201. // WebPMuxAssemble() is made.
  202. // Returns:
  203. // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or bitstream is NULL.
  204. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  205. // WEBP_MUX_OK - on success.
  206. WEBP_EXTERN WebPMuxError WebPMuxSetImage(
  207. WebPMux* mux, const WebPData* bitstream, int copy_data);
  208. // Adds a frame at the end of the mux object.
  209. // Notes: (1) frame.id should be WEBP_CHUNK_ANMF
  210. // (2) For setting a non-animated image, use WebPMuxSetImage() instead.
  211. // (3) Type of frame being pushed must be same as the frames in mux.
  212. // (4) As WebP only supports even offsets, any odd offset will be snapped
  213. // to an even location using: offset &= ~1
  214. // Parameters:
  215. // mux - (in/out) object to which the frame is to be added
  216. // frame - (in) frame data.
  217. // copy_data - (in) value 1 indicates given data WILL be copied to the mux
  218. // object and value 0 indicates data will NOT be copied. If the
  219. // data is not copied, it must exist until a call to
  220. // WebPMuxAssemble() is made.
  221. // Returns:
  222. // WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL
  223. // or if content of 'frame' is invalid.
  224. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  225. // WEBP_MUX_OK - on success.
  226. WEBP_EXTERN WebPMuxError WebPMuxPushFrame(
  227. WebPMux* mux, const WebPMuxFrameInfo* frame, int copy_data);
  228. // Gets the nth frame from the mux object.
  229. // The content of 'frame->bitstream' is allocated using WebPMalloc(), and NOT
  230. // owned by the 'mux' object. It MUST be deallocated by the caller by calling
  231. // WebPDataClear().
  232. // nth=0 has a special meaning - last position.
  233. // Parameters:
  234. // mux - (in) object from which the info is to be fetched
  235. // nth - (in) index of the frame in the mux object
  236. // frame - (out) data of the returned frame
  237. // Returns:
  238. // WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL.
  239. // WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object.
  240. // WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid.
  241. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  242. // WEBP_MUX_OK - on success.
  243. WEBP_EXTERN WebPMuxError WebPMuxGetFrame(
  244. const WebPMux* mux, uint32_t nth, WebPMuxFrameInfo* frame);
  245. // Deletes a frame from the mux object.
  246. // nth=0 has a special meaning - last position.
  247. // Parameters:
  248. // mux - (in/out) object from which a frame is to be deleted
  249. // nth - (in) The position from which the frame is to be deleted
  250. // Returns:
  251. // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL.
  252. // WEBP_MUX_NOT_FOUND - If there are less than nth frames in the mux object
  253. // before deletion.
  254. // WEBP_MUX_OK - on success.
  255. WEBP_EXTERN WebPMuxError WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth);
  256. //------------------------------------------------------------------------------
  257. // Animation.
  258. // Animation parameters.
  259. struct WebPMuxAnimParams {
  260. uint32_t bgcolor; // Background color of the canvas stored (in MSB order) as:
  261. // Bits 00 to 07: Alpha.
  262. // Bits 08 to 15: Red.
  263. // Bits 16 to 23: Green.
  264. // Bits 24 to 31: Blue.
  265. int loop_count; // Number of times to repeat the animation [0 = infinite].
  266. };
  267. // Sets the animation parameters in the mux object. Any existing ANIM chunks
  268. // will be removed.
  269. // Parameters:
  270. // mux - (in/out) object in which ANIM chunk is to be set/added
  271. // params - (in) animation parameters.
  272. // Returns:
  273. // WEBP_MUX_INVALID_ARGUMENT - if mux or params is NULL.
  274. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  275. // WEBP_MUX_OK - on success.
  276. WEBP_EXTERN WebPMuxError WebPMuxSetAnimationParams(
  277. WebPMux* mux, const WebPMuxAnimParams* params);
  278. // Gets the animation parameters from the mux object.
  279. // Parameters:
  280. // mux - (in) object from which the animation parameters to be fetched
  281. // params - (out) animation parameters extracted from the ANIM chunk
  282. // Returns:
  283. // WEBP_MUX_INVALID_ARGUMENT - if mux or params is NULL.
  284. // WEBP_MUX_NOT_FOUND - if ANIM chunk is not present in mux object.
  285. // WEBP_MUX_OK - on success.
  286. WEBP_EXTERN WebPMuxError WebPMuxGetAnimationParams(
  287. const WebPMux* mux, WebPMuxAnimParams* params);
  288. //------------------------------------------------------------------------------
  289. // Misc Utilities.
  290. // Sets the canvas size for the mux object. The width and height can be
  291. // specified explicitly or left as zero (0, 0).
  292. // * When width and height are specified explicitly, then this frame bound is
  293. // enforced during subsequent calls to WebPMuxAssemble() and an error is
  294. // reported if any animated frame does not completely fit within the canvas.
  295. // * When unspecified (0, 0), the constructed canvas will get the frame bounds
  296. // from the bounding-box over all frames after calling WebPMuxAssemble().
  297. // Parameters:
  298. // mux - (in) object to which the canvas size is to be set
  299. // width - (in) canvas width
  300. // height - (in) canvas height
  301. // Returns:
  302. // WEBP_MUX_INVALID_ARGUMENT - if mux is NULL; or
  303. // width or height are invalid or out of bounds
  304. // WEBP_MUX_OK - on success.
  305. WEBP_EXTERN WebPMuxError WebPMuxSetCanvasSize(WebPMux* mux,
  306. int width, int height);
  307. // Gets the canvas size from the mux object.
  308. // Note: This method assumes that the VP8X chunk, if present, is up-to-date.
  309. // That is, the mux object hasn't been modified since the last call to
  310. // WebPMuxAssemble() or WebPMuxCreate().
  311. // Parameters:
  312. // mux - (in) object from which the canvas size is to be fetched
  313. // width - (out) canvas width
  314. // height - (out) canvas height
  315. // Returns:
  316. // WEBP_MUX_INVALID_ARGUMENT - if mux, width or height is NULL.
  317. // WEBP_MUX_BAD_DATA - if VP8X/VP8/VP8L chunk or canvas size is invalid.
  318. // WEBP_MUX_OK - on success.
  319. WEBP_EXTERN WebPMuxError WebPMuxGetCanvasSize(const WebPMux* mux,
  320. int* width, int* height);
  321. // Gets the feature flags from the mux object.
  322. // Note: This method assumes that the VP8X chunk, if present, is up-to-date.
  323. // That is, the mux object hasn't been modified since the last call to
  324. // WebPMuxAssemble() or WebPMuxCreate().
  325. // Parameters:
  326. // mux - (in) object from which the features are to be fetched
  327. // flags - (out) the flags specifying which features are present in the
  328. // mux object. This will be an OR of various flag values.
  329. // Enum 'WebPFeatureFlags' can be used to test individual flag values.
  330. // Returns:
  331. // WEBP_MUX_INVALID_ARGUMENT - if mux or flags is NULL.
  332. // WEBP_MUX_BAD_DATA - if VP8X/VP8/VP8L chunk or canvas size is invalid.
  333. // WEBP_MUX_OK - on success.
  334. WEBP_EXTERN WebPMuxError WebPMuxGetFeatures(const WebPMux* mux,
  335. uint32_t* flags);
  336. // Gets number of chunks with the given 'id' in the mux object.
  337. // Parameters:
  338. // mux - (in) object from which the info is to be fetched
  339. // id - (in) chunk id specifying the type of chunk
  340. // num_elements - (out) number of chunks with the given chunk id
  341. // Returns:
  342. // WEBP_MUX_INVALID_ARGUMENT - if mux, or num_elements is NULL.
  343. // WEBP_MUX_OK - on success.
  344. WEBP_EXTERN WebPMuxError WebPMuxNumChunks(const WebPMux* mux,
  345. WebPChunkId id, int* num_elements);
  346. // Assembles all chunks in WebP RIFF format and returns in 'assembled_data'.
  347. // This function also validates the mux object.
  348. // Note: The content of 'assembled_data' will be ignored and overwritten.
  349. // Also, the content of 'assembled_data' is allocated using WebPMalloc(), and
  350. // NOT owned by the 'mux' object. It MUST be deallocated by the caller by
  351. // calling WebPDataClear(). It's always safe to call WebPDataClear() upon
  352. // return, even in case of error.
  353. // Parameters:
  354. // mux - (in/out) object whose chunks are to be assembled
  355. // assembled_data - (out) assembled WebP data
  356. // Returns:
  357. // WEBP_MUX_BAD_DATA - if mux object is invalid.
  358. // WEBP_MUX_INVALID_ARGUMENT - if mux or assembled_data is NULL.
  359. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  360. // WEBP_MUX_OK - on success.
  361. WEBP_EXTERN WebPMuxError WebPMuxAssemble(WebPMux* mux,
  362. WebPData* assembled_data);
  363. //------------------------------------------------------------------------------
  364. // WebPAnimEncoder API
  365. //
  366. // This API allows encoding (possibly) animated WebP images.
  367. //
  368. // Code Example:
  369. /*
  370. WebPAnimEncoderOptions enc_options;
  371. WebPAnimEncoderOptionsInit(&enc_options);
  372. // Tune 'enc_options' as needed.
  373. WebPAnimEncoder* enc = WebPAnimEncoderNew(width, height, &enc_options);
  374. while(<there are more frames>) {
  375. WebPConfig config;
  376. WebPConfigInit(&config);
  377. // Tune 'config' as needed.
  378. WebPAnimEncoderAdd(enc, frame, timestamp_ms, &config);
  379. }
  380. WebPAnimEncoderAdd(enc, NULL, timestamp_ms, NULL);
  381. WebPAnimEncoderAssemble(enc, webp_data);
  382. WebPAnimEncoderDelete(enc);
  383. // Write the 'webp_data' to a file, or re-mux it further.
  384. */
  385. typedef struct WebPAnimEncoder WebPAnimEncoder; // Main opaque object.
  386. // Forward declarations. Defined in encode.h.
  387. struct WebPPicture;
  388. struct WebPConfig;
  389. // Global options.
  390. struct WebPAnimEncoderOptions {
  391. WebPMuxAnimParams anim_params; // Animation parameters.
  392. int minimize_size; // If true, minimize the output size (slow). Implicitly
  393. // disables key-frame insertion.
  394. int kmin;
  395. int kmax; // Minimum and maximum distance between consecutive key
  396. // frames in the output. The library may insert some key
  397. // frames as needed to satisfy this criteria.
  398. // Note that these conditions should hold: kmax > kmin
  399. // and kmin >= kmax / 2 + 1. Also, if kmax <= 0, then
  400. // key-frame insertion is disabled; and if kmax == 1,
  401. // then all frames will be key-frames (kmin value does
  402. // not matter for these special cases).
  403. int allow_mixed; // If true, use mixed compression mode; may choose
  404. // either lossy and lossless for each frame.
  405. int verbose; // If true, print info and warning messages to stderr.
  406. uint32_t padding[4]; // Padding for later use.
  407. };
  408. // Internal, version-checked, entry point.
  409. WEBP_EXTERN int WebPAnimEncoderOptionsInitInternal(
  410. WebPAnimEncoderOptions*, int);
  411. // Should always be called, to initialize a fresh WebPAnimEncoderOptions
  412. // structure before modification. Returns false in case of version mismatch.
  413. // WebPAnimEncoderOptionsInit() must have succeeded before using the
  414. // 'enc_options' object.
  415. WEBP_NODISCARD static WEBP_INLINE int WebPAnimEncoderOptionsInit(
  416. WebPAnimEncoderOptions* enc_options) {
  417. return WebPAnimEncoderOptionsInitInternal(enc_options, WEBP_MUX_ABI_VERSION);
  418. }
  419. // Internal, version-checked, entry point.
  420. WEBP_EXTERN WebPAnimEncoder* WebPAnimEncoderNewInternal(
  421. int, int, const WebPAnimEncoderOptions*, int);
  422. // Creates and initializes a WebPAnimEncoder object.
  423. // Parameters:
  424. // width/height - (in) canvas width and height of the animation.
  425. // enc_options - (in) encoding options; can be passed NULL to pick
  426. // reasonable defaults.
  427. // Returns:
  428. // A pointer to the newly created WebPAnimEncoder object.
  429. // Or NULL in case of memory error.
  430. static WEBP_INLINE WebPAnimEncoder* WebPAnimEncoderNew(
  431. int width, int height, const WebPAnimEncoderOptions* enc_options) {
  432. return WebPAnimEncoderNewInternal(width, height, enc_options,
  433. WEBP_MUX_ABI_VERSION);
  434. }
  435. // Optimize the given frame for WebP, encode it and add it to the
  436. // WebPAnimEncoder object.
  437. // The last call to 'WebPAnimEncoderAdd' should be with frame = NULL, which
  438. // indicates that no more frames are to be added. This call is also used to
  439. // determine the duration of the last frame.
  440. // Parameters:
  441. // enc - (in/out) object to which the frame is to be added.
  442. // frame - (in/out) frame data in ARGB or YUV(A) format. If it is in YUV(A)
  443. // format, it will be converted to ARGB, which incurs a small loss.
  444. // timestamp_ms - (in) timestamp of this frame in milliseconds.
  445. // Duration of a frame would be calculated as
  446. // "timestamp of next frame - timestamp of this frame".
  447. // Hence, timestamps should be in non-decreasing order.
  448. // config - (in) encoding options; can be passed NULL to pick
  449. // reasonable defaults.
  450. // Returns:
  451. // On error, returns false and frame->error_code is set appropriately.
  452. // Otherwise, returns true.
  453. WEBP_NODISCARD WEBP_EXTERN int WebPAnimEncoderAdd(
  454. WebPAnimEncoder* enc, struct WebPPicture* frame, int timestamp_ms,
  455. const struct WebPConfig* config);
  456. // Assemble all frames added so far into a WebP bitstream.
  457. // This call should be preceded by a call to 'WebPAnimEncoderAdd' with
  458. // frame = NULL; if not, the duration of the last frame will be internally
  459. // estimated.
  460. // Parameters:
  461. // enc - (in/out) object from which the frames are to be assembled.
  462. // webp_data - (out) generated WebP bitstream.
  463. // Returns:
  464. // True on success.
  465. WEBP_NODISCARD WEBP_EXTERN int WebPAnimEncoderAssemble(WebPAnimEncoder* enc,
  466. WebPData* webp_data);
  467. // Get error string corresponding to the most recent call using 'enc'. The
  468. // returned string is owned by 'enc' and is valid only until the next call to
  469. // WebPAnimEncoderAdd() or WebPAnimEncoderAssemble() or WebPAnimEncoderDelete().
  470. // Parameters:
  471. // enc - (in/out) object from which the error string is to be fetched.
  472. // Returns:
  473. // NULL if 'enc' is NULL. Otherwise, returns the error string if the last call
  474. // to 'enc' had an error, or an empty string if the last call was a success.
  475. WEBP_EXTERN const char* WebPAnimEncoderGetError(WebPAnimEncoder* enc);
  476. // Deletes the WebPAnimEncoder object.
  477. // Parameters:
  478. // enc - (in/out) object to be deleted
  479. WEBP_EXTERN void WebPAnimEncoderDelete(WebPAnimEncoder* enc);
  480. //------------------------------------------------------------------------------
  481. // Non-image chunks.
  482. // Note: Only non-image related chunks should be managed through chunk APIs.
  483. // (Image related chunks are: "ANMF", "VP8 ", "VP8L" and "ALPH").
  484. // Adds a chunk with id 'fourcc' and data 'chunk_data' in the enc object.
  485. // Any existing chunk(s) with the same id will be removed.
  486. // Parameters:
  487. // enc - (in/out) object to which the chunk is to be added
  488. // fourcc - (in) a character array containing the fourcc of the given chunk;
  489. // e.g., "ICCP", "XMP ", "EXIF", etc.
  490. // chunk_data - (in) the chunk data to be added
  491. // copy_data - (in) value 1 indicates given data WILL be copied to the enc
  492. // object and value 0 indicates data will NOT be copied. If the
  493. // data is not copied, it must exist until a call to
  494. // WebPAnimEncoderAssemble() is made.
  495. // Returns:
  496. // WEBP_MUX_INVALID_ARGUMENT - if enc, fourcc or chunk_data is NULL.
  497. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
  498. // WEBP_MUX_OK - on success.
  499. WEBP_EXTERN WebPMuxError WebPAnimEncoderSetChunk(
  500. WebPAnimEncoder* enc, const char fourcc[4], const WebPData* chunk_data,
  501. int copy_data);
  502. // Gets a reference to the data of the chunk with id 'fourcc' in the enc object.
  503. // The caller should NOT free the returned data.
  504. // Parameters:
  505. // enc - (in) object from which the chunk data is to be fetched
  506. // fourcc - (in) a character array containing the fourcc of the chunk;
  507. // e.g., "ICCP", "XMP ", "EXIF", etc.
  508. // chunk_data - (out) returned chunk data
  509. // Returns:
  510. // WEBP_MUX_INVALID_ARGUMENT - if enc, fourcc or chunk_data is NULL.
  511. // WEBP_MUX_NOT_FOUND - If enc does not contain a chunk with the given id.
  512. // WEBP_MUX_OK - on success.
  513. WEBP_EXTERN WebPMuxError WebPAnimEncoderGetChunk(
  514. const WebPAnimEncoder* enc, const char fourcc[4], WebPData* chunk_data);
  515. // Deletes the chunk with the given 'fourcc' from the enc object.
  516. // Parameters:
  517. // enc - (in/out) object from which the chunk is to be deleted
  518. // fourcc - (in) a character array containing the fourcc of the chunk;
  519. // e.g., "ICCP", "XMP ", "EXIF", etc.
  520. // Returns:
  521. // WEBP_MUX_INVALID_ARGUMENT - if enc or fourcc is NULL.
  522. // WEBP_MUX_NOT_FOUND - If enc does not contain a chunk with the given fourcc.
  523. // WEBP_MUX_OK - on success.
  524. WEBP_EXTERN WebPMuxError WebPAnimEncoderDeleteChunk(
  525. WebPAnimEncoder* enc, const char fourcc[4]);
  526. //------------------------------------------------------------------------------
  527. #ifdef __cplusplus
  528. } // extern "C"
  529. #endif
  530. #endif // WEBP_WEBP_MUX_H_