muxinternal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 objects and utils for mux.
  11. //
  12. // Authors: Urvang (urvang@google.com)
  13. // Vikas (vikasa@google.com)
  14. #include <assert.h>
  15. #include "./muxi.h"
  16. #include "../utils/utils.h"
  17. #define UNDEFINED_CHUNK_SIZE ((uint32_t)(-1))
  18. const ChunkInfo kChunks[] = {
  19. { MKFOURCC('V', 'P', '8', 'X'), WEBP_CHUNK_VP8X, VP8X_CHUNK_SIZE },
  20. { MKFOURCC('I', 'C', 'C', 'P'), WEBP_CHUNK_ICCP, UNDEFINED_CHUNK_SIZE },
  21. { MKFOURCC('A', 'N', 'I', 'M'), WEBP_CHUNK_ANIM, ANIM_CHUNK_SIZE },
  22. { MKFOURCC('A', 'N', 'M', 'F'), WEBP_CHUNK_ANMF, ANMF_CHUNK_SIZE },
  23. { MKFOURCC('A', 'L', 'P', 'H'), WEBP_CHUNK_ALPHA, UNDEFINED_CHUNK_SIZE },
  24. { MKFOURCC('V', 'P', '8', ' '), WEBP_CHUNK_IMAGE, UNDEFINED_CHUNK_SIZE },
  25. { MKFOURCC('V', 'P', '8', 'L'), WEBP_CHUNK_IMAGE, UNDEFINED_CHUNK_SIZE },
  26. { MKFOURCC('E', 'X', 'I', 'F'), WEBP_CHUNK_EXIF, UNDEFINED_CHUNK_SIZE },
  27. { MKFOURCC('X', 'M', 'P', ' '), WEBP_CHUNK_XMP, UNDEFINED_CHUNK_SIZE },
  28. { NIL_TAG, WEBP_CHUNK_UNKNOWN, UNDEFINED_CHUNK_SIZE },
  29. { NIL_TAG, WEBP_CHUNK_NIL, UNDEFINED_CHUNK_SIZE }
  30. };
  31. //------------------------------------------------------------------------------
  32. int WebPGetMuxVersion(void) {
  33. return (MUX_MAJ_VERSION << 16) | (MUX_MIN_VERSION << 8) | MUX_REV_VERSION;
  34. }
  35. //------------------------------------------------------------------------------
  36. // Life of a chunk object.
  37. void ChunkInit(WebPChunk* const chunk) {
  38. assert(chunk);
  39. memset(chunk, 0, sizeof(*chunk));
  40. chunk->tag_ = NIL_TAG;
  41. }
  42. WebPChunk* ChunkRelease(WebPChunk* const chunk) {
  43. WebPChunk* next;
  44. if (chunk == NULL) return NULL;
  45. if (chunk->owner_) {
  46. WebPDataClear(&chunk->data_);
  47. }
  48. next = chunk->next_;
  49. ChunkInit(chunk);
  50. return next;
  51. }
  52. //------------------------------------------------------------------------------
  53. // Chunk misc methods.
  54. CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag) {
  55. int i;
  56. for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
  57. if (tag == kChunks[i].tag) return (CHUNK_INDEX)i;
  58. }
  59. return IDX_UNKNOWN;
  60. }
  61. WebPChunkId ChunkGetIdFromTag(uint32_t tag) {
  62. int i;
  63. for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
  64. if (tag == kChunks[i].tag) return kChunks[i].id;
  65. }
  66. return WEBP_CHUNK_UNKNOWN;
  67. }
  68. uint32_t ChunkGetTagFromFourCC(const char fourcc[4]) {
  69. return MKFOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
  70. }
  71. CHUNK_INDEX ChunkGetIndexFromFourCC(const char fourcc[4]) {
  72. const uint32_t tag = ChunkGetTagFromFourCC(fourcc);
  73. return ChunkGetIndexFromTag(tag);
  74. }
  75. //------------------------------------------------------------------------------
  76. // Chunk search methods.
  77. // Returns next chunk in the chunk list with the given tag.
  78. static WebPChunk* ChunkSearchNextInList(WebPChunk* chunk, uint32_t tag) {
  79. while (chunk != NULL && chunk->tag_ != tag) {
  80. chunk = chunk->next_;
  81. }
  82. return chunk;
  83. }
  84. WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag) {
  85. uint32_t iter = nth;
  86. first = ChunkSearchNextInList(first, tag);
  87. if (first == NULL) return NULL;
  88. while (--iter != 0) {
  89. WebPChunk* next_chunk = ChunkSearchNextInList(first->next_, tag);
  90. if (next_chunk == NULL) break;
  91. first = next_chunk;
  92. }
  93. return ((nth > 0) && (iter > 0)) ? NULL : first;
  94. }
  95. // Outputs a pointer to 'prev_chunk->next_',
  96. // where 'prev_chunk' is the pointer to the chunk at position (nth - 1).
  97. // Returns true if nth chunk was found.
  98. static int ChunkSearchListToSet(WebPChunk** chunk_list, uint32_t nth,
  99. WebPChunk*** const location) {
  100. uint32_t count = 0;
  101. assert(chunk_list != NULL);
  102. *location = chunk_list;
  103. while (*chunk_list != NULL) {
  104. WebPChunk* const cur_chunk = *chunk_list;
  105. ++count;
  106. if (count == nth) return 1; // Found.
  107. chunk_list = &cur_chunk->next_;
  108. *location = chunk_list;
  109. }
  110. // *chunk_list is ok to be NULL if adding at last location.
  111. return (nth == 0 || (count == nth - 1)) ? 1 : 0;
  112. }
  113. //------------------------------------------------------------------------------
  114. // Chunk writer methods.
  115. WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
  116. int copy_data, uint32_t tag) {
  117. // For internally allocated chunks, always copy data & make it owner of data.
  118. if (tag == kChunks[IDX_VP8X].tag || tag == kChunks[IDX_ANIM].tag) {
  119. copy_data = 1;
  120. }
  121. ChunkRelease(chunk);
  122. if (data != NULL) {
  123. if (copy_data) { // Copy data.
  124. if (!WebPDataCopy(data, &chunk->data_)) return WEBP_MUX_MEMORY_ERROR;
  125. chunk->owner_ = 1; // Chunk is owner of data.
  126. } else { // Don't copy data.
  127. chunk->data_ = *data;
  128. }
  129. }
  130. chunk->tag_ = tag;
  131. return WEBP_MUX_OK;
  132. }
  133. WebPMuxError ChunkSetNth(WebPChunk* chunk, WebPChunk** chunk_list,
  134. uint32_t nth) {
  135. WebPChunk* new_chunk;
  136. if (!ChunkSearchListToSet(chunk_list, nth, &chunk_list)) {
  137. return WEBP_MUX_NOT_FOUND;
  138. }
  139. new_chunk = (WebPChunk*)WebPSafeMalloc(1ULL, sizeof(*new_chunk));
  140. if (new_chunk == NULL) return WEBP_MUX_MEMORY_ERROR;
  141. *new_chunk = *chunk;
  142. chunk->owner_ = 0;
  143. new_chunk->next_ = *chunk_list;
  144. *chunk_list = new_chunk;
  145. return WEBP_MUX_OK;
  146. }
  147. //------------------------------------------------------------------------------
  148. // Chunk deletion method(s).
  149. WebPChunk* ChunkDelete(WebPChunk* const chunk) {
  150. WebPChunk* const next = ChunkRelease(chunk);
  151. WebPSafeFree(chunk);
  152. return next;
  153. }
  154. void ChunkListDelete(WebPChunk** const chunk_list) {
  155. while (*chunk_list != NULL) {
  156. *chunk_list = ChunkDelete(*chunk_list);
  157. }
  158. }
  159. //------------------------------------------------------------------------------
  160. // Chunk serialization methods.
  161. static uint8_t* ChunkEmit(const WebPChunk* const chunk, uint8_t* dst) {
  162. const size_t chunk_size = chunk->data_.size;
  163. assert(chunk);
  164. assert(chunk->tag_ != NIL_TAG);
  165. PutLE32(dst + 0, chunk->tag_);
  166. PutLE32(dst + TAG_SIZE, (uint32_t)chunk_size);
  167. assert(chunk_size == (uint32_t)chunk_size);
  168. memcpy(dst + CHUNK_HEADER_SIZE, chunk->data_.bytes, chunk_size);
  169. if (chunk_size & 1)
  170. dst[CHUNK_HEADER_SIZE + chunk_size] = 0; // Add padding.
  171. return dst + ChunkDiskSize(chunk);
  172. }
  173. uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst) {
  174. while (chunk_list != NULL) {
  175. dst = ChunkEmit(chunk_list, dst);
  176. chunk_list = chunk_list->next_;
  177. }
  178. return dst;
  179. }
  180. size_t ChunkListDiskSize(const WebPChunk* chunk_list) {
  181. size_t size = 0;
  182. while (chunk_list != NULL) {
  183. size += ChunkDiskSize(chunk_list);
  184. chunk_list = chunk_list->next_;
  185. }
  186. return size;
  187. }
  188. //------------------------------------------------------------------------------
  189. // Life of a MuxImage object.
  190. void MuxImageInit(WebPMuxImage* const wpi) {
  191. assert(wpi);
  192. memset(wpi, 0, sizeof(*wpi));
  193. }
  194. WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) {
  195. WebPMuxImage* next;
  196. if (wpi == NULL) return NULL;
  197. ChunkDelete(wpi->header_);
  198. ChunkDelete(wpi->alpha_);
  199. ChunkDelete(wpi->img_);
  200. ChunkListDelete(&wpi->unknown_);
  201. next = wpi->next_;
  202. MuxImageInit(wpi);
  203. return next;
  204. }
  205. //------------------------------------------------------------------------------
  206. // MuxImage search methods.
  207. // Get a reference to appropriate chunk list within an image given chunk tag.
  208. static WebPChunk** GetChunkListFromId(const WebPMuxImage* const wpi,
  209. WebPChunkId id) {
  210. assert(wpi != NULL);
  211. switch (id) {
  212. case WEBP_CHUNK_ANMF: return (WebPChunk**)&wpi->header_;
  213. case WEBP_CHUNK_ALPHA: return (WebPChunk**)&wpi->alpha_;
  214. case WEBP_CHUNK_IMAGE: return (WebPChunk**)&wpi->img_;
  215. default: return NULL;
  216. }
  217. }
  218. int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) {
  219. int count = 0;
  220. const WebPMuxImage* current;
  221. for (current = wpi_list; current != NULL; current = current->next_) {
  222. if (id == WEBP_CHUNK_NIL) {
  223. ++count; // Special case: count all images.
  224. } else {
  225. const WebPChunk* const wpi_chunk = *GetChunkListFromId(current, id);
  226. if (wpi_chunk != NULL) {
  227. const WebPChunkId wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag_);
  228. if (wpi_chunk_id == id) ++count; // Count images with a matching 'id'.
  229. }
  230. }
  231. }
  232. return count;
  233. }
  234. // Outputs a pointer to 'prev_wpi->next_',
  235. // where 'prev_wpi' is the pointer to the image at position (nth - 1).
  236. // Returns true if nth image was found.
  237. static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth,
  238. WebPMuxImage*** const location) {
  239. uint32_t count = 0;
  240. assert(wpi_list);
  241. *location = wpi_list;
  242. if (nth == 0) {
  243. nth = MuxImageCount(*wpi_list, WEBP_CHUNK_NIL);
  244. if (nth == 0) return 0; // Not found.
  245. }
  246. while (*wpi_list != NULL) {
  247. WebPMuxImage* const cur_wpi = *wpi_list;
  248. ++count;
  249. if (count == nth) return 1; // Found.
  250. wpi_list = &cur_wpi->next_;
  251. *location = wpi_list;
  252. }
  253. return 0; // Not found.
  254. }
  255. //------------------------------------------------------------------------------
  256. // MuxImage writer methods.
  257. WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list) {
  258. WebPMuxImage* new_wpi;
  259. while (*wpi_list != NULL) {
  260. WebPMuxImage* const cur_wpi = *wpi_list;
  261. if (cur_wpi->next_ == NULL) break;
  262. wpi_list = &cur_wpi->next_;
  263. }
  264. new_wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*new_wpi));
  265. if (new_wpi == NULL) return WEBP_MUX_MEMORY_ERROR;
  266. *new_wpi = *wpi;
  267. new_wpi->next_ = NULL;
  268. if (*wpi_list != NULL) {
  269. (*wpi_list)->next_ = new_wpi;
  270. } else {
  271. *wpi_list = new_wpi;
  272. }
  273. return WEBP_MUX_OK;
  274. }
  275. //------------------------------------------------------------------------------
  276. // MuxImage deletion methods.
  277. WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi) {
  278. // Delete the components of wpi. If wpi is NULL this is a noop.
  279. WebPMuxImage* const next = MuxImageRelease(wpi);
  280. WebPSafeFree(wpi);
  281. return next;
  282. }
  283. WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth) {
  284. assert(wpi_list);
  285. if (!SearchImageToGetOrDelete(wpi_list, nth, &wpi_list)) {
  286. return WEBP_MUX_NOT_FOUND;
  287. }
  288. *wpi_list = MuxImageDelete(*wpi_list);
  289. return WEBP_MUX_OK;
  290. }
  291. //------------------------------------------------------------------------------
  292. // MuxImage reader methods.
  293. WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
  294. WebPMuxImage** wpi) {
  295. assert(wpi_list);
  296. assert(wpi);
  297. if (!SearchImageToGetOrDelete((WebPMuxImage**)wpi_list, nth,
  298. (WebPMuxImage***)&wpi_list)) {
  299. return WEBP_MUX_NOT_FOUND;
  300. }
  301. *wpi = (WebPMuxImage*)*wpi_list;
  302. return WEBP_MUX_OK;
  303. }
  304. //------------------------------------------------------------------------------
  305. // MuxImage serialization methods.
  306. // Size of an image.
  307. size_t MuxImageDiskSize(const WebPMuxImage* const wpi) {
  308. size_t size = 0;
  309. if (wpi->header_ != NULL) size += ChunkDiskSize(wpi->header_);
  310. if (wpi->alpha_ != NULL) size += ChunkDiskSize(wpi->alpha_);
  311. if (wpi->img_ != NULL) size += ChunkDiskSize(wpi->img_);
  312. if (wpi->unknown_ != NULL) size += ChunkListDiskSize(wpi->unknown_);
  313. return size;
  314. }
  315. // Special case as ANMF chunk encapsulates other image chunks.
  316. static uint8_t* ChunkEmitSpecial(const WebPChunk* const header,
  317. size_t total_size, uint8_t* dst) {
  318. const size_t header_size = header->data_.size;
  319. const size_t offset_to_next = total_size - CHUNK_HEADER_SIZE;
  320. assert(header->tag_ == kChunks[IDX_ANMF].tag);
  321. PutLE32(dst + 0, header->tag_);
  322. PutLE32(dst + TAG_SIZE, (uint32_t)offset_to_next);
  323. assert(header_size == (uint32_t)header_size);
  324. memcpy(dst + CHUNK_HEADER_SIZE, header->data_.bytes, header_size);
  325. if (header_size & 1) {
  326. dst[CHUNK_HEADER_SIZE + header_size] = 0; // Add padding.
  327. }
  328. return dst + ChunkDiskSize(header);
  329. }
  330. uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) {
  331. // Ordering of chunks to be emitted is strictly as follows:
  332. // 1. ANMF chunk (if present).
  333. // 2. ALPH chunk (if present).
  334. // 3. VP8/VP8L chunk.
  335. assert(wpi);
  336. if (wpi->header_ != NULL) {
  337. dst = ChunkEmitSpecial(wpi->header_, MuxImageDiskSize(wpi), dst);
  338. }
  339. if (wpi->alpha_ != NULL) dst = ChunkEmit(wpi->alpha_, dst);
  340. if (wpi->img_ != NULL) dst = ChunkEmit(wpi->img_, dst);
  341. if (wpi->unknown_ != NULL) dst = ChunkListEmit(wpi->unknown_, dst);
  342. return dst;
  343. }
  344. //------------------------------------------------------------------------------
  345. // Helper methods for mux.
  346. int MuxHasAlpha(const WebPMuxImage* images) {
  347. while (images != NULL) {
  348. if (images->has_alpha_) return 1;
  349. images = images->next_;
  350. }
  351. return 0;
  352. }
  353. uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size) {
  354. PutLE32(data + 0, MKFOURCC('R', 'I', 'F', 'F'));
  355. PutLE32(data + TAG_SIZE, (uint32_t)size - CHUNK_HEADER_SIZE);
  356. assert(size == (uint32_t)size);
  357. PutLE32(data + TAG_SIZE + CHUNK_SIZE_BYTES, MKFOURCC('W', 'E', 'B', 'P'));
  358. return data + RIFF_HEADER_SIZE;
  359. }
  360. WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id) {
  361. assert(mux != NULL);
  362. switch (id) {
  363. case WEBP_CHUNK_VP8X: return (WebPChunk**)&mux->vp8x_;
  364. case WEBP_CHUNK_ICCP: return (WebPChunk**)&mux->iccp_;
  365. case WEBP_CHUNK_ANIM: return (WebPChunk**)&mux->anim_;
  366. case WEBP_CHUNK_EXIF: return (WebPChunk**)&mux->exif_;
  367. case WEBP_CHUNK_XMP: return (WebPChunk**)&mux->xmp_;
  368. default: return (WebPChunk**)&mux->unknown_;
  369. }
  370. }
  371. static int IsNotCompatible(int feature, int num_items) {
  372. return (feature != 0) != (num_items > 0);
  373. }
  374. #define NO_FLAG ((WebPFeatureFlags)0)
  375. // Test basic constraints:
  376. // retrieval, maximum number of chunks by index (use -1 to skip)
  377. // and feature incompatibility (use NO_FLAG to skip).
  378. // On success returns WEBP_MUX_OK and stores the chunk count in *num.
  379. static WebPMuxError ValidateChunk(const WebPMux* const mux, CHUNK_INDEX idx,
  380. WebPFeatureFlags feature,
  381. uint32_t vp8x_flags,
  382. int max, int* num) {
  383. const WebPMuxError err =
  384. WebPMuxNumChunks(mux, kChunks[idx].id, num);
  385. if (err != WEBP_MUX_OK) return err;
  386. if (max > -1 && *num > max) return WEBP_MUX_INVALID_ARGUMENT;
  387. if (feature != NO_FLAG && IsNotCompatible(vp8x_flags & feature, *num)) {
  388. return WEBP_MUX_INVALID_ARGUMENT;
  389. }
  390. return WEBP_MUX_OK;
  391. }
  392. WebPMuxError MuxValidate(const WebPMux* const mux) {
  393. int num_iccp;
  394. int num_exif;
  395. int num_xmp;
  396. int num_anim;
  397. int num_frames;
  398. int num_vp8x;
  399. int num_images;
  400. int num_alpha;
  401. uint32_t flags;
  402. WebPMuxError err;
  403. // Verify mux is not NULL.
  404. if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
  405. // Verify mux has at least one image.
  406. if (mux->images_ == NULL) return WEBP_MUX_INVALID_ARGUMENT;
  407. err = WebPMuxGetFeatures(mux, &flags);
  408. if (err != WEBP_MUX_OK) return err;
  409. // At most one color profile chunk.
  410. err = ValidateChunk(mux, IDX_ICCP, ICCP_FLAG, flags, 1, &num_iccp);
  411. if (err != WEBP_MUX_OK) return err;
  412. // At most one EXIF metadata.
  413. err = ValidateChunk(mux, IDX_EXIF, EXIF_FLAG, flags, 1, &num_exif);
  414. if (err != WEBP_MUX_OK) return err;
  415. // At most one XMP metadata.
  416. err = ValidateChunk(mux, IDX_XMP, XMP_FLAG, flags, 1, &num_xmp);
  417. if (err != WEBP_MUX_OK) return err;
  418. // Animation: ANIMATION_FLAG, ANIM chunk and ANMF chunk(s) are consistent.
  419. // At most one ANIM chunk.
  420. err = ValidateChunk(mux, IDX_ANIM, NO_FLAG, flags, 1, &num_anim);
  421. if (err != WEBP_MUX_OK) return err;
  422. err = ValidateChunk(mux, IDX_ANMF, NO_FLAG, flags, -1, &num_frames);
  423. if (err != WEBP_MUX_OK) return err;
  424. {
  425. const int has_animation = !!(flags & ANIMATION_FLAG);
  426. if (has_animation && (num_anim == 0 || num_frames == 0)) {
  427. return WEBP_MUX_INVALID_ARGUMENT;
  428. }
  429. if (!has_animation && (num_anim == 1 || num_frames > 0)) {
  430. return WEBP_MUX_INVALID_ARGUMENT;
  431. }
  432. }
  433. // Verify either VP8X chunk is present OR there is only one elem in
  434. // mux->images_.
  435. err = ValidateChunk(mux, IDX_VP8X, NO_FLAG, flags, 1, &num_vp8x);
  436. if (err != WEBP_MUX_OK) return err;
  437. err = ValidateChunk(mux, IDX_VP8, NO_FLAG, flags, -1, &num_images);
  438. if (err != WEBP_MUX_OK) return err;
  439. if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT;
  440. // ALPHA_FLAG & alpha chunk(s) are consistent.
  441. if (MuxHasAlpha(mux->images_)) {
  442. if (num_vp8x > 0) {
  443. // VP8X chunk is present, so it should contain ALPHA_FLAG.
  444. if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT;
  445. } else {
  446. // VP8X chunk is not present, so ALPH chunks should NOT be present either.
  447. err = WebPMuxNumChunks(mux, WEBP_CHUNK_ALPHA, &num_alpha);
  448. if (err != WEBP_MUX_OK) return err;
  449. if (num_alpha > 0) return WEBP_MUX_INVALID_ARGUMENT;
  450. }
  451. } else { // Mux doesn't need alpha. So, ALPHA_FLAG should NOT be present.
  452. if (flags & ALPHA_FLAG) return WEBP_MUX_INVALID_ARGUMENT;
  453. }
  454. return WEBP_MUX_OK;
  455. }
  456. #undef NO_FLAG
  457. //------------------------------------------------------------------------------