blob: 852771be18b5297ad9aa75f3b52fdf846705ad8d [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//
James Zernd6406142013-06-06 23:05:58 -07003// 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.
Urvang Joshia4f32ca2011-09-30 11:07:01 +05308// -----------------------------------------------------------------------------
9//
10// Simple command-line to create a WebP container file and to extract or strip
11// relevant data from the container file.
12//
Urvang Joshia4f32ca2011-09-30 11:07:01 +053013// Authors: Vikas (vikaas.arora@gmail.com),
14// Urvang (urvang@google.com)
15
16/* Usage examples:
17
18 Create container WebP file:
skal48600082012-11-14 06:19:31 +010019 webpmux -frame anim_1.webp +100+10+10 \
20 -frame anim_2.webp +100+25+25+1 \
21 -frame anim_3.webp +100+50+50+1 \
22 -frame anim_4.webp +100 \
23 -loop 10 -bgcolor 128,255,255,255 \
Urvang Joshia4f32ca2011-09-30 11:07:01 +053024 -o out_animation_container.webp
25
26 webpmux -set icc image_profile.icc in.webp -o out_icc_container.webp
Urvang Joshif903cba2012-10-31 16:30:41 -070027 webpmux -set exif image_metadata.exif in.webp -o out_exif_container.webp
28 webpmux -set xmp image_metadata.xmp in.webp -o out_xmp_container.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053029
Urvang Joshia4f32ca2011-09-30 11:07:01 +053030 Extract relevant data from WebP container file:
Urvang Joshia00a3da2012-10-31 17:49:15 -070031 webpmux -get frgm n in.webp -o out_fragment.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053032 webpmux -get frame n in.webp -o out_frame.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053033 webpmux -get icc in.webp -o image_profile.icc
Urvang Joshif903cba2012-10-31 16:30:41 -070034 webpmux -get exif in.webp -o image_metadata.exif
35 webpmux -get xmp in.webp -o image_metadata.xmp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053036
Urvang Joshia4f32ca2011-09-30 11:07:01 +053037 Strip data from WebP Container file:
James Zern04e84cf2011-11-04 15:20:08 -070038 webpmux -strip icc in.webp -o out.webp
Urvang Joshif903cba2012-10-31 16:30:41 -070039 webpmux -strip exif in.webp -o out.webp
40 webpmux -strip xmp in.webp -o out.webp
Urvang Joshia4f32ca2011-09-30 11:07:01 +053041
42 Misc:
Urvang Joshia4f32ca2011-09-30 11:07:01 +053043 webpmux -info in.webp
James Zern04e84cf2011-11-04 15:20:08 -070044 webpmux [ -h | -help ]
Urvang Joshia5042a32013-02-26 14:22:06 -080045 webpmux -version
Urvang Joshia4f32ca2011-09-30 11:07:01 +053046*/
47
Urvang Joshi5dbd4032013-03-15 14:46:12 -070048#ifdef HAVE_CONFIG_H
49#include "config.h"
50#endif
51
Urvang Joshia4f32ca2011-09-30 11:07:01 +053052#include <assert.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
Urvang Joshi0e6747f2013-09-16 15:29:24 -070056#include "webp/decode.h"
Urvang Joshia4f32ca2011-09-30 11:07:01 +053057#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
Urvang Joshid0c79f02012-08-23 16:28:36 +0530149#define RETURN_IF_ERROR3(ERR_MSG, FORMAT_STR1, FORMAT_STR2) \
150 if (err != WEBP_MUX_OK) { \
151 fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
152 return err; \
153 }
154
James Zerna0b27362012-01-27 17:39:47 -0800155#define ERROR_GOTO1(ERR_MSG, LABEL) \
156 do { \
157 fprintf(stderr, ERR_MSG); \
158 ok = 0; \
159 goto LABEL; \
160 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530161
James Zerna0b27362012-01-27 17:39:47 -0800162#define ERROR_GOTO2(ERR_MSG, FORMAT_STR, LABEL) \
163 do { \
164 fprintf(stderr, ERR_MSG, FORMAT_STR); \
165 ok = 0; \
166 goto LABEL; \
167 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530168
James Zerna0b27362012-01-27 17:39:47 -0800169#define ERROR_GOTO3(ERR_MSG, FORMAT_STR1, FORMAT_STR2, LABEL) \
Urvang Joshi6393fe42013-04-26 15:55:42 -0700170 do { \
171 fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
172 ok = 0; \
173 goto LABEL; \
174 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530175
176static WebPMuxError DisplayInfo(const WebPMux* mux) {
Urvang Joshifffefd12013-05-02 13:54:25 -0700177 int width, height;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530178 uint32_t flag;
179
Urvang Joshifffefd12013-05-02 13:54:25 -0700180 WebPMuxError err = WebPMuxGetCanvasSize(mux, &width, &height);
Urvang Joshi54b8e3f2013-09-18 11:44:24 -0700181 assert(err == WEBP_MUX_OK); // As WebPMuxCreate() was successful earlier.
Urvang Joshifffefd12013-05-02 13:54:25 -0700182 printf("Canvas size: %d x %d\n", width, height);
183
184 err = WebPMuxGetFeatures(mux, &flag);
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700185#ifndef WEBP_EXPERIMENTAL_FEATURES
186 if (flag & FRAGMENTS_FLAG) err = WEBP_MUX_INVALID_ARGUMENT;
187#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530188 RETURN_IF_ERROR("Failed to retrieve features\n");
189
190 if (flag == 0) {
191 fprintf(stderr, "No features present.\n");
192 return err;
193 }
194
195 // Print the features present.
James Zern974aaff2012-01-24 12:46:46 -0800196 printf("Features present:");
197 if (flag & ANIMATION_FLAG) printf(" animation");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700198 if (flag & FRAGMENTS_FLAG) printf(" image fragments");
James Zernd8dc72a2013-03-13 14:04:20 -0700199 if (flag & ICCP_FLAG) printf(" ICC profile");
Urvang Joshif903cba2012-10-31 16:30:41 -0700200 if (flag & EXIF_FLAG) printf(" EXIF metadata");
201 if (flag & XMP_FLAG) printf(" XMP metadata");
James Zern974aaff2012-01-24 12:46:46 -0800202 if (flag & ALPHA_FLAG) printf(" transparency");
203 printf("\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530204
Urvang Joshia00a3da2012-10-31 17:49:15 -0700205 if ((flag & ANIMATION_FLAG) || (flag & FRAGMENTS_FLAG)) {
Urvang Joshid0c79f02012-08-23 16:28:36 +0530206 const int is_anim = !!(flag & ANIMATION_FLAG);
Urvang Joshi92f80592012-10-30 12:14:10 -0700207 const WebPChunkId id = is_anim ? WEBP_CHUNK_ANMF : WEBP_CHUNK_FRGM;
Urvang Joshia00a3da2012-10-31 17:49:15 -0700208 const char* const type_str = is_anim ? "frame" : "fragment";
James Zerneec4b872012-01-07 12:44:01 -0800209 int nFrames;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530210
Urvang Joshid0c79f02012-08-23 16:28:36 +0530211 if (is_anim) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700212 WebPMuxAnimParams params;
213 err = WebPMuxGetAnimationParams(mux, &params);
Urvang Joshi54b8e3f2013-09-18 11:44:24 -0700214 assert(err == WEBP_MUX_OK);
Urvang Joshifa30c862012-11-01 15:34:46 -0700215 printf("Background color : 0x%.8X Loop Count : %d\n",
216 params.bgcolor, params.loop_count);
Urvang Joshid0c79f02012-08-23 16:28:36 +0530217 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530218
Urvang Joshid0c79f02012-08-23 16:28:36 +0530219 err = WebPMuxNumChunks(mux, id, &nFrames);
Urvang Joshi54b8e3f2013-09-18 11:44:24 -0700220 assert(err == WEBP_MUX_OK);
Urvang Joshid0c79f02012-08-23 16:28:36 +0530221
222 printf("Number of %ss: %d\n", type_str, nFrames);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530223 if (nFrames > 0) {
224 int i;
Urvang Joshi0e6747f2013-09-16 15:29:24 -0700225 printf("No.: width height alpha x_offset y_offset ");
226 if (is_anim) printf("duration dispose blend ");
Urvang Joshid0c79f02012-08-23 16:28:36 +0530227 printf("image_size\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530228 for (i = 1; i <= nFrames; i++) {
Urvang Joshiab3234a2012-08-23 15:18:51 +0530229 WebPMuxFrameInfo frame;
230 err = WebPMuxGetFrame(mux, i, &frame);
skal3e59a742013-05-22 00:58:53 +0200231 if (err == WEBP_MUX_OK) {
Urvang Joshi0e6747f2013-09-16 15:29:24 -0700232 WebPBitstreamFeatures features;
233 const VP8StatusCode status = WebPGetFeatures(
234 frame.bitstream.bytes, frame.bitstream.size, &features);
235 assert(status == VP8_STATUS_OK); // Checked by WebPMuxCreate().
236 (void)status;
237 printf("%3d: %5d %5d %5s %8d %8d ", i, features.width,
238 features.height, features.has_alpha ? "yes" : "no",
239 frame.x_offset, frame.y_offset);
Urvang Joshie81fac82013-08-26 18:04:52 -0700240 if (is_anim) {
Urvang Joshi0e6747f2013-09-16 15:29:24 -0700241 const char* const dispose =
242 (frame.dispose_method == WEBP_MUX_DISPOSE_NONE) ? "none"
243 : "background";
244 const char* const blend =
245 (frame.blend_method == WEBP_MUX_BLEND) ? "yes" : "no";
246 printf("%8d %10s %5s ", frame.duration, dispose, blend);
Urvang Joshie81fac82013-08-26 18:04:52 -0700247 }
skal3e59a742013-05-22 00:58:53 +0200248 printf("%10d\n", (int)frame.bitstream.size);
249 }
Urvang Joshia0770722012-10-30 14:54:46 -0700250 WebPDataClear(&frame.bitstream);
skal3e59a742013-05-22 00:58:53 +0200251 RETURN_IF_ERROR3("Failed to retrieve %s#%d\n", type_str, i);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530252 }
253 }
254 }
255
256 if (flag & ICCP_FLAG) {
James Zerneec4b872012-01-07 12:44:01 -0800257 WebPData icc_profile;
Urvang Joshi1c04a0d2012-08-23 15:28:20 +0530258 err = WebPMuxGetChunk(mux, "ICCP", &icc_profile);
Urvang Joshi54b8e3f2013-09-18 11:44:24 -0700259 assert(err == WEBP_MUX_OK);
James Zern14d42af2013-03-20 16:59:35 -0700260 printf("Size of the ICC profile data: %d\n", (int)icc_profile.size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530261 }
262
Urvang Joshif903cba2012-10-31 16:30:41 -0700263 if (flag & EXIF_FLAG) {
264 WebPData exif;
265 err = WebPMuxGetChunk(mux, "EXIF", &exif);
Urvang Joshi54b8e3f2013-09-18 11:44:24 -0700266 assert(err == WEBP_MUX_OK);
James Zern14d42af2013-03-20 16:59:35 -0700267 printf("Size of the EXIF metadata: %d\n", (int)exif.size);
Urvang Joshif903cba2012-10-31 16:30:41 -0700268 }
269
270 if (flag & XMP_FLAG) {
271 WebPData xmp;
272 err = WebPMuxGetChunk(mux, "XMP ", &xmp);
Urvang Joshi54b8e3f2013-09-18 11:44:24 -0700273 assert(err == WEBP_MUX_OK);
James Zern14d42af2013-03-20 16:59:35 -0700274 printf("Size of the XMP metadata: %d\n", (int)xmp.size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530275 }
276
Urvang Joshia00a3da2012-10-31 17:49:15 -0700277 if ((flag & ALPHA_FLAG) && !(flag & (ANIMATION_FLAG | FRAGMENTS_FLAG))) {
Urvang Joshid0c79f02012-08-23 16:28:36 +0530278 WebPMuxFrameInfo image;
279 err = WebPMuxGetFrame(mux, 1, &image);
skal3e59a742013-05-22 00:58:53 +0200280 if (err == WEBP_MUX_OK) {
281 printf("Size of the image (with alpha): %d\n", (int)image.bitstream.size);
282 }
283 WebPDataClear(&image.bitstream);
Urvang Joshicdf97aa2011-12-01 16:11:51 +0530284 RETURN_IF_ERROR("Failed to retrieve the image\n");
Urvang Joshicdf97aa2011-12-01 16:11:51 +0530285 }
286
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530287 return WEBP_MUX_OK;
288}
289
Pascal Massiminoabd030b2011-11-01 06:24:34 -0700290static void PrintHelp(void) {
James Zern974aaff2012-01-24 12:46:46 -0800291 printf("Usage: webpmux -get GET_OPTIONS INPUT -o OUTPUT\n");
292 printf(" webpmux -set SET_OPTIONS INPUT -o OUTPUT\n");
293 printf(" webpmux -strip STRIP_OPTIONS INPUT -o OUTPUT\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700294#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700295 printf(" webpmux -frgm FRAGMENT_OPTIONS [-frgm...] -o OUTPUT\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700296#endif
Urvang Joshifa30c862012-11-01 15:34:46 -0700297 printf(" webpmux -frame FRAME_OPTIONS [-frame...] [-loop LOOP_COUNT]"
298 "\n");
299 printf(" [-bgcolor BACKGROUND_COLOR] -o OUTPUT\n");
James Zern974aaff2012-01-24 12:46:46 -0800300 printf(" webpmux -info INPUT\n");
301 printf(" webpmux [-h|-help]\n");
Urvang Joshia5042a32013-02-26 14:22:06 -0800302 printf(" webpmux -version\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530303
James Zern974aaff2012-01-24 12:46:46 -0800304 printf("\n");
305 printf("GET_OPTIONS:\n");
306 printf(" Extract relevant data.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700307 printf(" icc Get ICC profile.\n");
308 printf(" exif Get EXIF metadata.\n");
309 printf(" xmp Get XMP metadata.\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700310#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700311 printf(" frgm n Get nth fragment.\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700312#endif
James Zern974aaff2012-01-24 12:46:46 -0800313 printf(" frame n Get nth frame.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530314
James Zern974aaff2012-01-24 12:46:46 -0800315 printf("\n");
316 printf("SET_OPTIONS:\n");
317 printf(" Set color profile/metadata.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700318 printf(" icc file.icc Set ICC profile.\n");
319 printf(" exif file.exif Set EXIF metadata.\n");
320 printf(" xmp file.xmp Set XMP metadata.\n");
321 printf(" where: 'file.icc' contains the ICC profile to be set,\n");
322 printf(" 'file.exif' contains the EXIF metadata to be set\n");
323 printf(" 'file.xmp' contains the XMP metadata to be set\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530324
James Zern974aaff2012-01-24 12:46:46 -0800325 printf("\n");
326 printf("STRIP_OPTIONS:\n");
327 printf(" Strip color profile/metadata.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700328 printf(" icc Strip ICC profile.\n");
329 printf(" exif Strip EXIF metadata.\n");
330 printf(" xmp Strip XMP metadata.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530331
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700332#ifdef WEBP_EXPERIMENTAL_FEATURES
James Zern974aaff2012-01-24 12:46:46 -0800333 printf("\n");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700334 printf("FRAGMENT_OPTIONS(i):\n");
335 printf(" Create fragmented image.\n");
James Zern974aaff2012-01-24 12:46:46 -0800336 printf(" file_i +xi+yi\n");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700337 printf(" where: 'file_i' is the i'th fragment (WebP format),\n");
338 printf(" 'xi','yi' specify the image offset for this fragment."
339 "\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700340#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530341
James Zern974aaff2012-01-24 12:46:46 -0800342 printf("\n");
343 printf("FRAME_OPTIONS(i):\n");
344 printf(" Create animation.\n");
Urvang Joshie81fac82013-08-26 18:04:52 -0700345 printf(" file_i +di+[xi+yi[+mi[bi]]]\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700346 printf(" where: 'file_i' is the i'th animation frame (WebP format),\n");
James Zern974aaff2012-01-24 12:46:46 -0800347 printf(" 'di' is the pause duration before next frame.\n");
James Zern8fab1612013-03-07 19:15:37 -0800348 printf(" 'xi','yi' specify the image offset for this frame.\n");
Urvang Joshifa30c862012-11-01 15:34:46 -0700349 printf(" 'mi' is the dispose method for this frame (0 or 1).\n");
Urvang Joshie81fac82013-08-26 18:04:52 -0700350 printf(" 'bi' is the blending method for this frame (+b or -b)."
351 "\n");
Urvang Joshifa30c862012-11-01 15:34:46 -0700352
353 printf("\n");
354 printf("LOOP_COUNT:\n");
355 printf(" Number of times to repeat the animation.\n");
356 printf(" Valid range is 0 to 65535 [Default: 0 (infinite)].\n");
357
358 printf("\n");
359 printf("BACKGROUND_COLOR:\n");
360 printf(" Background color of the canvas.\n");
361 printf(" A,R,G,B\n");
362 printf(" where: 'A', 'R', 'G' and 'B' are integers in the range 0 to 255 "
363 "specifying\n");
364 printf(" the Alpha, Red, Green and Blue component values "
365 "respectively\n");
366 printf(" [Default: 255,255,255,255].\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530367
Urvang Joshif903cba2012-10-31 16:30:41 -0700368 printf("\nINPUT & OUTPUT are in WebP format.\n");
369
Urvang Joshifa30c862012-11-01 15:34:46 -0700370 printf("\nNote: The nature of EXIF, XMP and ICC data is not checked");
371 printf(" and is assumed to be\nvalid.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530372}
373
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530374static int ReadFileToWebPData(const char* const filename,
375 WebPData* const webp_data) {
376 const uint8_t* data;
377 size_t size;
378 if (!ExUtilReadFile(filename, &data, &size)) return 0;
Urvang Joshia0770722012-10-30 14:54:46 -0700379 webp_data->bytes = data;
380 webp_data->size = size;
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530381 return 1;
382}
383
James Zern061263a2012-05-11 16:00:57 -0700384static int CreateMux(const char* const filename, WebPMux** mux) {
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530385 WebPData bitstream;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530386 assert(mux != NULL);
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530387 if (!ReadFileToWebPData(filename, &bitstream)) return 0;
Urvang Joshi6d5c7972012-06-07 13:45:06 +0530388 *mux = WebPMuxCreate(&bitstream, 1);
Urvang Joshia0770722012-10-30 14:54:46 -0700389 free((void*)bitstream.bytes);
Urvang Joshi6d5c7972012-06-07 13:45:06 +0530390 if (*mux != NULL) return 1;
391 fprintf(stderr, "Failed to create mux object from file %s.\n", filename);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530392 return 0;
393}
394
James Zern0f7820e2012-01-24 14:08:27 -0800395static int WriteData(const char* filename, const WebPData* const webpdata) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530396 int ok = 0;
James Zern04e84cf2011-11-04 15:20:08 -0700397 FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530398 if (!fout) {
399 fprintf(stderr, "Error opening output WebP file %s!\n", filename);
400 return 0;
401 }
Urvang Joshia0770722012-10-30 14:54:46 -0700402 if (fwrite(webpdata->bytes, webpdata->size, 1, fout) != 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530403 fprintf(stderr, "Error writing file %s!\n", filename);
404 } else {
James Zern14d42af2013-03-20 16:59:35 -0700405 fprintf(stderr, "Saved file %s (%d bytes)\n",
406 filename, (int)webpdata->size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530407 ok = 1;
408 }
409 if (fout != stdout) fclose(fout);
410 return ok;
411}
412
413static int WriteWebP(WebPMux* const mux, const char* filename) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530414 int ok;
Urvang Joshif1df5582012-06-07 11:04:57 +0530415 WebPData webp_data;
416 const WebPMuxError err = WebPMuxAssemble(mux, &webp_data);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530417 if (err != WEBP_MUX_OK) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530418 fprintf(stderr, "Error (%s) assembling the WebP file.\n", ErrorString(err));
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530419 return 0;
420 }
Urvang Joshif1df5582012-06-07 11:04:57 +0530421 ok = WriteData(filename, &webp_data);
422 WebPDataClear(&webp_data);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530423 return ok;
424}
425
Urvang Joshiab3234a2012-08-23 15:18:51 +0530426static int ParseFrameArgs(const char* args, WebPMuxFrameInfo* const info) {
skal48600082012-11-14 06:19:31 +0100427 int dispose_method, dummy;
Urvang Joshie81fac82013-08-26 18:04:52 -0700428 char plus_minus, blend_method;
429 const int num_args = sscanf(args, "+%d+%d+%d+%d%c%c+%d", &info->duration,
430 &info->x_offset, &info->y_offset, &dispose_method,
431 &plus_minus, &blend_method, &dummy);
skal48600082012-11-14 06:19:31 +0100432 switch (num_args) {
433 case 1:
434 info->x_offset = info->y_offset = 0; // fall through
435 case 3:
436 dispose_method = 0; // fall through
437 case 4:
Urvang Joshie81fac82013-08-26 18:04:52 -0700438 plus_minus = '+';
439 blend_method = 'b'; // fall through
440 case 6:
skal48600082012-11-14 06:19:31 +0100441 break;
Urvang Joshie81fac82013-08-26 18:04:52 -0700442 case 2:
443 case 5:
skal48600082012-11-14 06:19:31 +0100444 default:
445 return 0;
Urvang Joshifa30c862012-11-01 15:34:46 -0700446 }
447 // Note: The sanity of the following conversion is checked by
Urvang Joshie81fac82013-08-26 18:04:52 -0700448 // WebPMuxPushFrame().
Urvang Joshifa30c862012-11-01 15:34:46 -0700449 info->dispose_method = (WebPMuxAnimDispose)dispose_method;
Urvang Joshie81fac82013-08-26 18:04:52 -0700450
451 if (blend_method != 'b') return 0;
452 if (plus_minus != '-' && plus_minus != '+') return 0;
453 info->blend_method =
454 (plus_minus == '+') ? WEBP_MUX_BLEND : WEBP_MUX_NO_BLEND;
Urvang Joshifa30c862012-11-01 15:34:46 -0700455 return 1;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530456}
457
Urvang Joshia00a3da2012-10-31 17:49:15 -0700458static int ParseFragmentArgs(const char* args, WebPMuxFrameInfo* const info) {
Urvang Joshia0770722012-10-30 14:54:46 -0700459 return (sscanf(args, "+%d+%d", &info->x_offset, &info->y_offset) == 2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530460}
461
Urvang Joshifa30c862012-11-01 15:34:46 -0700462static int ParseBgcolorArgs(const char* args, uint32_t* const bgcolor) {
463 uint32_t a, r, g, b;
464 if (sscanf(args, "%u,%u,%u,%u", &a, &r, &g, &b) != 4) return 0;
465 if (a >= 256 || r >= 256 || g >= 256 || b >= 256) return 0;
466 *bgcolor = (a << 24) | (r << 16) | (g << 8) | (b << 0);
467 return 1;
468}
469
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530470//------------------------------------------------------------------------------
471// Clean-up.
472
473static void DeleteConfig(WebPMuxConfig* config) {
474 if (config != NULL) {
475 free(config->feature_.args_);
476 free(config);
477 }
478}
479
480//------------------------------------------------------------------------------
481// Parsing.
482
483// Basic syntactic checks on the command-line arguments.
484// Returns 1 on valid, 0 otherwise.
485// Also fills up num_feature_args to be number of feature arguments given.
486// (e.g. if there are 4 '-frame's and 1 '-loop', then num_feature_args = 5).
487static int ValidateCommandLine(int argc, const char* argv[],
488 int* num_feature_args) {
489 int num_frame_args;
Urvang Joshia00a3da2012-10-31 17:49:15 -0700490 int num_frgm_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530491 int num_loop_args;
Urvang Joshifa30c862012-11-01 15:34:46 -0700492 int num_bgcolor_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530493 int ok = 1;
494
495 assert(num_feature_args != NULL);
496 *num_feature_args = 0;
497
498 // Simple checks.
James Zern04e84cf2011-11-04 15:20:08 -0700499 if (CountOccurrences(argv, argc, "-get") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530500 ERROR_GOTO1("ERROR: Multiple '-get' arguments specified.\n", ErrValidate);
501 }
James Zern04e84cf2011-11-04 15:20:08 -0700502 if (CountOccurrences(argv, argc, "-set") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530503 ERROR_GOTO1("ERROR: Multiple '-set' arguments specified.\n", ErrValidate);
504 }
James Zern04e84cf2011-11-04 15:20:08 -0700505 if (CountOccurrences(argv, argc, "-strip") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530506 ERROR_GOTO1("ERROR: Multiple '-strip' arguments specified.\n", ErrValidate);
507 }
James Zern04e84cf2011-11-04 15:20:08 -0700508 if (CountOccurrences(argv, argc, "-info") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530509 ERROR_GOTO1("ERROR: Multiple '-info' arguments specified.\n", ErrValidate);
510 }
James Zern04e84cf2011-11-04 15:20:08 -0700511 if (CountOccurrences(argv, argc, "-o") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530512 ERROR_GOTO1("ERROR: Multiple output files specified.\n", ErrValidate);
513 }
514
515 // Compound checks.
James Zern04e84cf2011-11-04 15:20:08 -0700516 num_frame_args = CountOccurrences(argv, argc, "-frame");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700517 num_frgm_args = CountOccurrences(argv, argc, "-frgm");
James Zern04e84cf2011-11-04 15:20:08 -0700518 num_loop_args = CountOccurrences(argv, argc, "-loop");
Urvang Joshifa30c862012-11-01 15:34:46 -0700519 num_bgcolor_args = CountOccurrences(argv, argc, "-bgcolor");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530520
521 if (num_loop_args > 1) {
522 ERROR_GOTO1("ERROR: Multiple loop counts specified.\n", ErrValidate);
523 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700524 if (num_bgcolor_args > 1) {
525 ERROR_GOTO1("ERROR: Multiple background colors specified.\n", ErrValidate);
526 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530527
Urvang Joshifa30c862012-11-01 15:34:46 -0700528 if ((num_frame_args == 0) && (num_loop_args + num_bgcolor_args > 0)) {
529 ERROR_GOTO1("ERROR: Loop count and background color are relevant only in "
530 "case of animation.\n", ErrValidate);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530531 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700532 if (num_frame_args > 0 && num_frgm_args > 0) {
533 ERROR_GOTO1("ERROR: Only one of frames & fragments can be specified at a "
534 "time.\n", ErrValidate);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530535 }
536
537 assert(ok == 1);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700538 if (num_frame_args == 0 && num_frgm_args == 0) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700539 // Single argument ('set' action for ICCP/EXIF/XMP, OR a 'get' action).
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530540 *num_feature_args = 1;
541 } else {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700542 // Multiple arguments ('set' action for animation or fragmented image).
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530543 if (num_frame_args > 0) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700544 *num_feature_args = num_frame_args + num_loop_args + num_bgcolor_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530545 } else {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700546 *num_feature_args = num_frgm_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530547 }
548 }
549
550 ErrValidate:
551 return ok;
552}
553
554#define ACTION_IS_NIL (config->action_type_ == NIL_ACTION)
555
556#define FEATURETYPE_IS_NIL (feature->type_ == NIL_FEATURE)
557
558#define CHECK_NUM_ARGS_LESS(NUM, LABEL) \
559 if (argc < i + (NUM)) { \
560 fprintf(stderr, "ERROR: Too few arguments for '%s'.\n", argv[i]); \
561 goto LABEL; \
562 }
563
564#define CHECK_NUM_ARGS_NOT_EQUAL(NUM, LABEL) \
565 if (argc != i + (NUM)) { \
566 fprintf(stderr, "ERROR: Too many arguments for '%s'.\n", argv[i]); \
567 goto LABEL; \
568 }
569
570// Parses command-line arguments to fill up config object. Also performs some
571// semantic checks.
572static int ParseCommandLine(int argc, const char* argv[],
573 WebPMuxConfig* config) {
574 int i = 0;
575 int feature_arg_index = 0;
576 int ok = 1;
577
578 while (i < argc) {
579 Feature* const feature = &config->feature_;
James Zern04e84cf2011-11-04 15:20:08 -0700580 FeatureArg* const arg = &feature->args_[feature_arg_index];
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530581 if (argv[i][0] == '-') { // One of the action types or output.
582 if (!strcmp(argv[i], "-set")) {
583 if (ACTION_IS_NIL) {
584 config->action_type_ = ACTION_SET;
585 } else {
586 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
587 }
588 ++i;
589 } else if (!strcmp(argv[i], "-get")) {
590 if (ACTION_IS_NIL) {
591 config->action_type_ = ACTION_GET;
592 } else {
593 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
594 }
595 ++i;
596 } else if (!strcmp(argv[i], "-strip")) {
597 if (ACTION_IS_NIL) {
598 config->action_type_ = ACTION_STRIP;
599 feature->arg_count_ = 0;
600 } else {
601 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
602 }
603 ++i;
604 } else if (!strcmp(argv[i], "-frame")) {
605 CHECK_NUM_ARGS_LESS(3, ErrParse);
606 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
607 config->action_type_ = ACTION_SET;
608 } else {
609 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
610 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700611 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_ANMF) {
612 feature->type_ = FEATURE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530613 } else {
614 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
615 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700616 arg->subtype_ = SUBTYPE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530617 arg->filename_ = argv[i + 1];
618 arg->params_ = argv[i + 2];
619 ++feature_arg_index;
620 i += 3;
Urvang Joshifa30c862012-11-01 15:34:46 -0700621 } else if (!strcmp(argv[i], "-loop") || !strcmp(argv[i], "-bgcolor")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530622 CHECK_NUM_ARGS_LESS(2, ErrParse);
623 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
624 config->action_type_ = ACTION_SET;
625 } else {
626 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
627 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700628 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_ANMF) {
629 feature->type_ = FEATURE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530630 } else {
631 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
632 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700633 arg->subtype_ =
634 !strcmp(argv[i], "-loop") ? SUBTYPE_LOOP : SUBTYPE_BGCOLOR;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530635 arg->params_ = argv[i + 1];
636 ++feature_arg_index;
637 i += 2;
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700638#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700639 } else if (!strcmp(argv[i], "-frgm")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530640 CHECK_NUM_ARGS_LESS(3, ErrParse);
641 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
642 config->action_type_ = ACTION_SET;
643 } else {
644 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
645 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700646 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_FRGM) {
647 feature->type_ = FEATURE_FRGM;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530648 } else {
649 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
650 }
651 arg->filename_ = argv[i + 1];
652 arg->params_ = argv[i + 2];
653 ++feature_arg_index;
654 i += 3;
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700655#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530656 } else if (!strcmp(argv[i], "-o")) {
657 CHECK_NUM_ARGS_LESS(2, ErrParse);
658 config->output_ = argv[i + 1];
659 i += 2;
660 } else if (!strcmp(argv[i], "-info")) {
661 CHECK_NUM_ARGS_NOT_EQUAL(2, ErrParse);
662 if (config->action_type_ != NIL_ACTION) {
663 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
664 } else {
665 config->action_type_ = ACTION_INFO;
666 feature->arg_count_ = 0;
667 config->input_ = argv[i + 1];
668 }
669 i += 2;
670 } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help")) {
671 PrintHelp();
672 DeleteConfig(config);
James Zern974aaff2012-01-24 12:46:46 -0800673 exit(0);
Urvang Joshia5042a32013-02-26 14:22:06 -0800674 } else if (!strcmp(argv[i], "-version")) {
675 const int version = WebPGetMuxVersion();
676 printf("%d.%d.%d\n",
677 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
678 DeleteConfig(config);
679 exit(0);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530680 } else {
681 ERROR_GOTO2("ERROR: Unknown option: '%s'.\n", argv[i], ErrParse);
682 }
683 } else { // One of the feature types or input.
684 if (ACTION_IS_NIL) {
685 ERROR_GOTO1("ERROR: Action must be specified before other arguments.\n",
686 ErrParse);
687 }
Urvang Joshif903cba2012-10-31 16:30:41 -0700688 if (!strcmp(argv[i], "icc") || !strcmp(argv[i], "exif") ||
689 !strcmp(argv[i], "xmp")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530690 if (FEATURETYPE_IS_NIL) {
691 feature->type_ = (!strcmp(argv[i], "icc")) ? FEATURE_ICCP :
Urvang Joshif903cba2012-10-31 16:30:41 -0700692 (!strcmp(argv[i], "exif")) ? FEATURE_EXIF : FEATURE_XMP;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530693 } else {
694 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
695 }
696 if (config->action_type_ == ACTION_SET) {
697 CHECK_NUM_ARGS_LESS(2, ErrParse);
698 arg->filename_ = argv[i + 1];
699 ++feature_arg_index;
700 i += 2;
701 } else {
702 ++i;
703 }
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700704#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530705 } else if ((!strcmp(argv[i], "frame") ||
Urvang Joshia00a3da2012-10-31 17:49:15 -0700706 !strcmp(argv[i], "frgm")) &&
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700707#else
708 } else if (!strcmp(argv[i], "frame") &&
709#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530710 (config->action_type_ == ACTION_GET)) {
711 CHECK_NUM_ARGS_LESS(2, ErrParse);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700712 feature->type_ = (!strcmp(argv[i], "frame")) ? FEATURE_ANMF :
713 FEATURE_FRGM;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530714 arg->params_ = argv[i + 1];
715 ++feature_arg_index;
716 i += 2;
James Zern04e84cf2011-11-04 15:20:08 -0700717 } else { // Assume input file.
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530718 if (config->input_ == NULL) {
719 config->input_ = argv[i];
720 } else {
721 ERROR_GOTO2("ERROR at '%s': Multiple input files specified.\n",
722 argv[i], ErrParse);
723 }
724 ++i;
725 }
726 }
727 }
728 ErrParse:
729 return ok;
730}
731
James Zern04e84cf2011-11-04 15:20:08 -0700732// Additional checks after config is filled.
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530733static int ValidateConfig(WebPMuxConfig* config) {
734 int ok = 1;
735 Feature* const feature = &config->feature_;
736
737 // Action.
738 if (ACTION_IS_NIL) {
739 ERROR_GOTO1("ERROR: No action specified.\n", ErrValidate2);
740 }
741
742 // Feature type.
743 if (FEATURETYPE_IS_NIL && config->action_type_ != ACTION_INFO) {
744 ERROR_GOTO1("ERROR: No feature specified.\n", ErrValidate2);
745 }
746
747 // Input file.
748 if (config->input_ == NULL) {
749 if (config->action_type_ != ACTION_SET) {
750 ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700751 } else if (feature->type_ != FEATURE_ANMF &&
752 feature->type_ != FEATURE_FRGM) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530753 ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2);
754 }
755 }
756
757 // Output file.
758 if (config->output_ == NULL && config->action_type_ != ACTION_INFO) {
759 ERROR_GOTO1("ERROR: No output file specified.\n", ErrValidate2);
760 }
761
762 ErrValidate2:
763 return ok;
764}
765
766// Create config object from command-line arguments.
767static int InitializeConfig(int argc, const char* argv[],
768 WebPMuxConfig** config) {
769 int num_feature_args = 0;
770 int ok = 1;
771
772 assert(config != NULL);
773 *config = NULL;
774
775 // Validate command-line arguments.
776 if (!ValidateCommandLine(argc, argv, &num_feature_args)) {
777 ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
778 }
779
780 // Allocate memory.
781 *config = (WebPMuxConfig*)calloc(1, sizeof(**config));
782 if (*config == NULL) {
783 ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1);
784 }
785 (*config)->feature_.arg_count_ = num_feature_args;
786 (*config)->feature_.args_ =
787 (FeatureArg*)calloc(num_feature_args, sizeof(FeatureArg));
788 if ((*config)->feature_.args_ == NULL) {
789 ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1);
790 }
791
792 // Parse command-line.
James Zern04e84cf2011-11-04 15:20:08 -0700793 if (!ParseCommandLine(argc, argv, *config) ||
794 !ValidateConfig(*config)) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530795 ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
796 }
797
798 Err1:
799 return ok;
800}
801
802#undef ACTION_IS_NIL
803#undef FEATURETYPE_IS_NIL
804#undef CHECK_NUM_ARGS_LESS
805#undef CHECK_NUM_ARGS_MORE
806
807//------------------------------------------------------------------------------
808// Processing.
809
Urvang Joshia00a3da2012-10-31 17:49:15 -0700810static int GetFrameFragment(const WebPMux* mux,
Urvang Joshi7681bb92013-03-13 18:10:56 -0700811 const WebPMuxConfig* config, int is_frame) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530812 WebPMuxError err = WEBP_MUX_OK;
813 WebPMux* mux_single = NULL;
814 long num = 0;
815 int ok = 1;
Urvang Joshi7681bb92013-03-13 18:10:56 -0700816 const WebPChunkId id = is_frame ? WEBP_CHUNK_ANMF : WEBP_CHUNK_FRGM;
Urvang Joshiab3234a2012-08-23 15:18:51 +0530817 WebPMuxFrameInfo info;
Urvang Joshia0770722012-10-30 14:54:46 -0700818 WebPDataInit(&info.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530819
820 num = strtol(config->feature_.args_[0].params_, NULL, 10);
821 if (num < 0) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700822 ERROR_GOTO1("ERROR: Frame/Fragment index must be non-negative.\n", ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530823 }
824
Urvang Joshid0c79f02012-08-23 16:28:36 +0530825 err = WebPMuxGetFrame(mux, num, &info);
826 if (err == WEBP_MUX_OK && info.id != id) err = WEBP_MUX_NOT_FOUND;
827 if (err != WEBP_MUX_OK) {
828 ERROR_GOTO3("ERROR (%s): Could not get frame %ld.\n",
829 ErrorString(err), num, ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530830 }
831
832 mux_single = WebPMuxNew();
833 if (mux_single == NULL) {
834 err = WEBP_MUX_MEMORY_ERROR;
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530835 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
836 ErrorString(err), ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530837 }
Urvang Joshia0770722012-10-30 14:54:46 -0700838 err = WebPMuxSetImage(mux_single, &info.bitstream, 1);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530839 if (err != WEBP_MUX_OK) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530840 ERROR_GOTO2("ERROR (%s): Could not create single image mux object.\n",
841 ErrorString(err), ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530842 }
Urvang Joshid0c79f02012-08-23 16:28:36 +0530843
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530844 ok = WriteWebP(mux_single, config->output_);
845
846 ErrGet:
Urvang Joshia0770722012-10-30 14:54:46 -0700847 WebPDataClear(&info.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530848 WebPMuxDelete(mux_single);
849 return ok;
850}
851
852// Read and process config.
James Zern04e84cf2011-11-04 15:20:08 -0700853static int Process(const WebPMuxConfig* config) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530854 WebPMux* mux = NULL;
Urvang Joshif903cba2012-10-31 16:30:41 -0700855 WebPData chunk;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530856 WebPMuxError err = WEBP_MUX_OK;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530857 int ok = 1;
James Zern04e84cf2011-11-04 15:20:08 -0700858 const Feature* const feature = &config->feature_;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530859
James Zern04e84cf2011-11-04 15:20:08 -0700860 switch (config->action_type_) {
skal0d19fbf2013-01-21 17:20:14 +0100861 case ACTION_GET: {
James Zern061263a2012-05-11 16:00:57 -0700862 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530863 if (!ok) goto Err2;
James Zern04e84cf2011-11-04 15:20:08 -0700864 switch (feature->type_) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700865 case FEATURE_ANMF:
Urvang Joshi7caab1d2012-11-07 16:04:08 -0800866 case FEATURE_FRGM:
867 ok = GetFrameFragment(mux, config,
868 (feature->type_ == FEATURE_ANMF) ? 1 : 0);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530869 break;
870
871 case FEATURE_ICCP:
Urvang Joshif903cba2012-10-31 16:30:41 -0700872 case FEATURE_EXIF:
873 case FEATURE_XMP:
874 err = WebPMuxGetChunk(mux, kFourccList[feature->type_], &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530875 if (err != WEBP_MUX_OK) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700876 ERROR_GOTO3("ERROR (%s): Could not get the %s.\n",
877 ErrorString(err), kDescriptions[feature->type_], Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530878 }
Urvang Joshif903cba2012-10-31 16:30:41 -0700879 ok = WriteData(config->output_, &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530880 break;
881
882 default:
883 ERROR_GOTO1("ERROR: Invalid feature for action 'get'.\n", Err2);
884 break;
885 }
886 break;
skal0d19fbf2013-01-21 17:20:14 +0100887 }
888 case ACTION_SET: {
James Zern04e84cf2011-11-04 15:20:08 -0700889 switch (feature->type_) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700890 case FEATURE_ANMF: {
skal0d19fbf2013-01-21 17:20:14 +0100891 int i;
Urvang Joshifa30c862012-11-01 15:34:46 -0700892 WebPMuxAnimParams params = { 0xFFFFFFFF, 0 };
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530893 mux = WebPMuxNew();
894 if (mux == NULL) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530895 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
896 ErrorString(WEBP_MUX_MEMORY_ERROR), Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530897 }
skal0d19fbf2013-01-21 17:20:14 +0100898 for (i = 0; i < feature->arg_count_; ++i) {
899 switch (feature->args_[i].subtype_) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700900 case SUBTYPE_BGCOLOR: {
901 uint32_t bgcolor;
skal0d19fbf2013-01-21 17:20:14 +0100902 ok = ParseBgcolorArgs(feature->args_[i].params_, &bgcolor);
Urvang Joshifa30c862012-11-01 15:34:46 -0700903 if (!ok) {
904 ERROR_GOTO1("ERROR: Could not parse the background color \n",
905 Err2);
906 }
907 params.bgcolor = bgcolor;
908 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530909 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700910 case SUBTYPE_LOOP: {
911 const long loop_count =
skal0d19fbf2013-01-21 17:20:14 +0100912 strtol(feature->args_[i].params_, NULL, 10);
Urvang Joshifa30c862012-11-01 15:34:46 -0700913 if (loop_count != (int)loop_count) {
914 // Note: This is only a 'necessary' condition for loop_count
915 // to be valid. The 'sufficient' conditioned in checked in
916 // WebPMuxSetAnimationParams() method called later.
917 ERROR_GOTO1("ERROR: Loop count must be in the range 0 to "
918 "65535.\n", Err2);
919 }
920 params.loop_count = (int)loop_count;
921 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530922 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700923 case SUBTYPE_ANMF: {
924 WebPMuxFrameInfo frame;
925 frame.id = WEBP_CHUNK_ANMF;
skal0d19fbf2013-01-21 17:20:14 +0100926 ok = ReadFileToWebPData(feature->args_[i].filename_,
Urvang Joshifa30c862012-11-01 15:34:46 -0700927 &frame.bitstream);
928 if (!ok) goto Err2;
skal0d19fbf2013-01-21 17:20:14 +0100929 ok = ParseFrameArgs(feature->args_[i].params_, &frame);
Urvang Joshifa30c862012-11-01 15:34:46 -0700930 if (!ok) {
931 WebPDataClear(&frame.bitstream);
932 ERROR_GOTO1("ERROR: Could not parse frame properties.\n",
933 Err2);
934 }
935 err = WebPMuxPushFrame(mux, &frame, 1);
Urvang Joshia0770722012-10-30 14:54:46 -0700936 WebPDataClear(&frame.bitstream);
Urvang Joshifa30c862012-11-01 15:34:46 -0700937 if (err != WEBP_MUX_OK) {
938 ERROR_GOTO3("ERROR (%s): Could not add a frame at index %d."
skal0d19fbf2013-01-21 17:20:14 +0100939 "\n", ErrorString(err), i, Err2);
Urvang Joshifa30c862012-11-01 15:34:46 -0700940 }
941 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530942 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700943 default: {
944 ERROR_GOTO1("ERROR: Invalid subtype for 'frame'", Err2);
945 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530946 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530947 }
948 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700949 err = WebPMuxSetAnimationParams(mux, &params);
950 if (err != WEBP_MUX_OK) {
951 ERROR_GOTO2("ERROR (%s): Could not set animation parameters.\n",
952 ErrorString(err), Err2);
953 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530954 break;
Urvang Joshifa30c862012-11-01 15:34:46 -0700955 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530956
skal0d19fbf2013-01-21 17:20:14 +0100957 case FEATURE_FRGM: {
958 int i;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530959 mux = WebPMuxNew();
960 if (mux == NULL) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530961 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
962 ErrorString(WEBP_MUX_MEMORY_ERROR), Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530963 }
skal0d19fbf2013-01-21 17:20:14 +0100964 for (i = 0; i < feature->arg_count_; ++i) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700965 WebPMuxFrameInfo frgm;
966 frgm.id = WEBP_CHUNK_FRGM;
skal0d19fbf2013-01-21 17:20:14 +0100967 ok = ReadFileToWebPData(feature->args_[i].filename_,
Urvang Joshia00a3da2012-10-31 17:49:15 -0700968 &frgm.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530969 if (!ok) goto Err2;
skal0d19fbf2013-01-21 17:20:14 +0100970 ok = ParseFragmentArgs(feature->args_[i].params_, &frgm);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530971 if (!ok) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700972 WebPDataClear(&frgm.bitstream);
973 ERROR_GOTO1("ERROR: Could not parse fragment properties.\n",
974 Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530975 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700976 err = WebPMuxPushFrame(mux, &frgm, 1);
977 WebPDataClear(&frgm.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530978 if (err != WEBP_MUX_OK) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700979 ERROR_GOTO3("ERROR (%s): Could not add a fragment at index %d.\n",
skal0d19fbf2013-01-21 17:20:14 +0100980 ErrorString(err), i, Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530981 }
982 }
983 break;
skal0d19fbf2013-01-21 17:20:14 +0100984 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530985
986 case FEATURE_ICCP:
Urvang Joshif903cba2012-10-31 16:30:41 -0700987 case FEATURE_EXIF:
skal0d19fbf2013-01-21 17:20:14 +0100988 case FEATURE_XMP: {
James Zern061263a2012-05-11 16:00:57 -0700989 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530990 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700991 ok = ReadFileToWebPData(feature->args_[0].filename_, &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530992 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700993 err = WebPMuxSetChunk(mux, kFourccList[feature->type_], &chunk, 1);
994 free((void*)chunk.bytes);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530995 if (err != WEBP_MUX_OK) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700996 ERROR_GOTO3("ERROR (%s): Could not set the %s.\n",
997 ErrorString(err), kDescriptions[feature->type_], Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530998 }
999 break;
skal0d19fbf2013-01-21 17:20:14 +01001000 }
1001 default: {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301002 ERROR_GOTO1("ERROR: Invalid feature for action 'set'.\n", Err2);
1003 break;
skal0d19fbf2013-01-21 17:20:14 +01001004 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301005 }
1006 ok = WriteWebP(mux, config->output_);
1007 break;
skal0d19fbf2013-01-21 17:20:14 +01001008 }
1009 case ACTION_STRIP: {
James Zern061263a2012-05-11 16:00:57 -07001010 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301011 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -07001012 if (feature->type_ == FEATURE_ICCP || feature->type_ == FEATURE_EXIF ||
1013 feature->type_ == FEATURE_XMP) {
1014 err = WebPMuxDeleteChunk(mux, kFourccList[feature->type_]);
1015 if (err != WEBP_MUX_OK) {
1016 ERROR_GOTO3("ERROR (%s): Could not strip the %s.\n",
1017 ErrorString(err), kDescriptions[feature->type_], Err2);
1018 }
1019 } else {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301020 ERROR_GOTO1("ERROR: Invalid feature for action 'strip'.\n", Err2);
1021 break;
1022 }
1023 ok = WriteWebP(mux, config->output_);
1024 break;
skal0d19fbf2013-01-21 17:20:14 +01001025 }
1026 case ACTION_INFO: {
James Zern061263a2012-05-11 16:00:57 -07001027 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301028 if (!ok) goto Err2;
1029 ok = (DisplayInfo(mux) == WEBP_MUX_OK);
1030 break;
skal0d19fbf2013-01-21 17:20:14 +01001031 }
1032 default: {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301033 assert(0); // Invalid action.
1034 break;
skal0d19fbf2013-01-21 17:20:14 +01001035 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301036 }
1037
1038 Err2:
1039 WebPMuxDelete(mux);
1040 return ok;
1041}
1042
1043//------------------------------------------------------------------------------
1044// Main.
1045
1046int main(int argc, const char* argv[]) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301047 WebPMuxConfig* config;
James Zern974aaff2012-01-24 12:46:46 -08001048 int ok = InitializeConfig(argc - 1, argv + 1, &config);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301049 if (ok) {
James Zern974aaff2012-01-24 12:46:46 -08001050 ok = Process(config);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301051 } else {
1052 PrintHelp();
1053 }
1054 DeleteConfig(config);
James Zern974aaff2012-01-24 12:46:46 -08001055 return !ok;
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301056}
1057
1058//------------------------------------------------------------------------------