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