muxinternal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. //------------------------------------------------------------------------------
  96. // Chunk writer methods.
  97. WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
  98. int copy_data, uint32_t tag) {
  99. // For internally allocated chunks, always copy data & make it owner of data.
  100. if (tag == kChunks[IDX_VP8X].tag || tag == kChunks[IDX_ANIM].tag) {
  101. copy_data = 1;
  102. }
  103. ChunkRelease(chunk);
  104. if (data != NULL) {
  105. if (copy_data) { // Copy data.
  106. if (!WebPDataCopy(data, &chunk->data_)) return WEBP_MUX_MEMORY_ERROR;
  107. chunk->owner_ = 1; // Chunk is owner of data.
  108. } else { // Don't copy data.
  109. chunk->data_ = *data;
  110. }
  111. }
  112. chunk->tag_ = tag;
  113. return WEBP_MUX_OK;
  114. }
  115. WebPMuxError ChunkSetHead(WebPChunk* const chunk,
  116. WebPChunk** const chunk_list) {
  117. WebPChunk* new_chunk;
  118. assert(chunk_list != NULL);
  119. if (*chunk_list != NULL) {
  120. return WEBP_MUX_NOT_FOUND;
  121. }
  122. new_chunk = (WebPChunk*)WebPSafeMalloc(1ULL, sizeof(*new_chunk));
  123. if (new_chunk == NULL) return WEBP_MUX_MEMORY_ERROR;
  124. *new_chunk = *chunk;
  125. chunk->owner_ = 0;
  126. new_chunk->next_ = NULL;
  127. *chunk_list = new_chunk;
  128. return WEBP_MUX_OK;
  129. }
  130. WebPMuxError ChunkAppend(WebPChunk* const chunk,
  131. WebPChunk*** const chunk_list) {
  132. assert(chunk_list != NULL && *chunk_list != NULL);
  133. if (**chunk_list == NULL) {
  134. ChunkSetHead(chunk, *chunk_list);
  135. } else {
  136. WebPChunk* last_chunk = **chunk_list;
  137. while (last_chunk->next_ != NULL) last_chunk = last_chunk->next_;
  138. ChunkSetHead(chunk, &last_chunk->next_);
  139. *chunk_list = &last_chunk->next_;
  140. }
  141. return WEBP_MUX_OK;
  142. }
  143. //------------------------------------------------------------------------------
  144. // Chunk deletion method(s).
  145. WebPChunk* ChunkDelete(WebPChunk* const chunk) {
  146. WebPChunk* const next = ChunkRelease(chunk);
  147. WebPSafeFree(chunk);
  148. return next;
  149. }
  150. void ChunkListDelete(WebPChunk** const chunk_list) {
  151. while (*chunk_list != NULL) {
  152. *chunk_list = ChunkDelete(*chunk_list);
  153. }
  154. }
  155. //------------------------------------------------------------------------------
  156. // Chunk serialization methods.
  157. static uint8_t* ChunkEmit(const WebPChunk* const chunk, uint8_t* dst) {
  158. const size_t chunk_size = chunk->data_.size;
  159. assert(chunk);
  160. assert(chunk->tag_ != NIL_TAG);
  161. PutLE32(dst + 0, chunk->tag_);
  162. PutLE32(dst + TAG_SIZE, (uint32_t)chunk_size);
  163. assert(chunk_size == (uint32_t)chunk_size);
  164. memcpy(dst + CHUNK_HEADER_SIZE, chunk->data_.bytes, chunk_size);
  165. if (chunk_size & 1)
  166. dst[CHUNK_HEADER_SIZE + chunk_size] = 0; // Add padding.
  167. return dst + ChunkDiskSize(chunk);
  168. }
  169. uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst) {
  170. while (chunk_list != NULL) {
  171. dst = ChunkEmit(chunk_list, dst);
  172. chunk_list = chunk_list->next_;
  173. }
  174. return dst;
  175. }
  176. size_t ChunkListDiskSize(const WebPChunk* chunk_list) {
  177. size_t size = 0;
  178. while (chunk_list != NULL) {
  179. size += ChunkDiskSize(chunk_list);
  180. chunk_list = chunk_list->next_;
  181. }
  182. return size;
  183. }
  184. //------------------------------------------------------------------------------
  185. // Life of a MuxImage object.
  186. void MuxImageInit(WebPMuxImage* const wpi) {
  187. assert(wpi);
  188. memset(wpi, 0, sizeof(*wpi));
  189. }
  190. WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) {
  191. WebPMuxImage* next;
  192. if (wpi == NULL) return NULL;
  193. // There should be at most one chunk of header_, alpha_, img_ but we call
  194. // ChunkListDelete to be safe
  195. ChunkListDelete(&wpi->header_);
  196. ChunkListDelete(&wpi->alpha_);
  197. ChunkListDelete(&wpi->img_);
  198. ChunkListDelete(&wpi->unknown_);
  199. next = wpi->next_;
  200. MuxImageInit(wpi);
  201. return next;
  202. }
  203. //------------------------------------------------------------------------------
  204. // MuxImage search methods.
  205. // Get a reference to appropriate chunk list within an image given chunk tag.
  206. static WebPChunk** GetChunkListFromId(const WebPMuxImage* const wpi,
  207. WebPChunkId id) {
  208. assert(wpi != NULL);
  209. switch (id) {
  210. case WEBP_CHUNK_ANMF: return (WebPChunk**)&wpi->header_;
  211. case WEBP_CHUNK_ALPHA: return (WebPChunk**)&wpi->alpha_;
  212. case WEBP_CHUNK_IMAGE: return (WebPChunk**)&wpi->img_;
  213. default: return NULL;
  214. }
  215. }
  216. int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) {
  217. int count = 0;
  218. const WebPMuxImage* current;
  219. for (current = wpi_list; current != NULL; current = current->next_) {
  220. if (id == WEBP_CHUNK_NIL) {
  221. ++count; // Special case: count all images.
  222. } else {
  223. const WebPChunk* const wpi_chunk = *GetChunkListFromId(current, id);
  224. if (wpi_chunk != NULL) {
  225. const WebPChunkId wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag_);
  226. if (wpi_chunk_id == id) ++count; // Count images with a matching 'id'.
  227. }
  228. }
  229. }
  230. return count;
  231. }
  232. // Outputs a pointer to 'prev_wpi->next_',
  233. // where 'prev_wpi' is the pointer to the image at position (nth - 1).
  234. // Returns true if nth image was found.
  235. static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth,
  236. WebPMuxImage*** const location) {
  237. uint32_t count = 0;
  238. assert(wpi_list);
  239. *location = wpi_list;
  240. if (nth == 0) {
  241. nth = MuxImageCount(*wpi_list, WEBP_CHUNK_NIL);
  242. if (nth == 0) return 0; // Not found.
  243. }
  244. while (*wpi_list != NULL) {
  245. WebPMuxImage* const cur_wpi = *wpi_list;
  246. ++count;
  247. if (count == nth) return 1; // Found.
  248. wpi_list = &cur_wpi->next_;
  249. *location = wpi_list;
  250. }
  251. return 0; // Not found.
  252. }
  253. //------------------------------------------------------------------------------
  254. // MuxImage writer methods.
  255. WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list) {
  256. WebPMuxImage* new_wpi;
  257. while (*wpi_list != NULL) {
  258. WebPMuxImage* const cur_wpi = *wpi_list;
  259. if (cur_wpi->next_ == NULL) break;
  260. wpi_list = &cur_wpi->next_;
  261. }
  262. new_wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*new_wpi));
  263. if (new_wpi == NULL) return WEBP_MUX_MEMORY_ERROR;
  264. *new_wpi = *wpi;
  265. new_wpi->next_ = NULL;
  266. if (*wpi_list != NULL) {
  267. (*wpi_list)->next_ = new_wpi;
  268. } else {
  269. *wpi_list = new_wpi;
  270. }
  271. return WEBP_MUX_OK;
  272. }
  273. //------------------------------------------------------------------------------
  274. // MuxImage deletion methods.
  275. WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi) {
  276. // Delete the components of wpi. If wpi is NULL this is a noop.
  277. WebPMuxImage* const next = MuxImageRelease(wpi);
  278. WebPSafeFree(wpi);
  279. return next;
  280. }
  281. WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth) {
  282. assert(wpi_list);
  283. if (!SearchImageToGetOrDelete(wpi_list, nth, &wpi_list)) {
  284. return WEBP_MUX_NOT_FOUND;
  285. }
  286. *wpi_list = MuxImageDelete(*wpi_list);
  287. return WEBP_MUX_OK;
  288. }
  289. //------------------------------------------------------------------------------
  290. // MuxImage reader methods.
  291. WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
  292. WebPMuxImage** wpi) {
  293. assert(wpi_list);
  294. assert(wpi);
  295. if (!SearchImageToGetOrDelete((WebPMuxImage**)wpi_list, nth,
  296. (WebPMuxImage***)&wpi_list)) {
  297. return WEBP_MUX_NOT_FOUND;
  298. }
  299. *wpi = (WebPMuxImage*)*wpi_list;
  300. return WEBP_MUX_OK;
  301. }
  302. //------------------------------------------------------------------------------
  303. // MuxImage serialization methods.
  304. // Size of an image.
  305. size_t MuxImageDiskSize(const WebPMuxImage* const wpi) {
  306. size_t size = 0;
  307. if (wpi->header_ != NULL) size += ChunkDiskSize(wpi->header_);
  308. if (wpi->alpha_ != NULL) size += ChunkDiskSize(wpi->alpha_);
  309. if (wpi->img_ != NULL) size += ChunkDiskSize(wpi->img_);
  310. if (wpi->unknown_ != NULL) size += ChunkListDiskSize(wpi->unknown_);
  311. return size;
  312. }
  313. // Special case as ANMF chunk encapsulates other image chunks.
  314. static uint8_t* ChunkEmitSpecial(const WebPChunk* const header,
  315. size_t total_size, uint8_t* dst) {
  316. const size_t header_size = header->data_.size;
  317. const size_t offset_to_next = total_size - CHUNK_HEADER_SIZE;
  318. assert(header->tag_ == kChunks[IDX_ANMF].tag);
  319. PutLE32(dst + 0, header->tag_);
  320. PutLE32(dst + TAG_SIZE, (uint32_t)offset_to_next);
  321. assert(header_size == (uint32_t)header_size);
  322. memcpy(dst + CHUNK_HEADER_SIZE, header->data_.bytes, header_size);
  323. if (header_size & 1) {
  324. dst[CHUNK_HEADER_SIZE + header_size] = 0; // Add padding.
  325. }
  326. return dst + ChunkDiskSize(header);
  327. }
  328. uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) {
  329. // Ordering of chunks to be emitted is strictly as follows:
  330. // 1. ANMF chunk (if present).
  331. // 2. ALPH chunk (if present).
  332. // 3. VP8/VP8L chunk.
  333. assert(wpi);
  334. if (wpi->header_ != NULL) {
  335. dst = ChunkEmitSpecial(wpi->header_, MuxImageDiskSize(wpi), dst);
  336. }
  337. if (wpi->alpha_ != NULL) dst = ChunkEmit(wpi->alpha_, dst);
  338. if (wpi->img_ != NULL) dst = ChunkEmit(wpi->img_, dst);
  339. if (wpi->unknown_ != NULL) dst = ChunkListEmit(wpi->unknown_, dst);
  340. return dst;
  341. }
  342. //------------------------------------------------------------------------------
  343. // Helper methods for mux.
  344. int MuxHasAlpha(const WebPMuxImage* images) {
  345. while (images != NULL) {
  346. if (images->has_alpha_) return 1;
  347. images = images->next_;
  348. }
  349. return 0;
  350. }
  351. uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size) {
  352. PutLE32(data + 0, MKFOURCC('R', 'I', 'F', 'F'));
  353. PutLE32(data + TAG_SIZE, (uint32_t)size - CHUNK_HEADER_SIZE);
  354. assert(size == (uint32_t)size);
  355. PutLE32(data + TAG_SIZE + CHUNK_SIZE_BYTES, MKFOURCC('W', 'E', 'B', 'P'));
  356. return data + RIFF_HEADER_SIZE;
  357. }
  358. WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id) {
  359. assert(mux != NULL);
  360. switch (id) {
  361. case WEBP_CHUNK_VP8X: return (WebPChunk**)&mux->vp8x_;
  362. case WEBP_CHUNK_ICCP: return (WebPChunk**)&mux->iccp_;
  363. case WEBP_CHUNK_ANIM: return (WebPChunk**)&mux->anim_;
  364. case WEBP_CHUNK_EXIF: return (WebPChunk**)&mux->exif_;
  365. case WEBP_CHUNK_XMP: return (WebPChunk**)&mux->xmp_;
  366. default: return (WebPChunk**)&mux->unknown_;
  367. }
  368. }
  369. static int IsNotCompatible(int feature, int num_items) {
  370. return (feature != 0) != (num_items > 0);
  371. }
  372. #define NO_FLAG ((WebPFeatureFlags)0)
  373. // Test basic constraints:
  374. // retrieval, maximum number of chunks by index (use -1 to skip)
  375. // and feature incompatibility (use NO_FLAG to skip).
  376. // On success returns WEBP_MUX_OK and stores the chunk count in *num.
  377. static WebPMuxError ValidateChunk(const WebPMux* const mux, CHUNK_INDEX idx,
  378. WebPFeatureFlags feature,
  379. uint32_t vp8x_flags,
  380. int max, int* num) {
  381. const WebPMuxError err =
  382. WebPMuxNumChunks(mux, kChunks[idx].id, num);
  383. if (err != WEBP_MUX_OK) return err;
  384. if (max > -1 && *num > max) return WEBP_MUX_INVALID_ARGUMENT;
  385. if (feature != NO_FLAG && IsNotCompatible(vp8x_flags & feature, *num)) {
  386. return WEBP_MUX_INVALID_ARGUMENT;
  387. }
  388. return WEBP_MUX_OK;
  389. }
  390. WebPMuxError MuxValidate(const WebPMux* const mux) {
  391. int num_iccp;
  392. int num_exif;
  393. int num_xmp;
  394. int num_anim;
  395. int num_frames;
  396. int num_vp8x;
  397. int num_images;
  398. int num_alpha;
  399. uint32_t flags;
  400. WebPMuxError err;
  401. // Verify mux is not NULL.
  402. if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
  403. // Verify mux has at least one image.
  404. if (mux->images_ == NULL) return WEBP_MUX_INVALID_ARGUMENT;
  405. err = WebPMuxGetFeatures(mux, &flags);
  406. if (err != WEBP_MUX_OK) return err;
  407. // At most one color profile chunk.
  408. err = ValidateChunk(mux, IDX_ICCP, ICCP_FLAG, flags, 1, &num_iccp);
  409. if (err != WEBP_MUX_OK) return err;
  410. // At most one EXIF metadata.
  411. err = ValidateChunk(mux, IDX_EXIF, EXIF_FLAG, flags, 1, &num_exif);
  412. if (err != WEBP_MUX_OK) return err;
  413. // At most one XMP metadata.
  414. err = ValidateChunk(mux, IDX_XMP, XMP_FLAG, flags, 1, &num_xmp);
  415. if (err != WEBP_MUX_OK) return err;
  416. // Animation: ANIMATION_FLAG, ANIM chunk and ANMF chunk(s) are consistent.
  417. // At most one ANIM chunk.
  418. err = ValidateChunk(mux, IDX_ANIM, NO_FLAG, flags, 1, &num_anim);
  419. if (err != WEBP_MUX_OK) return err;
  420. err = ValidateChunk(mux, IDX_ANMF, NO_FLAG, flags, -1, &num_frames);
  421. if (err != WEBP_MUX_OK) return err;
  422. {
  423. const int has_animation = !!(flags & ANIMATION_FLAG);
  424. if (has_animation && (num_anim == 0 || num_frames == 0)) {
  425. return WEBP_MUX_INVALID_ARGUMENT;
  426. }
  427. if (!has_animation && (num_anim == 1 || num_frames > 0)) {
  428. return WEBP_MUX_INVALID_ARGUMENT;
  429. }
  430. if (!has_animation) {
  431. const WebPMuxImage* images = mux->images_;
  432. // There can be only one image.
  433. if (images == NULL || images->next_ != NULL) {
  434. return WEBP_MUX_INVALID_ARGUMENT;
  435. }
  436. // Size must match.
  437. if (mux->canvas_width_ > 0) {
  438. if (images->width_ != mux->canvas_width_ ||
  439. images->height_ != mux->canvas_height_) {
  440. return WEBP_MUX_INVALID_ARGUMENT;
  441. }
  442. }
  443. }
  444. }
  445. // Verify either VP8X chunk is present OR there is only one elem in
  446. // mux->images_.
  447. err = ValidateChunk(mux, IDX_VP8X, NO_FLAG, flags, 1, &num_vp8x);
  448. if (err != WEBP_MUX_OK) return err;
  449. err = ValidateChunk(mux, IDX_VP8, NO_FLAG, flags, -1, &num_images);
  450. if (err != WEBP_MUX_OK) return err;
  451. if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT;
  452. // ALPHA_FLAG & alpha chunk(s) are consistent.
  453. // Note: ALPHA_FLAG can be set when there is actually no Alpha data present.
  454. if (MuxHasAlpha(mux->images_)) {
  455. if (num_vp8x > 0) {
  456. // VP8X chunk is present, so it should contain ALPHA_FLAG.
  457. if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT;
  458. } else {
  459. // VP8X chunk is not present, so ALPH chunks should NOT be present either.
  460. err = WebPMuxNumChunks(mux, WEBP_CHUNK_ALPHA, &num_alpha);
  461. if (err != WEBP_MUX_OK) return err;
  462. if (num_alpha > 0) return WEBP_MUX_INVALID_ARGUMENT;
  463. }
  464. }
  465. return WEBP_MUX_OK;
  466. }
  467. #undef NO_FLAG
  468. //------------------------------------------------------------------------------