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