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 // Set and delete APIs 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 //------------------------------------------------------------------------------
20 // Life of a mux object.
21
MuxInit(WebPMux * const mux)22 static void MuxInit(WebPMux* const mux) {
23 assert(mux != NULL);
24 memset(mux, 0, sizeof(*mux));
25 mux->canvas_width_ = 0; // just to be explicit
26 mux->canvas_height_ = 0;
27 }
28
WebPNewInternal(int version)29 WebPMux* WebPNewInternal(int version) {
30 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_MUX_ABI_VERSION)) {
31 return NULL;
32 } else {
33 WebPMux* const mux = (WebPMux*)WebPSafeMalloc(1ULL, sizeof(WebPMux));
34 if (mux != NULL) MuxInit(mux);
35 return mux;
36 }
37 }
38
39 // Delete all images in 'wpi_list'.
DeleteAllImages(WebPMuxImage ** const wpi_list)40 static void DeleteAllImages(WebPMuxImage** const wpi_list) {
41 while (*wpi_list != NULL) {
42 *wpi_list = MuxImageDelete(*wpi_list);
43 }
44 }
45
MuxRelease(WebPMux * const mux)46 static void MuxRelease(WebPMux* const mux) {
47 assert(mux != NULL);
48 DeleteAllImages(&mux->images_);
49 ChunkListDelete(&mux->vp8x_);
50 ChunkListDelete(&mux->iccp_);
51 ChunkListDelete(&mux->anim_);
52 ChunkListDelete(&mux->exif_);
53 ChunkListDelete(&mux->xmp_);
54 ChunkListDelete(&mux->unknown_);
55 }
56
WebPMuxDelete(WebPMux * mux)57 void WebPMuxDelete(WebPMux* mux) {
58 if (mux != NULL) {
59 MuxRelease(mux);
60 WebPSafeFree(mux);
61 }
62 }
63
64 //------------------------------------------------------------------------------
65 // Helper method(s).
66
67 // Handy MACRO, makes MuxSet() very symmetric to MuxGet().
68 #define SWITCH_ID_LIST(INDEX, LIST) \
69 if (idx == (INDEX)) { \
70 err = ChunkAssignData(&chunk, data, copy_data, tag); \
71 if (err == WEBP_MUX_OK) { \
72 err = ChunkSetHead(&chunk, (LIST)); \
73 } \
74 return err; \
75 }
76
MuxSet(WebPMux * const mux,uint32_t tag,const WebPData * const data,int copy_data)77 static WebPMuxError MuxSet(WebPMux* const mux, uint32_t tag,
78 const WebPData* const data, int copy_data) {
79 WebPChunk chunk;
80 WebPMuxError err = WEBP_MUX_NOT_FOUND;
81 const CHUNK_INDEX idx = ChunkGetIndexFromTag(tag);
82 assert(mux != NULL);
83 assert(!IsWPI(kChunks[idx].id));
84
85 ChunkInit(&chunk);
86 SWITCH_ID_LIST(IDX_VP8X, &mux->vp8x_);
87 SWITCH_ID_LIST(IDX_ICCP, &mux->iccp_);
88 SWITCH_ID_LIST(IDX_ANIM, &mux->anim_);
89 SWITCH_ID_LIST(IDX_EXIF, &mux->exif_);
90 SWITCH_ID_LIST(IDX_XMP, &mux->xmp_);
91 SWITCH_ID_LIST(IDX_UNKNOWN, &mux->unknown_);
92 return err;
93 }
94 #undef SWITCH_ID_LIST
95
96 // Create data for frame given image data, offsets and duration.
CreateFrameData(int width,int height,const WebPMuxFrameInfo * const info,WebPData * const frame)97 static WebPMuxError CreateFrameData(
98 int width, int height, const WebPMuxFrameInfo* const info,
99 WebPData* const frame) {
100 uint8_t* frame_bytes;
101 const size_t frame_size = kChunks[IDX_ANMF].size;
102
103 assert(width > 0 && height > 0 && info->duration >= 0);
104 assert(info->dispose_method == (info->dispose_method & 1));
105 // Note: assertion on upper bounds is done in PutLE24().
106
107 frame_bytes = (uint8_t*)WebPSafeMalloc(1ULL, frame_size);
108 if (frame_bytes == NULL) return WEBP_MUX_MEMORY_ERROR;
109
110 PutLE24(frame_bytes + 0, info->x_offset / 2);
111 PutLE24(frame_bytes + 3, info->y_offset / 2);
112
113 PutLE24(frame_bytes + 6, width - 1);
114 PutLE24(frame_bytes + 9, height - 1);
115 PutLE24(frame_bytes + 12, info->duration);
116 frame_bytes[15] =
117 (info->blend_method == WEBP_MUX_NO_BLEND ? 2 : 0) |
118 (info->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ? 1 : 0);
119
120 frame->bytes = frame_bytes;
121 frame->size = frame_size;
122 return WEBP_MUX_OK;
123 }
124
125 // Outputs image data given a bitstream. The bitstream can either be a
126 // single-image WebP file or raw VP8/VP8L data.
127 // Also outputs 'is_lossless' to be true if the given bitstream is lossless.
GetImageData(const WebPData * const bitstream,WebPData * const image,WebPData * const alpha,int * const is_lossless)128 static WebPMuxError GetImageData(const WebPData* const bitstream,
129 WebPData* const image, WebPData* const alpha,
130 int* const is_lossless) {
131 WebPDataInit(alpha); // Default: no alpha.
132 if (bitstream->size < TAG_SIZE ||
133 memcmp(bitstream->bytes, "RIFF", TAG_SIZE)) {
134 // It is NOT webp file data. Return input data as is.
135 *image = *bitstream;
136 } else {
137 // It is webp file data. Extract image data from it.
138 const WebPMuxImage* wpi;
139 WebPMux* const mux = WebPMuxCreate(bitstream, 0);
140 if (mux == NULL) return WEBP_MUX_BAD_DATA;
141 wpi = mux->images_;
142 assert(wpi != NULL && wpi->img_ != NULL);
143 *image = wpi->img_->data_;
144 if (wpi->alpha_ != NULL) {
145 *alpha = wpi->alpha_->data_;
146 }
147 WebPMuxDelete(mux);
148 }
149 *is_lossless = VP8LCheckSignature(image->bytes, image->size);
150 return WEBP_MUX_OK;
151 }
152
DeleteChunks(WebPChunk ** chunk_list,uint32_t tag)153 static WebPMuxError DeleteChunks(WebPChunk** chunk_list, uint32_t tag) {
154 WebPMuxError err = WEBP_MUX_NOT_FOUND;
155 assert(chunk_list);
156 while (*chunk_list) {
157 WebPChunk* const chunk = *chunk_list;
158 if (chunk->tag_ == tag) {
159 *chunk_list = ChunkDelete(chunk);
160 err = WEBP_MUX_OK;
161 } else {
162 chunk_list = &chunk->next_;
163 }
164 }
165 return err;
166 }
167
MuxDeleteAllNamedData(WebPMux * const mux,uint32_t tag)168 static WebPMuxError MuxDeleteAllNamedData(WebPMux* const mux, uint32_t tag) {
169 const WebPChunkId id = ChunkGetIdFromTag(tag);
170 assert(mux != NULL);
171 if (IsWPI(id)) return WEBP_MUX_INVALID_ARGUMENT;
172 return DeleteChunks(MuxGetChunkListFromId(mux, id), tag);
173 }
174
175 //------------------------------------------------------------------------------
176 // Set API(s).
177
WebPMuxSetChunk(WebPMux * mux,const char fourcc[4],const WebPData * chunk_data,int copy_data)178 WebPMuxError WebPMuxSetChunk(WebPMux* mux, const char fourcc[4],
179 const WebPData* chunk_data, int copy_data) {
180 uint32_t tag;
181 WebPMuxError err;
182 if (mux == NULL || fourcc == NULL || chunk_data == NULL ||
183 chunk_data->bytes == NULL || chunk_data->size > MAX_CHUNK_PAYLOAD) {
184 return WEBP_MUX_INVALID_ARGUMENT;
185 }
186 tag = ChunkGetTagFromFourCC(fourcc);
187
188 // Delete existing chunk(s) with the same 'fourcc'.
189 err = MuxDeleteAllNamedData(mux, tag);
190 if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
191
192 // Add the given chunk.
193 return MuxSet(mux, tag, chunk_data, copy_data);
194 }
195
196 // Creates a chunk from given 'data' and sets it as 1st chunk in 'chunk_list'.
AddDataToChunkList(const WebPData * const data,int copy_data,uint32_t tag,WebPChunk ** chunk_list)197 static WebPMuxError AddDataToChunkList(
198 const WebPData* const data, int copy_data, uint32_t tag,
199 WebPChunk** chunk_list) {
200 WebPChunk chunk;
201 WebPMuxError err;
202 ChunkInit(&chunk);
203 err = ChunkAssignData(&chunk, data, copy_data, tag);
204 if (err != WEBP_MUX_OK) goto Err;
205 err = ChunkSetHead(&chunk, chunk_list);
206 if (err != WEBP_MUX_OK) goto Err;
207 return WEBP_MUX_OK;
208 Err:
209 ChunkRelease(&chunk);
210 return err;
211 }
212
213 // Extracts image & alpha data from the given bitstream and then sets wpi.alpha_
214 // and wpi.img_ appropriately.
SetAlphaAndImageChunks(const WebPData * const bitstream,int copy_data,WebPMuxImage * const wpi)215 static WebPMuxError SetAlphaAndImageChunks(
216 const WebPData* const bitstream, int copy_data, WebPMuxImage* const wpi) {
217 int is_lossless = 0;
218 WebPData image, alpha;
219 WebPMuxError err = GetImageData(bitstream, &image, &alpha, &is_lossless);
220 const int image_tag =
221 is_lossless ? kChunks[IDX_VP8L].tag : kChunks[IDX_VP8].tag;
222 if (err != WEBP_MUX_OK) return err;
223 if (alpha.bytes != NULL) {
224 err = AddDataToChunkList(&alpha, copy_data, kChunks[IDX_ALPHA].tag,
225 &wpi->alpha_);
226 if (err != WEBP_MUX_OK) return err;
227 }
228 err = AddDataToChunkList(&image, copy_data, image_tag, &wpi->img_);
229 if (err != WEBP_MUX_OK) return err;
230 return MuxImageFinalize(wpi) ? WEBP_MUX_OK : WEBP_MUX_INVALID_ARGUMENT;
231 }
232
WebPMuxSetImage(WebPMux * mux,const WebPData * bitstream,int copy_data)233 WebPMuxError WebPMuxSetImage(WebPMux* mux, const WebPData* bitstream,
234 int copy_data) {
235 WebPMuxImage wpi;
236 WebPMuxError err;
237
238 if (mux == NULL || bitstream == NULL || bitstream->bytes == NULL ||
239 bitstream->size > MAX_CHUNK_PAYLOAD) {
240 return WEBP_MUX_INVALID_ARGUMENT;
241 }
242
243 if (mux->images_ != NULL) {
244 // Only one 'simple image' can be added in mux. So, remove present images.
245 DeleteAllImages(&mux->images_);
246 }
247
248 MuxImageInit(&wpi);
249 err = SetAlphaAndImageChunks(bitstream, copy_data, &wpi);
250 if (err != WEBP_MUX_OK) goto Err;
251
252 // Add this WebPMuxImage to mux.
253 err = MuxImagePush(&wpi, &mux->images_);
254 if (err != WEBP_MUX_OK) goto Err;
255
256 // All is well.
257 return WEBP_MUX_OK;
258
259 Err: // Something bad happened.
260 MuxImageRelease(&wpi);
261 return err;
262 }
263
WebPMuxPushFrame(WebPMux * mux,const WebPMuxFrameInfo * info,int copy_data)264 WebPMuxError WebPMuxPushFrame(WebPMux* mux, const WebPMuxFrameInfo* info,
265 int copy_data) {
266 WebPMuxImage wpi;
267 WebPMuxError err;
268
269 if (mux == NULL || info == NULL) return WEBP_MUX_INVALID_ARGUMENT;
270
271 if (info->id != WEBP_CHUNK_ANMF) return WEBP_MUX_INVALID_ARGUMENT;
272
273 if (info->bitstream.bytes == NULL ||
274 info->bitstream.size > MAX_CHUNK_PAYLOAD) {
275 return WEBP_MUX_INVALID_ARGUMENT;
276 }
277
278 if (mux->images_ != NULL) {
279 const WebPMuxImage* const image = mux->images_;
280 const uint32_t image_id = (image->header_ != NULL) ?
281 ChunkGetIdFromTag(image->header_->tag_) : WEBP_CHUNK_IMAGE;
282 if (image_id != info->id) {
283 return WEBP_MUX_INVALID_ARGUMENT; // Conflicting frame types.
284 }
285 }
286
287 MuxImageInit(&wpi);
288 err = SetAlphaAndImageChunks(&info->bitstream, copy_data, &wpi);
289 if (err != WEBP_MUX_OK) goto Err;
290 assert(wpi.img_ != NULL); // As SetAlphaAndImageChunks() was successful.
291
292 {
293 WebPData frame;
294 const uint32_t tag = kChunks[IDX_ANMF].tag;
295 WebPMuxFrameInfo tmp = *info;
296 tmp.x_offset &= ~1; // Snap offsets to even.
297 tmp.y_offset &= ~1;
298 if (tmp.x_offset < 0 || tmp.x_offset >= MAX_POSITION_OFFSET ||
299 tmp.y_offset < 0 || tmp.y_offset >= MAX_POSITION_OFFSET ||
300 (tmp.duration < 0 || tmp.duration >= MAX_DURATION) ||
301 tmp.dispose_method != (tmp.dispose_method & 1)) {
302 err = WEBP_MUX_INVALID_ARGUMENT;
303 goto Err;
304 }
305 err = CreateFrameData(wpi.width_, wpi.height_, &tmp, &frame);
306 if (err != WEBP_MUX_OK) goto Err;
307 // Add frame chunk (with copy_data = 1).
308 err = AddDataToChunkList(&frame, 1, tag, &wpi.header_);
309 WebPDataClear(&frame); // frame owned by wpi.header_ now.
310 if (err != WEBP_MUX_OK) goto Err;
311 }
312
313 // Add this WebPMuxImage to mux.
314 err = MuxImagePush(&wpi, &mux->images_);
315 if (err != WEBP_MUX_OK) goto Err;
316
317 // All is well.
318 return WEBP_MUX_OK;
319
320 Err: // Something bad happened.
321 MuxImageRelease(&wpi);
322 return err;
323 }
324
WebPMuxSetAnimationParams(WebPMux * mux,const WebPMuxAnimParams * params)325 WebPMuxError WebPMuxSetAnimationParams(WebPMux* mux,
326 const WebPMuxAnimParams* params) {
327 WebPMuxError err;
328 uint8_t data[ANIM_CHUNK_SIZE];
329 const WebPData anim = { data, ANIM_CHUNK_SIZE };
330
331 if (mux == NULL || params == NULL) return WEBP_MUX_INVALID_ARGUMENT;
332 if (params->loop_count < 0 || params->loop_count >= MAX_LOOP_COUNT) {
333 return WEBP_MUX_INVALID_ARGUMENT;
334 }
335
336 // Delete any existing ANIM chunk(s).
337 err = MuxDeleteAllNamedData(mux, kChunks[IDX_ANIM].tag);
338 if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
339
340 // Set the animation parameters.
341 PutLE32(data, params->bgcolor);
342 PutLE16(data + 4, params->loop_count);
343 return MuxSet(mux, kChunks[IDX_ANIM].tag, &anim, 1);
344 }
345
WebPMuxSetCanvasSize(WebPMux * mux,int width,int height)346 WebPMuxError WebPMuxSetCanvasSize(WebPMux* mux,
347 int width, int height) {
348 WebPMuxError err;
349 if (mux == NULL) {
350 return WEBP_MUX_INVALID_ARGUMENT;
351 }
352 if (width < 0 || height < 0 ||
353 width > MAX_CANVAS_SIZE || height > MAX_CANVAS_SIZE) {
354 return WEBP_MUX_INVALID_ARGUMENT;
355 }
356 if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
357 return WEBP_MUX_INVALID_ARGUMENT;
358 }
359 if ((width * height) == 0 && (width | height) != 0) {
360 // one of width / height is zero, but not both -> invalid!
361 return WEBP_MUX_INVALID_ARGUMENT;
362 }
363 // If we already assembled a VP8X chunk, invalidate it.
364 err = MuxDeleteAllNamedData(mux, kChunks[IDX_VP8X].tag);
365 if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
366
367 mux->canvas_width_ = width;
368 mux->canvas_height_ = height;
369 return WEBP_MUX_OK;
370 }
371
372 //------------------------------------------------------------------------------
373 // Delete API(s).
374
WebPMuxDeleteChunk(WebPMux * mux,const char fourcc[4])375 WebPMuxError WebPMuxDeleteChunk(WebPMux* mux, const char fourcc[4]) {
376 if (mux == NULL || fourcc == NULL) return WEBP_MUX_INVALID_ARGUMENT;
377 return MuxDeleteAllNamedData(mux, ChunkGetTagFromFourCC(fourcc));
378 }
379
WebPMuxDeleteFrame(WebPMux * mux,uint32_t nth)380 WebPMuxError WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth) {
381 if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
382 return MuxImageDeleteNth(&mux->images_, nth);
383 }
384
385 //------------------------------------------------------------------------------
386 // Assembly of the WebP RIFF file.
387
GetFrameInfo(const WebPChunk * const frame_chunk,int * const x_offset,int * const y_offset,int * const duration)388 static WebPMuxError GetFrameInfo(
389 const WebPChunk* const frame_chunk,
390 int* const x_offset, int* const y_offset, int* const duration) {
391 const WebPData* const data = &frame_chunk->data_;
392 const size_t expected_data_size = ANMF_CHUNK_SIZE;
393 assert(frame_chunk->tag_ == kChunks[IDX_ANMF].tag);
394 assert(frame_chunk != NULL);
395 if (data->size != expected_data_size) return WEBP_MUX_INVALID_ARGUMENT;
396
397 *x_offset = 2 * GetLE24(data->bytes + 0);
398 *y_offset = 2 * GetLE24(data->bytes + 3);
399 *duration = GetLE24(data->bytes + 12);
400 return WEBP_MUX_OK;
401 }
402
GetImageInfo(const WebPMuxImage * const wpi,int * const x_offset,int * const y_offset,int * const duration,int * const width,int * const height)403 static WebPMuxError GetImageInfo(const WebPMuxImage* const wpi,
404 int* const x_offset, int* const y_offset,
405 int* const duration,
406 int* const width, int* const height) {
407 const WebPChunk* const frame_chunk = wpi->header_;
408 WebPMuxError err;
409 assert(wpi != NULL);
410 assert(frame_chunk != NULL);
411
412 // Get offsets and duration from ANMF chunk.
413 err = GetFrameInfo(frame_chunk, x_offset, y_offset, duration);
414 if (err != WEBP_MUX_OK) return err;
415
416 // Get width and height from VP8/VP8L chunk.
417 if (width != NULL) *width = wpi->width_;
418 if (height != NULL) *height = wpi->height_;
419 return WEBP_MUX_OK;
420 }
421
422 // Returns the tightest dimension for the canvas considering the image list.
GetAdjustedCanvasSize(const WebPMux * const mux,int * const width,int * const height)423 static WebPMuxError GetAdjustedCanvasSize(const WebPMux* const mux,
424 int* const width, int* const height) {
425 WebPMuxImage* wpi = NULL;
426 assert(mux != NULL);
427 assert(width != NULL && height != NULL);
428
429 wpi = mux->images_;
430 assert(wpi != NULL);
431 assert(wpi->img_ != NULL);
432
433 if (wpi->next_ != NULL) {
434 int max_x = 0, max_y = 0;
435 // if we have a chain of wpi's, header_ is necessarily set
436 assert(wpi->header_ != NULL);
437 // Aggregate the bounding box for animation frames.
438 for (; wpi != NULL; wpi = wpi->next_) {
439 int x_offset = 0, y_offset = 0, duration = 0, w = 0, h = 0;
440 const WebPMuxError err = GetImageInfo(wpi, &x_offset, &y_offset,
441 &duration, &w, &h);
442 const int max_x_pos = x_offset + w;
443 const int max_y_pos = y_offset + h;
444 if (err != WEBP_MUX_OK) return err;
445 assert(x_offset < MAX_POSITION_OFFSET);
446 assert(y_offset < MAX_POSITION_OFFSET);
447
448 if (max_x_pos > max_x) max_x = max_x_pos;
449 if (max_y_pos > max_y) max_y = max_y_pos;
450 }
451 *width = max_x;
452 *height = max_y;
453 } else {
454 // For a single image, canvas dimensions are same as image dimensions.
455 *width = wpi->width_;
456 *height = wpi->height_;
457 }
458 return WEBP_MUX_OK;
459 }
460
461 // VP8X format:
462 // Total Size : 10,
463 // Flags : 4 bytes,
464 // Width : 3 bytes,
465 // Height : 3 bytes.
CreateVP8XChunk(WebPMux * const mux)466 static WebPMuxError CreateVP8XChunk(WebPMux* const mux) {
467 WebPMuxError err = WEBP_MUX_OK;
468 uint32_t flags = 0;
469 int width = 0;
470 int height = 0;
471 uint8_t data[VP8X_CHUNK_SIZE];
472 const WebPData vp8x = { data, VP8X_CHUNK_SIZE };
473 const WebPMuxImage* images = NULL;
474
475 assert(mux != NULL);
476 images = mux->images_; // First image.
477 if (images == NULL || images->img_ == NULL ||
478 images->img_->data_.bytes == NULL) {
479 return WEBP_MUX_INVALID_ARGUMENT;
480 }
481
482 // If VP8X chunk(s) is(are) already present, remove them (and later add new
483 // VP8X chunk with updated flags).
484 err = MuxDeleteAllNamedData(mux, kChunks[IDX_VP8X].tag);
485 if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
486
487 // Set flags.
488 if (mux->iccp_ != NULL && mux->iccp_->data_.bytes != NULL) {
489 flags |= ICCP_FLAG;
490 }
491 if (mux->exif_ != NULL && mux->exif_->data_.bytes != NULL) {
492 flags |= EXIF_FLAG;
493 }
494 if (mux->xmp_ != NULL && mux->xmp_->data_.bytes != NULL) {
495 flags |= XMP_FLAG;
496 }
497 if (images->header_ != NULL) {
498 if (images->header_->tag_ == kChunks[IDX_ANMF].tag) {
499 // This is an image with animation.
500 flags |= ANIMATION_FLAG;
501 }
502 }
503 if (MuxImageCount(images, WEBP_CHUNK_ALPHA) > 0) {
504 flags |= ALPHA_FLAG; // Some images have an alpha channel.
505 }
506
507 err = GetAdjustedCanvasSize(mux, &width, &height);
508 if (err != WEBP_MUX_OK) return err;
509
510 if (width <= 0 || height <= 0) {
511 return WEBP_MUX_INVALID_ARGUMENT;
512 }
513 if (width > MAX_CANVAS_SIZE || height > MAX_CANVAS_SIZE) {
514 return WEBP_MUX_INVALID_ARGUMENT;
515 }
516
517 if (mux->canvas_width_ != 0 || mux->canvas_height_ != 0) {
518 if (width > mux->canvas_width_ || height > mux->canvas_height_) {
519 return WEBP_MUX_INVALID_ARGUMENT;
520 }
521 width = mux->canvas_width_;
522 height = mux->canvas_height_;
523 }
524
525 if (flags == 0 && mux->unknown_ == NULL) {
526 // For simple file format, VP8X chunk should not be added.
527 return WEBP_MUX_OK;
528 }
529
530 if (MuxHasAlpha(images)) {
531 // This means some frames explicitly/implicitly contain alpha.
532 // Note: This 'flags' update must NOT be done for a lossless image
533 // without a VP8X chunk!
534 flags |= ALPHA_FLAG;
535 }
536
537 PutLE32(data + 0, flags); // VP8X chunk flags.
538 PutLE24(data + 4, width - 1); // canvas width.
539 PutLE24(data + 7, height - 1); // canvas height.
540
541 return MuxSet(mux, kChunks[IDX_VP8X].tag, &vp8x, 1);
542 }
543
544 // Cleans up 'mux' by removing any unnecessary chunks.
MuxCleanup(WebPMux * const mux)545 static WebPMuxError MuxCleanup(WebPMux* const mux) {
546 int num_frames;
547 int num_anim_chunks;
548
549 // If we have an image with a single frame, and its rectangle
550 // covers the whole canvas, convert it to a non-animated image
551 // (to avoid writing ANMF chunk unnecessarily).
552 WebPMuxError err = WebPMuxNumChunks(mux, kChunks[IDX_ANMF].id, &num_frames);
553 if (err != WEBP_MUX_OK) return err;
554 if (num_frames == 1) {
555 WebPMuxImage* frame = NULL;
556 err = MuxImageGetNth((const WebPMuxImage**)&mux->images_, 1, &frame);
557 assert(err == WEBP_MUX_OK); // We know that one frame does exist.
558 assert(frame != NULL);
559 if (frame->header_ != NULL &&
560 ((mux->canvas_width_ == 0 && mux->canvas_height_ == 0) ||
561 (frame->width_ == mux->canvas_width_ &&
562 frame->height_ == mux->canvas_height_))) {
563 assert(frame->header_->tag_ == kChunks[IDX_ANMF].tag);
564 ChunkDelete(frame->header_); // Removes ANMF chunk.
565 frame->header_ = NULL;
566 num_frames = 0;
567 }
568 }
569 // Remove ANIM chunk if this is a non-animated image.
570 err = WebPMuxNumChunks(mux, kChunks[IDX_ANIM].id, &num_anim_chunks);
571 if (err != WEBP_MUX_OK) return err;
572 if (num_anim_chunks >= 1 && num_frames == 0) {
573 err = MuxDeleteAllNamedData(mux, kChunks[IDX_ANIM].tag);
574 if (err != WEBP_MUX_OK) return err;
575 }
576 return WEBP_MUX_OK;
577 }
578
579 // Total size of a list of images.
ImageListDiskSize(const WebPMuxImage * wpi_list)580 static size_t ImageListDiskSize(const WebPMuxImage* wpi_list) {
581 size_t size = 0;
582 while (wpi_list != NULL) {
583 size += MuxImageDiskSize(wpi_list);
584 wpi_list = wpi_list->next_;
585 }
586 return size;
587 }
588
589 // Write out the given list of images into 'dst'.
ImageListEmit(const WebPMuxImage * wpi_list,uint8_t * dst)590 static uint8_t* ImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst) {
591 while (wpi_list != NULL) {
592 dst = MuxImageEmit(wpi_list, dst);
593 wpi_list = wpi_list->next_;
594 }
595 return dst;
596 }
597
WebPMuxAssemble(WebPMux * mux,WebPData * assembled_data)598 WebPMuxError WebPMuxAssemble(WebPMux* mux, WebPData* assembled_data) {
599 size_t size = 0;
600 uint8_t* data = NULL;
601 uint8_t* dst = NULL;
602 WebPMuxError err;
603
604 if (assembled_data == NULL) {
605 return WEBP_MUX_INVALID_ARGUMENT;
606 }
607 // Clean up returned data, in case something goes wrong.
608 memset(assembled_data, 0, sizeof(*assembled_data));
609
610 if (mux == NULL) {
611 return WEBP_MUX_INVALID_ARGUMENT;
612 }
613
614 // Finalize mux.
615 err = MuxCleanup(mux);
616 if (err != WEBP_MUX_OK) return err;
617 err = CreateVP8XChunk(mux);
618 if (err != WEBP_MUX_OK) return err;
619
620 // Allocate data.
621 size = ChunkListDiskSize(mux->vp8x_) + ChunkListDiskSize(mux->iccp_)
622 + ChunkListDiskSize(mux->anim_) + ImageListDiskSize(mux->images_)
623 + ChunkListDiskSize(mux->exif_) + ChunkListDiskSize(mux->xmp_)
624 + ChunkListDiskSize(mux->unknown_) + RIFF_HEADER_SIZE;
625
626 data = (uint8_t*)WebPSafeMalloc(1ULL, size);
627 if (data == NULL) return WEBP_MUX_MEMORY_ERROR;
628
629 // Emit header & chunks.
630 dst = MuxEmitRiffHeader(data, size);
631 dst = ChunkListEmit(mux->vp8x_, dst);
632 dst = ChunkListEmit(mux->iccp_, dst);
633 dst = ChunkListEmit(mux->anim_, dst);
634 dst = ImageListEmit(mux->images_, dst);
635 dst = ChunkListEmit(mux->exif_, dst);
636 dst = ChunkListEmit(mux->xmp_, dst);
637 dst = ChunkListEmit(mux->unknown_, dst);
638 assert(dst == data + size);
639
640 // Validate mux.
641 err = MuxValidate(mux);
642 if (err != WEBP_MUX_OK) {
643 WebPSafeFree(data);
644 data = NULL;
645 size = 0;
646 }
647
648 // Finalize data.
649 assembled_data->bytes = data;
650 assembled_data->size = size;
651
652 return err;
653 }
654
655 //------------------------------------------------------------------------------
656