blob: 689a624d5342154bde97f47242cfadfe92224f27 [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Urvang Joshia4f32ca2011-09-30 11:07:01 +05302//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// Simple command-line to create a WebP container file and to extract or strip
9// relevant data from the container file.
10//
James Zern04e84cf2011-11-04 15:20:08 -070011// Compile with: gcc -o webpmux webpmux.c -lwebpmux -lwebp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053012//
13//
14// Authors: Vikas (vikaas.arora@gmail.com),
15// Urvang (urvang@google.com)
16
17/* Usage examples:
18
19 Create container WebP file:
skal48600082012-11-14 06:19:31 +010020 webpmux -frame anim_1.webp +100+10+10 \
21 -frame anim_2.webp +100+25+25+1 \
22 -frame anim_3.webp +100+50+50+1 \
23 -frame anim_4.webp +100 \
24 -loop 10 -bgcolor 128,255,255,255 \
Urvang Joshia4f32ca2011-09-30 11:07:01 +053025 -o out_animation_container.webp
26
27 webpmux -set icc image_profile.icc in.webp -o out_icc_container.webp
Urvang Joshif903cba2012-10-31 16:30:41 -070028 webpmux -set exif image_metadata.exif in.webp -o out_exif_container.webp
29 webpmux -set xmp image_metadata.xmp in.webp -o out_xmp_container.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053030
Urvang Joshia4f32ca2011-09-30 11:07:01 +053031 Extract relevant data from WebP container file:
Urvang Joshia00a3da2012-10-31 17:49:15 -070032 webpmux -get frgm n in.webp -o out_fragment.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053033 webpmux -get frame n in.webp -o out_frame.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053034 webpmux -get icc in.webp -o image_profile.icc
Urvang Joshif903cba2012-10-31 16:30:41 -070035 webpmux -get exif in.webp -o image_metadata.exif
36 webpmux -get xmp in.webp -o image_metadata.xmp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053037
Urvang Joshia4f32ca2011-09-30 11:07:01 +053038 Strip data from WebP Container file:
James Zern04e84cf2011-11-04 15:20:08 -070039 webpmux -strip icc in.webp -o out.webp
Urvang Joshif903cba2012-10-31 16:30:41 -070040 webpmux -strip exif in.webp -o out.webp
41 webpmux -strip xmp in.webp -o out.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053042
43 Misc:
Urvang Joshia4f32ca2011-09-30 11:07:01 +053044 webpmux -info in.webp
James Zern04e84cf2011-11-04 15:20:08 -070045 webpmux [ -h | -help ]
Urvang Joshia5042a32013-02-26 14:22:06 -080046 webpmux -version
Urvang Joshia4f32ca2011-09-30 11:07:01 +053047*/
48
Urvang Joshi5dbd4032013-03-15 14:46:12 -070049#ifdef HAVE_CONFIG_H
50#include "config.h"
51#endif
52
Urvang Joshia4f32ca2011-09-30 11:07:01 +053053#include <assert.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include "webp/mux.h"
James Zern061263a2012-05-11 16:00:57 -070058#include "./example_util.h"
Urvang Joshia4f32ca2011-09-30 11:07:01 +053059
Urvang Joshia4f32ca2011-09-30 11:07:01 +053060//------------------------------------------------------------------------------
61// Config object to parse command-line arguments.
62
63typedef enum {
64 NIL_ACTION = 0,
65 ACTION_GET,
66 ACTION_SET,
67 ACTION_STRIP,
68 ACTION_INFO,
69 ACTION_HELP
70} ActionType;
71
72typedef enum {
73 NIL_SUBTYPE = 0,
Urvang Joshifa30c862012-11-01 15:34:46 -070074 SUBTYPE_ANMF,
75 SUBTYPE_LOOP,
76 SUBTYPE_BGCOLOR
Urvang Joshia4f32ca2011-09-30 11:07:01 +053077} FeatureSubType;
78
79typedef struct {
80 FeatureSubType subtype_;
81 const char* filename_;
82 const char* params_;
83} FeatureArg;
84
85typedef enum {
86 NIL_FEATURE = 0,
Urvang Joshif903cba2012-10-31 16:30:41 -070087 FEATURE_EXIF,
88 FEATURE_XMP,
Urvang Joshia4f32ca2011-09-30 11:07:01 +053089 FEATURE_ICCP,
Urvang Joshia00a3da2012-10-31 17:49:15 -070090 FEATURE_ANMF,
91 FEATURE_FRGM,
Urvang Joshif903cba2012-10-31 16:30:41 -070092 LAST_FEATURE
Urvang Joshia4f32ca2011-09-30 11:07:01 +053093} FeatureType;
94
Urvang Joshif903cba2012-10-31 16:30:41 -070095static const char* const kFourccList[LAST_FEATURE] = {
96 NULL, "EXIF", "XMP ", "ICCP", "ANMF", "FRGM"
97};
98
99static const char* const kDescriptions[LAST_FEATURE] = {
100 NULL, "EXIF metadata", "XMP metadata", "ICC profile",
Urvang Joshia00a3da2012-10-31 17:49:15 -0700101 "Animation frame", "Image fragment"
Urvang Joshif903cba2012-10-31 16:30:41 -0700102};
103
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530104typedef struct {
105 FeatureType type_;
106 FeatureArg* args_;
107 int arg_count_;
108} Feature;
109
110typedef struct {
111 ActionType action_type_;
112 const char* input_;
113 const char* output_;
114 Feature feature_;
115} WebPMuxConfig;
116
117//------------------------------------------------------------------------------
118// Helper functions.
119
James Zern04e84cf2011-11-04 15:20:08 -0700120static int CountOccurrences(const char* arglist[], int list_length,
121 const char* arg) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530122 int i;
123 int num_occurences = 0;
124
125 for (i = 0; i < list_length; ++i) {
126 if (!strcmp(arglist[i], arg)) {
127 ++num_occurences;
128 }
129 }
130 return num_occurences;
131}
132
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530133static const char* const kErrorMessages[] = {
Urvang Joshia4b9b1c2012-07-06 17:33:59 +0530134 "WEBP_MUX_NOT_FOUND", "WEBP_MUX_INVALID_ARGUMENT", "WEBP_MUX_BAD_DATA",
135 "WEBP_MUX_MEMORY_ERROR", "WEBP_MUX_NOT_ENOUGH_DATA"
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530136};
137
138static const char* ErrorString(WebPMuxError err) {
Urvang Joshia4b9b1c2012-07-06 17:33:59 +0530139 assert(err <= WEBP_MUX_NOT_FOUND && err >= WEBP_MUX_NOT_ENOUGH_DATA);
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530140 return kErrorMessages[-err];
141}
142
James Zerna0b27362012-01-27 17:39:47 -0800143#define RETURN_IF_ERROR(ERR_MSG) \
144 if (err != WEBP_MUX_OK) { \
145 fprintf(stderr, ERR_MSG); \
146 return err; \
147 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530148
James Zerna0b27362012-01-27 17:39:47 -0800149#define RETURN_IF_ERROR2(ERR_MSG, FORMAT_STR) \
150 if (err != WEBP_MUX_OK) { \
151 fprintf(stderr, ERR_MSG, FORMAT_STR); \
152 return err; \
153 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530154
Urvang Joshid0c79f02012-08-23 16:28:36 +0530155#define RETURN_IF_ERROR3(ERR_MSG, FORMAT_STR1, FORMAT_STR2) \
156 if (err != WEBP_MUX_OK) { \
157 fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
158 return err; \
159 }
160
James Zerna0b27362012-01-27 17:39:47 -0800161#define ERROR_GOTO1(ERR_MSG, LABEL) \
162 do { \
163 fprintf(stderr, ERR_MSG); \
164 ok = 0; \
165 goto LABEL; \
166 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530167
James Zerna0b27362012-01-27 17:39:47 -0800168#define ERROR_GOTO2(ERR_MSG, FORMAT_STR, LABEL) \
169 do { \
170 fprintf(stderr, ERR_MSG, FORMAT_STR); \
171 ok = 0; \
172 goto LABEL; \
173 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530174
James Zerna0b27362012-01-27 17:39:47 -0800175#define ERROR_GOTO3(ERR_MSG, FORMAT_STR1, FORMAT_STR2, LABEL) \
Urvang Joshi6393fe42013-04-26 15:55:42 -0700176 do { \
177 fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
178 ok = 0; \
179 goto LABEL; \
180 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530181
182static WebPMuxError DisplayInfo(const WebPMux* mux) {
Urvang Joshifffefd12013-05-02 13:54:25 -0700183 int width, height;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530184 uint32_t flag;
185
Urvang Joshifffefd12013-05-02 13:54:25 -0700186 WebPMuxError err = WebPMuxGetCanvasSize(mux, &width, &height);
187 RETURN_IF_ERROR("Failed to retrieve canvas width/height.\n");
188 printf("Canvas size: %d x %d\n", width, height);
189
190 err = WebPMuxGetFeatures(mux, &flag);
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700191#ifndef WEBP_EXPERIMENTAL_FEATURES
192 if (flag & FRAGMENTS_FLAG) err = WEBP_MUX_INVALID_ARGUMENT;
193#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530194 RETURN_IF_ERROR("Failed to retrieve features\n");
195
196 if (flag == 0) {
197 fprintf(stderr, "No features present.\n");
198 return err;
199 }
200
201 // Print the features present.
James Zern974aaff2012-01-24 12:46:46 -0800202 printf("Features present:");
203 if (flag & ANIMATION_FLAG) printf(" animation");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700204 if (flag & FRAGMENTS_FLAG) printf(" image fragments");
James Zernd8dc72a2013-03-13 14:04:20 -0700205 if (flag & ICCP_FLAG) printf(" ICC profile");
Urvang Joshif903cba2012-10-31 16:30:41 -0700206 if (flag & EXIF_FLAG) printf(" EXIF metadata");
207 if (flag & XMP_FLAG) printf(" XMP metadata");
James Zern974aaff2012-01-24 12:46:46 -0800208 if (flag & ALPHA_FLAG) printf(" transparency");
209 printf("\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530210
Urvang Joshia00a3da2012-10-31 17:49:15 -0700211 if ((flag & ANIMATION_FLAG) || (flag & FRAGMENTS_FLAG)) {
Urvang Joshid0c79f02012-08-23 16:28:36 +0530212 const int is_anim = !!(flag & ANIMATION_FLAG);
Urvang Joshi92f80592012-10-30 12:14:10 -0700213 const WebPChunkId id = is_anim ? WEBP_CHUNK_ANMF : WEBP_CHUNK_FRGM;
Urvang Joshia00a3da2012-10-31 17:49:15 -0700214 const char* const type_str = is_anim ? "frame" : "fragment";
James Zerneec4b872012-01-07 12:44:01 -0800215 int nFrames;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530216
Urvang Joshid0c79f02012-08-23 16:28:36 +0530217 if (is_anim) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700218 WebPMuxAnimParams params;
219 err = WebPMuxGetAnimationParams(mux, &params);
220 RETURN_IF_ERROR("Failed to retrieve animation parameters\n");
221 printf("Background color : 0x%.8X Loop Count : %d\n",
222 params.bgcolor, params.loop_count);
Urvang Joshid0c79f02012-08-23 16:28:36 +0530223 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530224
Urvang Joshid0c79f02012-08-23 16:28:36 +0530225 err = WebPMuxNumChunks(mux, id, &nFrames);
226 RETURN_IF_ERROR2("Failed to retrieve number of %ss\n", type_str);
227
228 printf("Number of %ss: %d\n", type_str, nFrames);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530229 if (nFrames > 0) {
230 int i;
Urvang Joshid0c79f02012-08-23 16:28:36 +0530231 printf("No.: x_offset y_offset ");
Urvang Joshifa30c862012-11-01 15:34:46 -0700232 if (is_anim) printf("duration dispose ");
Urvang Joshid0c79f02012-08-23 16:28:36 +0530233 printf("image_size\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530234 for (i = 1; i <= nFrames; i++) {
Urvang Joshiab3234a2012-08-23 15:18:51 +0530235 WebPMuxFrameInfo frame;
236 err = WebPMuxGetFrame(mux, i, &frame);
Urvang Joshid0c79f02012-08-23 16:28:36 +0530237 RETURN_IF_ERROR3("Failed to retrieve %s#%d\n", type_str, i);
Urvang Joshia0770722012-10-30 14:54:46 -0700238 printf("%3d: %8d %8d ", i, frame.x_offset, frame.y_offset);
Urvang Joshifa30c862012-11-01 15:34:46 -0700239 if (is_anim) printf("%8d %7d ", frame.duration, frame.dispose_method);
James Zern14d42af2013-03-20 16:59:35 -0700240 printf("%10d\n", (int)frame.bitstream.size);
Urvang Joshia0770722012-10-30 14:54:46 -0700241 WebPDataClear(&frame.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530242 }
243 }
244 }
245
246 if (flag & ICCP_FLAG) {
James Zerneec4b872012-01-07 12:44:01 -0800247 WebPData icc_profile;
Urvang Joshi1c04a0d2012-08-23 15:28:20 +0530248 err = WebPMuxGetChunk(mux, "ICCP", &icc_profile);
Urvang Joshif903cba2012-10-31 16:30:41 -0700249 RETURN_IF_ERROR("Failed to retrieve the ICC profile\n");
James Zern14d42af2013-03-20 16:59:35 -0700250 printf("Size of the ICC profile data: %d\n", (int)icc_profile.size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530251 }
252
Urvang Joshif903cba2012-10-31 16:30:41 -0700253 if (flag & EXIF_FLAG) {
254 WebPData exif;
255 err = WebPMuxGetChunk(mux, "EXIF", &exif);
256 RETURN_IF_ERROR("Failed to retrieve the EXIF metadata\n");
James Zern14d42af2013-03-20 16:59:35 -0700257 printf("Size of the EXIF metadata: %d\n", (int)exif.size);
Urvang Joshif903cba2012-10-31 16:30:41 -0700258 }
259
260 if (flag & XMP_FLAG) {
261 WebPData xmp;
262 err = WebPMuxGetChunk(mux, "XMP ", &xmp);
263 RETURN_IF_ERROR("Failed to retrieve the XMP metadata\n");
James Zern14d42af2013-03-20 16:59:35 -0700264 printf("Size of the XMP metadata: %d\n", (int)xmp.size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530265 }
266
Urvang Joshia00a3da2012-10-31 17:49:15 -0700267 if ((flag & ALPHA_FLAG) && !(flag & (ANIMATION_FLAG | FRAGMENTS_FLAG))) {
Urvang Joshid0c79f02012-08-23 16:28:36 +0530268 WebPMuxFrameInfo image;
269 err = WebPMuxGetFrame(mux, 1, &image);
Urvang Joshicdf97aa2011-12-01 16:11:51 +0530270 RETURN_IF_ERROR("Failed to retrieve the image\n");
James Zern14d42af2013-03-20 16:59:35 -0700271 printf("Size of the image (with alpha): %d\n", (int)image.bitstream.size);
Urvang Joshicdf97aa2011-12-01 16:11:51 +0530272 }
273
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530274 return WEBP_MUX_OK;
275}
276
Pascal Massiminoabd030b2011-11-01 06:24:34 -0700277static void PrintHelp(void) {
James Zern974aaff2012-01-24 12:46:46 -0800278 printf("Usage: webpmux -get GET_OPTIONS INPUT -o OUTPUT\n");
279 printf(" webpmux -set SET_OPTIONS INPUT -o OUTPUT\n");
280 printf(" webpmux -strip STRIP_OPTIONS INPUT -o OUTPUT\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700281#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700282 printf(" webpmux -frgm FRAGMENT_OPTIONS [-frgm...] -o OUTPUT\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700283#endif
Urvang Joshifa30c862012-11-01 15:34:46 -0700284 printf(" webpmux -frame FRAME_OPTIONS [-frame...] [-loop LOOP_COUNT]"
285 "\n");
286 printf(" [-bgcolor BACKGROUND_COLOR] -o OUTPUT\n");
James Zern974aaff2012-01-24 12:46:46 -0800287 printf(" webpmux -info INPUT\n");
288 printf(" webpmux [-h|-help]\n");
Urvang Joshia5042a32013-02-26 14:22:06 -0800289 printf(" webpmux -version\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530290
James Zern974aaff2012-01-24 12:46:46 -0800291 printf("\n");
292 printf("GET_OPTIONS:\n");
293 printf(" Extract relevant data.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700294 printf(" icc Get ICC profile.\n");
295 printf(" exif Get EXIF metadata.\n");
296 printf(" xmp Get XMP metadata.\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700297#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700298 printf(" frgm n Get nth fragment.\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700299#endif
James Zern974aaff2012-01-24 12:46:46 -0800300 printf(" frame n Get nth frame.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530301
James Zern974aaff2012-01-24 12:46:46 -0800302 printf("\n");
303 printf("SET_OPTIONS:\n");
304 printf(" Set color profile/metadata.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700305 printf(" icc file.icc Set ICC profile.\n");
306 printf(" exif file.exif Set EXIF metadata.\n");
307 printf(" xmp file.xmp Set XMP metadata.\n");
308 printf(" where: 'file.icc' contains the ICC profile to be set,\n");
309 printf(" 'file.exif' contains the EXIF metadata to be set\n");
310 printf(" 'file.xmp' contains the XMP metadata to be set\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530311
James Zern974aaff2012-01-24 12:46:46 -0800312 printf("\n");
313 printf("STRIP_OPTIONS:\n");
314 printf(" Strip color profile/metadata.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700315 printf(" icc Strip ICC profile.\n");
316 printf(" exif Strip EXIF metadata.\n");
317 printf(" xmp Strip XMP metadata.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530318
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700319#ifdef WEBP_EXPERIMENTAL_FEATURES
James Zern974aaff2012-01-24 12:46:46 -0800320 printf("\n");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700321 printf("FRAGMENT_OPTIONS(i):\n");
322 printf(" Create fragmented image.\n");
James Zern974aaff2012-01-24 12:46:46 -0800323 printf(" file_i +xi+yi\n");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700324 printf(" where: 'file_i' is the i'th fragment (WebP format),\n");
325 printf(" 'xi','yi' specify the image offset for this fragment."
326 "\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700327#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530328
James Zern974aaff2012-01-24 12:46:46 -0800329 printf("\n");
330 printf("FRAME_OPTIONS(i):\n");
331 printf(" Create animation.\n");
James Zern8fab1612013-03-07 19:15:37 -0800332 printf(" file_i +di+xi+yi+mi\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700333 printf(" where: 'file_i' is the i'th animation frame (WebP format),\n");
James Zern974aaff2012-01-24 12:46:46 -0800334 printf(" 'di' is the pause duration before next frame.\n");
James Zern8fab1612013-03-07 19:15:37 -0800335 printf(" 'xi','yi' specify the image offset for this frame.\n");
Urvang Joshifa30c862012-11-01 15:34:46 -0700336 printf(" 'mi' is the dispose method for this frame (0 or 1).\n");
337
338 printf("\n");
339 printf("LOOP_COUNT:\n");
340 printf(" Number of times to repeat the animation.\n");
341 printf(" Valid range is 0 to 65535 [Default: 0 (infinite)].\n");
342
343 printf("\n");
344 printf("BACKGROUND_COLOR:\n");
345 printf(" Background color of the canvas.\n");
346 printf(" A,R,G,B\n");
347 printf(" where: 'A', 'R', 'G' and 'B' are integers in the range 0 to 255 "
348 "specifying\n");
349 printf(" the Alpha, Red, Green and Blue component values "
350 "respectively\n");
351 printf(" [Default: 255,255,255,255].\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530352
Urvang Joshif903cba2012-10-31 16:30:41 -0700353 printf("\nINPUT & OUTPUT are in WebP format.\n");
354
Urvang Joshifa30c862012-11-01 15:34:46 -0700355 printf("\nNote: The nature of EXIF, XMP and ICC data is not checked");
356 printf(" and is assumed to be\nvalid.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530357}
358
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530359static int ReadFileToWebPData(const char* const filename,
360 WebPData* const webp_data) {
361 const uint8_t* data;
362 size_t size;
363 if (!ExUtilReadFile(filename, &data, &size)) return 0;
Urvang Joshia0770722012-10-30 14:54:46 -0700364 webp_data->bytes = data;
365 webp_data->size = size;
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530366 return 1;
367}
368
James Zern061263a2012-05-11 16:00:57 -0700369static int CreateMux(const char* const filename, WebPMux** mux) {
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530370 WebPData bitstream;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530371 assert(mux != NULL);
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530372 if (!ReadFileToWebPData(filename, &bitstream)) return 0;
Urvang Joshi6d5c7972012-06-07 13:45:06 +0530373 *mux = WebPMuxCreate(&bitstream, 1);
Urvang Joshia0770722012-10-30 14:54:46 -0700374 free((void*)bitstream.bytes);
Urvang Joshi6d5c7972012-06-07 13:45:06 +0530375 if (*mux != NULL) return 1;
376 fprintf(stderr, "Failed to create mux object from file %s.\n", filename);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530377 return 0;
378}
379
James Zern0f7820e2012-01-24 14:08:27 -0800380static int WriteData(const char* filename, const WebPData* const webpdata) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530381 int ok = 0;
James Zern04e84cf2011-11-04 15:20:08 -0700382 FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530383 if (!fout) {
384 fprintf(stderr, "Error opening output WebP file %s!\n", filename);
385 return 0;
386 }
Urvang Joshia0770722012-10-30 14:54:46 -0700387 if (fwrite(webpdata->bytes, webpdata->size, 1, fout) != 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530388 fprintf(stderr, "Error writing file %s!\n", filename);
389 } else {
James Zern14d42af2013-03-20 16:59:35 -0700390 fprintf(stderr, "Saved file %s (%d bytes)\n",
391 filename, (int)webpdata->size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530392 ok = 1;
393 }
394 if (fout != stdout) fclose(fout);
395 return ok;
396}
397
398static int WriteWebP(WebPMux* const mux, const char* filename) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530399 int ok;
Urvang Joshif1df5582012-06-07 11:04:57 +0530400 WebPData webp_data;
401 const WebPMuxError err = WebPMuxAssemble(mux, &webp_data);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530402 if (err != WEBP_MUX_OK) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530403 fprintf(stderr, "Error (%s) assembling the WebP file.\n", ErrorString(err));
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530404 return 0;
405 }
Urvang Joshif1df5582012-06-07 11:04:57 +0530406 ok = WriteData(filename, &webp_data);
407 WebPDataClear(&webp_data);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530408 return ok;
409}
410
Urvang Joshiab3234a2012-08-23 15:18:51 +0530411static int ParseFrameArgs(const char* args, WebPMuxFrameInfo* const info) {
skal48600082012-11-14 06:19:31 +0100412 int dispose_method, dummy;
413 const int num_args = sscanf(args, "+%d+%d+%d+%d+%d",
414 &info->duration, &info->x_offset, &info->y_offset,
415 &dispose_method, &dummy);
416 switch (num_args) {
417 case 1:
418 info->x_offset = info->y_offset = 0; // fall through
419 case 3:
420 dispose_method = 0; // fall through
421 case 4:
422 break;
423 default:
424 return 0;
Urvang Joshifa30c862012-11-01 15:34:46 -0700425 }
426 // Note: The sanity of the following conversion is checked by
427 // WebPMuxSetAnimationParams().
428 info->dispose_method = (WebPMuxAnimDispose)dispose_method;
429 return 1;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530430}
431
Urvang Joshia00a3da2012-10-31 17:49:15 -0700432static int ParseFragmentArgs(const char* args, WebPMuxFrameInfo* const info) {
Urvang Joshia0770722012-10-30 14:54:46 -0700433 return (sscanf(args, "+%d+%d", &info->x_offset, &info->y_offset) == 2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530434}
435
Urvang Joshifa30c862012-11-01 15:34:46 -0700436static int ParseBgcolorArgs(const char* args, uint32_t* const bgcolor) {
437 uint32_t a, r, g, b;
438 if (sscanf(args, "%u,%u,%u,%u", &a, &r, &g, &b) != 4) return 0;
439 if (a >= 256 || r >= 256 || g >= 256 || b >= 256) return 0;
440 *bgcolor = (a << 24) | (r << 16) | (g << 8) | (b << 0);
441 return 1;
442}
443
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530444//------------------------------------------------------------------------------
445// Clean-up.
446
447static void DeleteConfig(WebPMuxConfig* config) {
448 if (config != NULL) {
449 free(config->feature_.args_);
450 free(config);
451 }
452}
453
454//------------------------------------------------------------------------------
455// Parsing.
456
457// Basic syntactic checks on the command-line arguments.
458// Returns 1 on valid, 0 otherwise.
459// Also fills up num_feature_args to be number of feature arguments given.
460// (e.g. if there are 4 '-frame's and 1 '-loop', then num_feature_args = 5).
461static int ValidateCommandLine(int argc, const char* argv[],
462 int* num_feature_args) {
463 int num_frame_args;
Urvang Joshia00a3da2012-10-31 17:49:15 -0700464 int num_frgm_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530465 int num_loop_args;
Urvang Joshifa30c862012-11-01 15:34:46 -0700466 int num_bgcolor_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530467 int ok = 1;
468
469 assert(num_feature_args != NULL);
470 *num_feature_args = 0;
471
472 // Simple checks.
James Zern04e84cf2011-11-04 15:20:08 -0700473 if (CountOccurrences(argv, argc, "-get") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530474 ERROR_GOTO1("ERROR: Multiple '-get' arguments specified.\n", ErrValidate);
475 }
James Zern04e84cf2011-11-04 15:20:08 -0700476 if (CountOccurrences(argv, argc, "-set") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530477 ERROR_GOTO1("ERROR: Multiple '-set' arguments specified.\n", ErrValidate);
478 }
James Zern04e84cf2011-11-04 15:20:08 -0700479 if (CountOccurrences(argv, argc, "-strip") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530480 ERROR_GOTO1("ERROR: Multiple '-strip' arguments specified.\n", ErrValidate);
481 }
James Zern04e84cf2011-11-04 15:20:08 -0700482 if (CountOccurrences(argv, argc, "-info") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530483 ERROR_GOTO1("ERROR: Multiple '-info' arguments specified.\n", ErrValidate);
484 }
James Zern04e84cf2011-11-04 15:20:08 -0700485 if (CountOccurrences(argv, argc, "-o") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530486 ERROR_GOTO1("ERROR: Multiple output files specified.\n", ErrValidate);
487 }
488
489 // Compound checks.
James Zern04e84cf2011-11-04 15:20:08 -0700490 num_frame_args = CountOccurrences(argv, argc, "-frame");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700491 num_frgm_args = CountOccurrences(argv, argc, "-frgm");
James Zern04e84cf2011-11-04 15:20:08 -0700492 num_loop_args = CountOccurrences(argv, argc, "-loop");
Urvang Joshifa30c862012-11-01 15:34:46 -0700493 num_bgcolor_args = CountOccurrences(argv, argc, "-bgcolor");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530494
495 if (num_loop_args > 1) {
496 ERROR_GOTO1("ERROR: Multiple loop counts specified.\n", ErrValidate);
497 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700498 if (num_bgcolor_args > 1) {
499 ERROR_GOTO1("ERROR: Multiple background colors specified.\n", ErrValidate);
500 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530501
Urvang Joshifa30c862012-11-01 15:34:46 -0700502 if ((num_frame_args == 0) && (num_loop_args + num_bgcolor_args > 0)) {
503 ERROR_GOTO1("ERROR: Loop count and background color are relevant only in "
504 "case of animation.\n", ErrValidate);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530505 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700506 if (num_frame_args > 0 && num_frgm_args > 0) {
507 ERROR_GOTO1("ERROR: Only one of frames & fragments can be specified at a "
508 "time.\n", ErrValidate);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530509 }
510
511 assert(ok == 1);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700512 if (num_frame_args == 0 && num_frgm_args == 0) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700513 // Single argument ('set' action for ICCP/EXIF/XMP, OR a 'get' action).
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530514 *num_feature_args = 1;
515 } else {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700516 // Multiple arguments ('set' action for animation or fragmented image).
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530517 if (num_frame_args > 0) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700518 *num_feature_args = num_frame_args + num_loop_args + num_bgcolor_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530519 } else {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700520 *num_feature_args = num_frgm_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530521 }
522 }
523
524 ErrValidate:
525 return ok;
526}
527
528#define ACTION_IS_NIL (config->action_type_ == NIL_ACTION)
529
530#define FEATURETYPE_IS_NIL (feature->type_ == NIL_FEATURE)
531
532#define CHECK_NUM_ARGS_LESS(NUM, LABEL) \
533 if (argc < i + (NUM)) { \
534 fprintf(stderr, "ERROR: Too few arguments for '%s'.\n", argv[i]); \
535 goto LABEL; \
536 }
537
538#define CHECK_NUM_ARGS_NOT_EQUAL(NUM, LABEL) \
539 if (argc != i + (NUM)) { \
540 fprintf(stderr, "ERROR: Too many arguments for '%s'.\n", argv[i]); \
541 goto LABEL; \
542 }
543
544// Parses command-line arguments to fill up config object. Also performs some
545// semantic checks.
546static int ParseCommandLine(int argc, const char* argv[],
547 WebPMuxConfig* config) {
548 int i = 0;
549 int feature_arg_index = 0;
550 int ok = 1;
551
552 while (i < argc) {
553 Feature* const feature = &config->feature_;
James Zern04e84cf2011-11-04 15:20:08 -0700554 FeatureArg* const arg = &feature->args_[feature_arg_index];
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530555 if (argv[i][0] == '-') { // One of the action types or output.
556 if (!strcmp(argv[i], "-set")) {
557 if (ACTION_IS_NIL) {
558 config->action_type_ = ACTION_SET;
559 } else {
560 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
561 }
562 ++i;
563 } else if (!strcmp(argv[i], "-get")) {
564 if (ACTION_IS_NIL) {
565 config->action_type_ = ACTION_GET;
566 } else {
567 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
568 }
569 ++i;
570 } else if (!strcmp(argv[i], "-strip")) {
571 if (ACTION_IS_NIL) {
572 config->action_type_ = ACTION_STRIP;
573 feature->arg_count_ = 0;
574 } else {
575 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
576 }
577 ++i;
578 } else if (!strcmp(argv[i], "-frame")) {
579 CHECK_NUM_ARGS_LESS(3, ErrParse);
580 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
581 config->action_type_ = ACTION_SET;
582 } else {
583 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
584 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700585 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_ANMF) {
586 feature->type_ = FEATURE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530587 } else {
588 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
589 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700590 arg->subtype_ = SUBTYPE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530591 arg->filename_ = argv[i + 1];
592 arg->params_ = argv[i + 2];
593 ++feature_arg_index;
594 i += 3;
Urvang Joshifa30c862012-11-01 15:34:46 -0700595 } else if (!strcmp(argv[i], "-loop") || !strcmp(argv[i], "-bgcolor")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530596 CHECK_NUM_ARGS_LESS(2, ErrParse);
597 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
598 config->action_type_ = ACTION_SET;
599 } else {
600 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
601 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700602 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_ANMF) {
603 feature->type_ = FEATURE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530604 } else {
605 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
606 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700607 arg->subtype_ =
608 !strcmp(argv[i], "-loop") ? SUBTYPE_LOOP : SUBTYPE_BGCOLOR;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530609 arg->params_ = argv[i + 1];
610 ++feature_arg_index;
611 i += 2;
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700612#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700613 } else if (!strcmp(argv[i], "-frgm")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530614 CHECK_NUM_ARGS_LESS(3, ErrParse);
615 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
616 config->action_type_ = ACTION_SET;
617 } else {
618 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
619 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700620 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_FRGM) {
621 feature->type_ = FEATURE_FRGM;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530622 } else {
623 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
624 }
625 arg->filename_ = argv[i + 1];
626 arg->params_ = argv[i + 2];
627 ++feature_arg_index;
628 i += 3;
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700629#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530630 } else if (!strcmp(argv[i], "-o")) {
631 CHECK_NUM_ARGS_LESS(2, ErrParse);
632 config->output_ = argv[i + 1];
633 i += 2;
634 } else if (!strcmp(argv[i], "-info")) {
635 CHECK_NUM_ARGS_NOT_EQUAL(2, ErrParse);
636 if (config->action_type_ != NIL_ACTION) {
637 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
638 } else {
639 config->action_type_ = ACTION_INFO;
640 feature->arg_count_ = 0;
641 config->input_ = argv[i + 1];
642 }
643 i += 2;
644 } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help")) {
645 PrintHelp();
646 DeleteConfig(config);
James Zern974aaff2012-01-24 12:46:46 -0800647 exit(0);
Urvang Joshia5042a32013-02-26 14:22:06 -0800648 } else if (!strcmp(argv[i], "-version")) {
649 const int version = WebPGetMuxVersion();
650 printf("%d.%d.%d\n",
651 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
652 DeleteConfig(config);
653 exit(0);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530654 } else {
655 ERROR_GOTO2("ERROR: Unknown option: '%s'.\n", argv[i], ErrParse);
656 }
657 } else { // One of the feature types or input.
658 if (ACTION_IS_NIL) {
659 ERROR_GOTO1("ERROR: Action must be specified before other arguments.\n",
660 ErrParse);
661 }
Urvang Joshif903cba2012-10-31 16:30:41 -0700662 if (!strcmp(argv[i], "icc") || !strcmp(argv[i], "exif") ||
663 !strcmp(argv[i], "xmp")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530664 if (FEATURETYPE_IS_NIL) {
665 feature->type_ = (!strcmp(argv[i], "icc")) ? FEATURE_ICCP :
Urvang Joshif903cba2012-10-31 16:30:41 -0700666 (!strcmp(argv[i], "exif")) ? FEATURE_EXIF : FEATURE_XMP;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530667 } else {
668 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
669 }
670 if (config->action_type_ == ACTION_SET) {
671 CHECK_NUM_ARGS_LESS(2, ErrParse);
672 arg->filename_ = argv[i + 1];
673 ++feature_arg_index;
674 i += 2;
675 } else {
676 ++i;
677 }
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700678#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530679 } else if ((!strcmp(argv[i], "frame") ||
Urvang Joshia00a3da2012-10-31 17:49:15 -0700680 !strcmp(argv[i], "frgm")) &&
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700681#else
682 } else if (!strcmp(argv[i], "frame") &&
683#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530684 (config->action_type_ == ACTION_GET)) {
685 CHECK_NUM_ARGS_LESS(2, ErrParse);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700686 feature->type_ = (!strcmp(argv[i], "frame")) ? FEATURE_ANMF :
687 FEATURE_FRGM;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530688 arg->params_ = argv[i + 1];
689 ++feature_arg_index;
690 i += 2;
James Zern04e84cf2011-11-04 15:20:08 -0700691 } else { // Assume input file.
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530692 if (config->input_ == NULL) {
693 config->input_ = argv[i];
694 } else {
695 ERROR_GOTO2("ERROR at '%s': Multiple input files specified.\n",
696 argv[i], ErrParse);
697 }
698 ++i;
699 }
700 }
701 }
702 ErrParse:
703 return ok;
704}
705
James Zern04e84cf2011-11-04 15:20:08 -0700706// Additional checks after config is filled.
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530707static int ValidateConfig(WebPMuxConfig* config) {
708 int ok = 1;
709 Feature* const feature = &config->feature_;
710
711 // Action.
712 if (ACTION_IS_NIL) {
713 ERROR_GOTO1("ERROR: No action specified.\n", ErrValidate2);
714 }
715
716 // Feature type.
717 if (FEATURETYPE_IS_NIL && config->action_type_ != ACTION_INFO) {
718 ERROR_GOTO1("ERROR: No feature specified.\n", ErrValidate2);
719 }
720
721 // Input file.
722 if (config->input_ == NULL) {
723 if (config->action_type_ != ACTION_SET) {
724 ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700725 } else if (feature->type_ != FEATURE_ANMF &&
726 feature->type_ != FEATURE_FRGM) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530727 ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2);
728 }
729 }
730
731 // Output file.
732 if (config->output_ == NULL && config->action_type_ != ACTION_INFO) {
733 ERROR_GOTO1("ERROR: No output file specified.\n", ErrValidate2);
734 }
735
736 ErrValidate2:
737 return ok;
738}
739
740// Create config object from command-line arguments.
741static int InitializeConfig(int argc, const char* argv[],
742 WebPMuxConfig** config) {
743 int num_feature_args = 0;
744 int ok = 1;
745
746 assert(config != NULL);
747 *config = NULL;
748
749 // Validate command-line arguments.
750 if (!ValidateCommandLine(argc, argv, &num_feature_args)) {
751 ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
752 }
753
754 // Allocate memory.
755 *config = (WebPMuxConfig*)calloc(1, sizeof(**config));
756 if (*config == NULL) {
757 ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1);
758 }
759 (*config)->feature_.arg_count_ = num_feature_args;
760 (*config)->feature_.args_ =
761 (FeatureArg*)calloc(num_feature_args, sizeof(FeatureArg));
762 if ((*config)->feature_.args_ == NULL) {
763 ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1);
764 }
765
766 // Parse command-line.
James Zern04e84cf2011-11-04 15:20:08 -0700767 if (!ParseCommandLine(argc, argv, *config) ||
768 !ValidateConfig(*config)) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530769 ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
770 }
771
772 Err1:
773 return ok;
774}
775
776#undef ACTION_IS_NIL
777#undef FEATURETYPE_IS_NIL
778#undef CHECK_NUM_ARGS_LESS
779#undef CHECK_NUM_ARGS_MORE
780
781//------------------------------------------------------------------------------
782// Processing.
783
Urvang Joshia00a3da2012-10-31 17:49:15 -0700784static int GetFrameFragment(const WebPMux* mux,
Urvang Joshi7681bb92013-03-13 18:10:56 -0700785 const WebPMuxConfig* config, int is_frame) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530786 WebPMuxError err = WEBP_MUX_OK;
787 WebPMux* mux_single = NULL;
788 long num = 0;
789 int ok = 1;
Urvang Joshi7681bb92013-03-13 18:10:56 -0700790 const WebPChunkId id = is_frame ? WEBP_CHUNK_ANMF : WEBP_CHUNK_FRGM;
Urvang Joshiab3234a2012-08-23 15:18:51 +0530791 WebPMuxFrameInfo info;
Urvang Joshia0770722012-10-30 14:54:46 -0700792 WebPDataInit(&info.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530793
794 num = strtol(config->feature_.args_[0].params_, NULL, 10);
795 if (num < 0) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700796 ERROR_GOTO1("ERROR: Frame/Fragment index must be non-negative.\n", ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530797 }
798
Urvang Joshid0c79f02012-08-23 16:28:36 +0530799 err = WebPMuxGetFrame(mux, num, &info);
800 if (err == WEBP_MUX_OK && info.id != id) err = WEBP_MUX_NOT_FOUND;
801 if (err != WEBP_MUX_OK) {
802 ERROR_GOTO3("ERROR (%s): Could not get frame %ld.\n",
803 ErrorString(err), num, ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530804 }
805
806 mux_single = WebPMuxNew();
807 if (mux_single == NULL) {
808 err = WEBP_MUX_MEMORY_ERROR;
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530809 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
810 ErrorString(err), ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530811 }
Urvang Joshia0770722012-10-30 14:54:46 -0700812 err = WebPMuxSetImage(mux_single, &info.bitstream, 1);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530813 if (err != WEBP_MUX_OK) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530814 ERROR_GOTO2("ERROR (%s): Could not create single image mux object.\n",
815 ErrorString(err), ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530816 }
Urvang Joshid0c79f02012-08-23 16:28:36 +0530817
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530818 ok = WriteWebP(mux_single, config->output_);
819
820 ErrGet:
Urvang Joshia0770722012-10-30 14:54:46 -0700821 WebPDataClear(&info.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530822 WebPMuxDelete(mux_single);
823 return ok;
824}
825
826// Read and process config.
James Zern04e84cf2011-11-04 15:20:08 -0700827static int Process(const WebPMuxConfig* config) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530828 WebPMux* mux = NULL;
Urvang Joshif903cba2012-10-31 16:30:41 -0700829 WebPData chunk;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530830 WebPMuxError err = WEBP_MUX_OK;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530831 int ok = 1;
James Zern04e84cf2011-11-04 15:20:08 -0700832 const Feature* const feature = &config->feature_;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530833
James Zern04e84cf2011-11-04 15:20:08 -0700834 switch (config->action_type_) {
skal0d19fbf2013-01-21 17:20:14 +0100835 case ACTION_GET: {
James Zern061263a2012-05-11 16:00:57 -0700836 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530837 if (!ok) goto Err2;
James Zern04e84cf2011-11-04 15:20:08 -0700838 switch (feature->type_) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700839 case FEATURE_ANMF:
Urvang Joshi7caab1d2012-11-07 16:04:08 -0800840 case FEATURE_FRGM:
841 ok = GetFrameFragment(mux, config,
842 (feature->type_ == FEATURE_ANMF) ? 1 : 0);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530843 break;
844
845 case FEATURE_ICCP:
Urvang Joshif903cba2012-10-31 16:30:41 -0700846 case FEATURE_EXIF:
847 case FEATURE_XMP:
848 err = WebPMuxGetChunk(mux, kFourccList[feature->type_], &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530849 if (err != WEBP_MUX_OK) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700850 ERROR_GOTO3("ERROR (%s): Could not get the %s.\n",
851 ErrorString(err), kDescriptions[feature->type_], Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530852 }
Urvang Joshif903cba2012-10-31 16:30:41 -0700853 ok = WriteData(config->output_, &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530854 break;
855
856 default:
857 ERROR_GOTO1("ERROR: Invalid feature for action 'get'.\n", Err2);
858 break;
859 }
860 break;
skal0d19fbf2013-01-21 17:20:14 +0100861 }
862 case ACTION_SET: {
James Zern04e84cf2011-11-04 15:20:08 -0700863 switch (feature->type_) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700864 case FEATURE_ANMF: {
skal0d19fbf2013-01-21 17:20:14 +0100865 int i;
Urvang Joshifa30c862012-11-01 15:34:46 -0700866 WebPMuxAnimParams params = { 0xFFFFFFFF, 0 };
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530867 mux = WebPMuxNew();
868 if (mux == NULL) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530869 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
870 ErrorString(WEBP_MUX_MEMORY_ERROR), Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530871 }
skal0d19fbf2013-01-21 17:20:14 +0100872 for (i = 0; i < feature->arg_count_; ++i) {
873 switch (feature->args_[i].subtype_) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700874 case SUBTYPE_BGCOLOR: {
875 uint32_t bgcolor;
skal0d19fbf2013-01-21 17:20:14 +0100876 ok = ParseBgcolorArgs(feature->args_[i].params_, &bgcolor);
Urvang Joshifa30c862012-11-01 15:34:46 -0700877 if (!ok) {
878 ERROR_GOTO1("ERROR: Could not parse the background color \n",
879 Err2);
880 }
881 params.bgcolor = bgcolor;
882 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530883 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700884 case SUBTYPE_LOOP: {
885 const long loop_count =
skal0d19fbf2013-01-21 17:20:14 +0100886 strtol(feature->args_[i].params_, NULL, 10);
Urvang Joshifa30c862012-11-01 15:34:46 -0700887 if (loop_count != (int)loop_count) {
888 // Note: This is only a 'necessary' condition for loop_count
889 // to be valid. The 'sufficient' conditioned in checked in
890 // WebPMuxSetAnimationParams() method called later.
891 ERROR_GOTO1("ERROR: Loop count must be in the range 0 to "
892 "65535.\n", Err2);
893 }
894 params.loop_count = (int)loop_count;
895 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530896 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700897 case SUBTYPE_ANMF: {
898 WebPMuxFrameInfo frame;
899 frame.id = WEBP_CHUNK_ANMF;
skal0d19fbf2013-01-21 17:20:14 +0100900 ok = ReadFileToWebPData(feature->args_[i].filename_,
Urvang Joshifa30c862012-11-01 15:34:46 -0700901 &frame.bitstream);
902 if (!ok) goto Err2;
skal0d19fbf2013-01-21 17:20:14 +0100903 ok = ParseFrameArgs(feature->args_[i].params_, &frame);
Urvang Joshifa30c862012-11-01 15:34:46 -0700904 if (!ok) {
905 WebPDataClear(&frame.bitstream);
906 ERROR_GOTO1("ERROR: Could not parse frame properties.\n",
907 Err2);
908 }
909 err = WebPMuxPushFrame(mux, &frame, 1);
Urvang Joshia0770722012-10-30 14:54:46 -0700910 WebPDataClear(&frame.bitstream);
Urvang Joshifa30c862012-11-01 15:34:46 -0700911 if (err != WEBP_MUX_OK) {
912 ERROR_GOTO3("ERROR (%s): Could not add a frame at index %d."
skal0d19fbf2013-01-21 17:20:14 +0100913 "\n", ErrorString(err), i, Err2);
Urvang Joshifa30c862012-11-01 15:34:46 -0700914 }
915 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530916 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700917 default: {
918 ERROR_GOTO1("ERROR: Invalid subtype for 'frame'", Err2);
919 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530920 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530921 }
922 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700923 err = WebPMuxSetAnimationParams(mux, &params);
924 if (err != WEBP_MUX_OK) {
925 ERROR_GOTO2("ERROR (%s): Could not set animation parameters.\n",
926 ErrorString(err), Err2);
927 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530928 break;
Urvang Joshifa30c862012-11-01 15:34:46 -0700929 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530930
skal0d19fbf2013-01-21 17:20:14 +0100931 case FEATURE_FRGM: {
932 int i;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530933 mux = WebPMuxNew();
934 if (mux == NULL) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530935 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
936 ErrorString(WEBP_MUX_MEMORY_ERROR), Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530937 }
skal0d19fbf2013-01-21 17:20:14 +0100938 for (i = 0; i < feature->arg_count_; ++i) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700939 WebPMuxFrameInfo frgm;
940 frgm.id = WEBP_CHUNK_FRGM;
skal0d19fbf2013-01-21 17:20:14 +0100941 ok = ReadFileToWebPData(feature->args_[i].filename_,
Urvang Joshia00a3da2012-10-31 17:49:15 -0700942 &frgm.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530943 if (!ok) goto Err2;
skal0d19fbf2013-01-21 17:20:14 +0100944 ok = ParseFragmentArgs(feature->args_[i].params_, &frgm);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530945 if (!ok) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700946 WebPDataClear(&frgm.bitstream);
947 ERROR_GOTO1("ERROR: Could not parse fragment properties.\n",
948 Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530949 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700950 err = WebPMuxPushFrame(mux, &frgm, 1);
951 WebPDataClear(&frgm.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530952 if (err != WEBP_MUX_OK) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700953 ERROR_GOTO3("ERROR (%s): Could not add a fragment at index %d.\n",
skal0d19fbf2013-01-21 17:20:14 +0100954 ErrorString(err), i, Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530955 }
956 }
957 break;
skal0d19fbf2013-01-21 17:20:14 +0100958 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530959
960 case FEATURE_ICCP:
Urvang Joshif903cba2012-10-31 16:30:41 -0700961 case FEATURE_EXIF:
skal0d19fbf2013-01-21 17:20:14 +0100962 case FEATURE_XMP: {
James Zern061263a2012-05-11 16:00:57 -0700963 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530964 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700965 ok = ReadFileToWebPData(feature->args_[0].filename_, &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530966 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700967 err = WebPMuxSetChunk(mux, kFourccList[feature->type_], &chunk, 1);
968 free((void*)chunk.bytes);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530969 if (err != WEBP_MUX_OK) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700970 ERROR_GOTO3("ERROR (%s): Could not set the %s.\n",
971 ErrorString(err), kDescriptions[feature->type_], Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530972 }
973 break;
skal0d19fbf2013-01-21 17:20:14 +0100974 }
975 default: {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530976 ERROR_GOTO1("ERROR: Invalid feature for action 'set'.\n", Err2);
977 break;
skal0d19fbf2013-01-21 17:20:14 +0100978 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530979 }
980 ok = WriteWebP(mux, config->output_);
981 break;
skal0d19fbf2013-01-21 17:20:14 +0100982 }
983 case ACTION_STRIP: {
James Zern061263a2012-05-11 16:00:57 -0700984 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530985 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700986 if (feature->type_ == FEATURE_ICCP || feature->type_ == FEATURE_EXIF ||
987 feature->type_ == FEATURE_XMP) {
988 err = WebPMuxDeleteChunk(mux, kFourccList[feature->type_]);
989 if (err != WEBP_MUX_OK) {
990 ERROR_GOTO3("ERROR (%s): Could not strip the %s.\n",
991 ErrorString(err), kDescriptions[feature->type_], Err2);
992 }
993 } else {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530994 ERROR_GOTO1("ERROR: Invalid feature for action 'strip'.\n", Err2);
995 break;
996 }
997 ok = WriteWebP(mux, config->output_);
998 break;
skal0d19fbf2013-01-21 17:20:14 +0100999 }
1000 case ACTION_INFO: {
James Zern061263a2012-05-11 16:00:57 -07001001 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301002 if (!ok) goto Err2;
1003 ok = (DisplayInfo(mux) == WEBP_MUX_OK);
1004 break;
skal0d19fbf2013-01-21 17:20:14 +01001005 }
1006 default: {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301007 assert(0); // Invalid action.
1008 break;
skal0d19fbf2013-01-21 17:20:14 +01001009 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301010 }
1011
1012 Err2:
1013 WebPMuxDelete(mux);
1014 return ok;
1015}
1016
1017//------------------------------------------------------------------------------
1018// Main.
1019
1020int main(int argc, const char* argv[]) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301021 WebPMuxConfig* config;
James Zern974aaff2012-01-24 12:46:46 -08001022 int ok = InitializeConfig(argc - 1, argv + 1, &config);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301023 if (ok) {
James Zern974aaff2012-01-24 12:46:46 -08001024 ok = Process(config);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301025 } else {
1026 PrintHelp();
1027 }
1028 DeleteConfig(config);
James Zern974aaff2012-01-24 12:46:46 -08001029 return !ok;
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301030}
1031
1032//------------------------------------------------------------------------------