blob: e8afe3ad0b28f0b9dcaea72e48398f72395298de [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>
56#include "webp/mux.h"
James Zern061263a2012-05-11 16:00:57 -070057#include "./example_util.h"
Urvang Joshia4f32ca2011-09-30 11:07:01 +053058
Urvang Joshia4f32ca2011-09-30 11:07:01 +053059//------------------------------------------------------------------------------
60// Config object to parse command-line arguments.
61
62typedef enum {
63 NIL_ACTION = 0,
64 ACTION_GET,
65 ACTION_SET,
66 ACTION_STRIP,
67 ACTION_INFO,
68 ACTION_HELP
69} ActionType;
70
71typedef enum {
72 NIL_SUBTYPE = 0,
Urvang Joshifa30c862012-11-01 15:34:46 -070073 SUBTYPE_ANMF,
74 SUBTYPE_LOOP,
75 SUBTYPE_BGCOLOR
Urvang Joshia4f32ca2011-09-30 11:07:01 +053076} FeatureSubType;
77
78typedef struct {
79 FeatureSubType subtype_;
80 const char* filename_;
81 const char* params_;
82} FeatureArg;
83
84typedef enum {
85 NIL_FEATURE = 0,
Urvang Joshif903cba2012-10-31 16:30:41 -070086 FEATURE_EXIF,
87 FEATURE_XMP,
Urvang Joshia4f32ca2011-09-30 11:07:01 +053088 FEATURE_ICCP,
Urvang Joshia00a3da2012-10-31 17:49:15 -070089 FEATURE_ANMF,
90 FEATURE_FRGM,
Urvang Joshif903cba2012-10-31 16:30:41 -070091 LAST_FEATURE
Urvang Joshia4f32ca2011-09-30 11:07:01 +053092} FeatureType;
93
Urvang Joshif903cba2012-10-31 16:30:41 -070094static const char* const kFourccList[LAST_FEATURE] = {
95 NULL, "EXIF", "XMP ", "ICCP", "ANMF", "FRGM"
96};
97
98static const char* const kDescriptions[LAST_FEATURE] = {
99 NULL, "EXIF metadata", "XMP metadata", "ICC profile",
Urvang Joshia00a3da2012-10-31 17:49:15 -0700100 "Animation frame", "Image fragment"
Urvang Joshif903cba2012-10-31 16:30:41 -0700101};
102
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530103typedef struct {
104 FeatureType type_;
105 FeatureArg* args_;
106 int arg_count_;
107} Feature;
108
109typedef struct {
110 ActionType action_type_;
111 const char* input_;
112 const char* output_;
113 Feature feature_;
114} WebPMuxConfig;
115
116//------------------------------------------------------------------------------
117// Helper functions.
118
James Zern04e84cf2011-11-04 15:20:08 -0700119static int CountOccurrences(const char* arglist[], int list_length,
120 const char* arg) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530121 int i;
122 int num_occurences = 0;
123
124 for (i = 0; i < list_length; ++i) {
125 if (!strcmp(arglist[i], arg)) {
126 ++num_occurences;
127 }
128 }
129 return num_occurences;
130}
131
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530132static const char* const kErrorMessages[] = {
Urvang Joshia4b9b1c2012-07-06 17:33:59 +0530133 "WEBP_MUX_NOT_FOUND", "WEBP_MUX_INVALID_ARGUMENT", "WEBP_MUX_BAD_DATA",
134 "WEBP_MUX_MEMORY_ERROR", "WEBP_MUX_NOT_ENOUGH_DATA"
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530135};
136
137static const char* ErrorString(WebPMuxError err) {
Urvang Joshia4b9b1c2012-07-06 17:33:59 +0530138 assert(err <= WEBP_MUX_NOT_FOUND && err >= WEBP_MUX_NOT_ENOUGH_DATA);
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530139 return kErrorMessages[-err];
140}
141
James Zerna0b27362012-01-27 17:39:47 -0800142#define RETURN_IF_ERROR(ERR_MSG) \
143 if (err != WEBP_MUX_OK) { \
144 fprintf(stderr, ERR_MSG); \
145 return err; \
146 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530147
James Zerna0b27362012-01-27 17:39:47 -0800148#define RETURN_IF_ERROR2(ERR_MSG, FORMAT_STR) \
149 if (err != WEBP_MUX_OK) { \
150 fprintf(stderr, ERR_MSG, FORMAT_STR); \
151 return err; \
152 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530153
Urvang Joshid0c79f02012-08-23 16:28:36 +0530154#define RETURN_IF_ERROR3(ERR_MSG, FORMAT_STR1, FORMAT_STR2) \
155 if (err != WEBP_MUX_OK) { \
156 fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
157 return err; \
158 }
159
James Zerna0b27362012-01-27 17:39:47 -0800160#define ERROR_GOTO1(ERR_MSG, LABEL) \
161 do { \
162 fprintf(stderr, ERR_MSG); \
163 ok = 0; \
164 goto LABEL; \
165 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530166
James Zerna0b27362012-01-27 17:39:47 -0800167#define ERROR_GOTO2(ERR_MSG, FORMAT_STR, LABEL) \
168 do { \
169 fprintf(stderr, ERR_MSG, FORMAT_STR); \
170 ok = 0; \
171 goto LABEL; \
172 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530173
James Zerna0b27362012-01-27 17:39:47 -0800174#define ERROR_GOTO3(ERR_MSG, FORMAT_STR1, FORMAT_STR2, LABEL) \
Urvang Joshi6393fe42013-04-26 15:55:42 -0700175 do { \
176 fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
177 ok = 0; \
178 goto LABEL; \
179 } while (0)
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530180
181static WebPMuxError DisplayInfo(const WebPMux* mux) {
Urvang Joshifffefd12013-05-02 13:54:25 -0700182 int width, height;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530183 uint32_t flag;
184
Urvang Joshifffefd12013-05-02 13:54:25 -0700185 WebPMuxError err = WebPMuxGetCanvasSize(mux, &width, &height);
186 RETURN_IF_ERROR("Failed to retrieve canvas width/height.\n");
187 printf("Canvas size: %d x %d\n", width, height);
188
189 err = WebPMuxGetFeatures(mux, &flag);
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700190#ifndef WEBP_EXPERIMENTAL_FEATURES
191 if (flag & FRAGMENTS_FLAG) err = WEBP_MUX_INVALID_ARGUMENT;
192#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530193 RETURN_IF_ERROR("Failed to retrieve features\n");
194
195 if (flag == 0) {
196 fprintf(stderr, "No features present.\n");
197 return err;
198 }
199
200 // Print the features present.
James Zern974aaff2012-01-24 12:46:46 -0800201 printf("Features present:");
202 if (flag & ANIMATION_FLAG) printf(" animation");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700203 if (flag & FRAGMENTS_FLAG) printf(" image fragments");
James Zernd8dc72a2013-03-13 14:04:20 -0700204 if (flag & ICCP_FLAG) printf(" ICC profile");
Urvang Joshif903cba2012-10-31 16:30:41 -0700205 if (flag & EXIF_FLAG) printf(" EXIF metadata");
206 if (flag & XMP_FLAG) printf(" XMP metadata");
James Zern974aaff2012-01-24 12:46:46 -0800207 if (flag & ALPHA_FLAG) printf(" transparency");
208 printf("\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530209
Urvang Joshia00a3da2012-10-31 17:49:15 -0700210 if ((flag & ANIMATION_FLAG) || (flag & FRAGMENTS_FLAG)) {
Urvang Joshid0c79f02012-08-23 16:28:36 +0530211 const int is_anim = !!(flag & ANIMATION_FLAG);
Urvang Joshi92f80592012-10-30 12:14:10 -0700212 const WebPChunkId id = is_anim ? WEBP_CHUNK_ANMF : WEBP_CHUNK_FRGM;
Urvang Joshia00a3da2012-10-31 17:49:15 -0700213 const char* const type_str = is_anim ? "frame" : "fragment";
James Zerneec4b872012-01-07 12:44:01 -0800214 int nFrames;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530215
Urvang Joshid0c79f02012-08-23 16:28:36 +0530216 if (is_anim) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700217 WebPMuxAnimParams params;
218 err = WebPMuxGetAnimationParams(mux, &params);
219 RETURN_IF_ERROR("Failed to retrieve animation parameters\n");
220 printf("Background color : 0x%.8X Loop Count : %d\n",
221 params.bgcolor, params.loop_count);
Urvang Joshid0c79f02012-08-23 16:28:36 +0530222 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530223
Urvang Joshid0c79f02012-08-23 16:28:36 +0530224 err = WebPMuxNumChunks(mux, id, &nFrames);
225 RETURN_IF_ERROR2("Failed to retrieve number of %ss\n", type_str);
226
227 printf("Number of %ss: %d\n", type_str, nFrames);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530228 if (nFrames > 0) {
229 int i;
Urvang Joshid0c79f02012-08-23 16:28:36 +0530230 printf("No.: x_offset y_offset ");
Urvang Joshifa30c862012-11-01 15:34:46 -0700231 if (is_anim) printf("duration dispose ");
Urvang Joshid0c79f02012-08-23 16:28:36 +0530232 printf("image_size\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530233 for (i = 1; i <= nFrames; i++) {
Urvang Joshiab3234a2012-08-23 15:18:51 +0530234 WebPMuxFrameInfo frame;
235 err = WebPMuxGetFrame(mux, i, &frame);
skal3e59a742013-05-22 00:58:53 +0200236 if (err == WEBP_MUX_OK) {
237 printf("%3d: %8d %8d ", i, frame.x_offset, frame.y_offset);
238 if (is_anim) printf("%8d %7d ", frame.duration, frame.dispose_method);
239 printf("%10d\n", (int)frame.bitstream.size);
240 }
Urvang Joshia0770722012-10-30 14:54:46 -0700241 WebPDataClear(&frame.bitstream);
skal3e59a742013-05-22 00:58:53 +0200242 RETURN_IF_ERROR3("Failed to retrieve %s#%d\n", type_str, i);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530243 }
244 }
245 }
246
247 if (flag & ICCP_FLAG) {
James Zerneec4b872012-01-07 12:44:01 -0800248 WebPData icc_profile;
Urvang Joshi1c04a0d2012-08-23 15:28:20 +0530249 err = WebPMuxGetChunk(mux, "ICCP", &icc_profile);
Urvang Joshif903cba2012-10-31 16:30:41 -0700250 RETURN_IF_ERROR("Failed to retrieve the ICC profile\n");
James Zern14d42af2013-03-20 16:59:35 -0700251 printf("Size of the ICC profile data: %d\n", (int)icc_profile.size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530252 }
253
Urvang Joshif903cba2012-10-31 16:30:41 -0700254 if (flag & EXIF_FLAG) {
255 WebPData exif;
256 err = WebPMuxGetChunk(mux, "EXIF", &exif);
257 RETURN_IF_ERROR("Failed to retrieve the EXIF metadata\n");
James Zern14d42af2013-03-20 16:59:35 -0700258 printf("Size of the EXIF metadata: %d\n", (int)exif.size);
Urvang Joshif903cba2012-10-31 16:30:41 -0700259 }
260
261 if (flag & XMP_FLAG) {
262 WebPData xmp;
263 err = WebPMuxGetChunk(mux, "XMP ", &xmp);
264 RETURN_IF_ERROR("Failed to retrieve the XMP metadata\n");
James Zern14d42af2013-03-20 16:59:35 -0700265 printf("Size of the XMP metadata: %d\n", (int)xmp.size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530266 }
267
Urvang Joshia00a3da2012-10-31 17:49:15 -0700268 if ((flag & ALPHA_FLAG) && !(flag & (ANIMATION_FLAG | FRAGMENTS_FLAG))) {
Urvang Joshid0c79f02012-08-23 16:28:36 +0530269 WebPMuxFrameInfo image;
270 err = WebPMuxGetFrame(mux, 1, &image);
skal3e59a742013-05-22 00:58:53 +0200271 if (err == WEBP_MUX_OK) {
272 printf("Size of the image (with alpha): %d\n", (int)image.bitstream.size);
273 }
274 WebPDataClear(&image.bitstream);
Urvang Joshicdf97aa2011-12-01 16:11:51 +0530275 RETURN_IF_ERROR("Failed to retrieve the image\n");
Urvang Joshicdf97aa2011-12-01 16:11:51 +0530276 }
277
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530278 return WEBP_MUX_OK;
279}
280
Pascal Massiminoabd030b2011-11-01 06:24:34 -0700281static void PrintHelp(void) {
James Zern974aaff2012-01-24 12:46:46 -0800282 printf("Usage: webpmux -get GET_OPTIONS INPUT -o OUTPUT\n");
283 printf(" webpmux -set SET_OPTIONS INPUT -o OUTPUT\n");
284 printf(" webpmux -strip STRIP_OPTIONS INPUT -o OUTPUT\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700285#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700286 printf(" webpmux -frgm FRAGMENT_OPTIONS [-frgm...] -o OUTPUT\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700287#endif
Urvang Joshifa30c862012-11-01 15:34:46 -0700288 printf(" webpmux -frame FRAME_OPTIONS [-frame...] [-loop LOOP_COUNT]"
289 "\n");
290 printf(" [-bgcolor BACKGROUND_COLOR] -o OUTPUT\n");
James Zern974aaff2012-01-24 12:46:46 -0800291 printf(" webpmux -info INPUT\n");
292 printf(" webpmux [-h|-help]\n");
Urvang Joshia5042a32013-02-26 14:22:06 -0800293 printf(" webpmux -version\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530294
James Zern974aaff2012-01-24 12:46:46 -0800295 printf("\n");
296 printf("GET_OPTIONS:\n");
297 printf(" Extract relevant data.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700298 printf(" icc Get ICC profile.\n");
299 printf(" exif Get EXIF metadata.\n");
300 printf(" xmp Get XMP metadata.\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700301#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700302 printf(" frgm n Get nth fragment.\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700303#endif
James Zern974aaff2012-01-24 12:46:46 -0800304 printf(" frame n Get nth frame.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530305
James Zern974aaff2012-01-24 12:46:46 -0800306 printf("\n");
307 printf("SET_OPTIONS:\n");
308 printf(" Set color profile/metadata.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700309 printf(" icc file.icc Set ICC profile.\n");
310 printf(" exif file.exif Set EXIF metadata.\n");
311 printf(" xmp file.xmp Set XMP metadata.\n");
312 printf(" where: 'file.icc' contains the ICC profile to be set,\n");
313 printf(" 'file.exif' contains the EXIF metadata to be set\n");
314 printf(" 'file.xmp' contains the XMP metadata to be set\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530315
James Zern974aaff2012-01-24 12:46:46 -0800316 printf("\n");
317 printf("STRIP_OPTIONS:\n");
318 printf(" Strip color profile/metadata.\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700319 printf(" icc Strip ICC profile.\n");
320 printf(" exif Strip EXIF metadata.\n");
321 printf(" xmp Strip XMP metadata.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530322
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700323#ifdef WEBP_EXPERIMENTAL_FEATURES
James Zern974aaff2012-01-24 12:46:46 -0800324 printf("\n");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700325 printf("FRAGMENT_OPTIONS(i):\n");
326 printf(" Create fragmented image.\n");
James Zern974aaff2012-01-24 12:46:46 -0800327 printf(" file_i +xi+yi\n");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700328 printf(" where: 'file_i' is the i'th fragment (WebP format),\n");
329 printf(" 'xi','yi' specify the image offset for this fragment."
330 "\n");
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700331#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530332
James Zern974aaff2012-01-24 12:46:46 -0800333 printf("\n");
334 printf("FRAME_OPTIONS(i):\n");
335 printf(" Create animation.\n");
James Zern8fab1612013-03-07 19:15:37 -0800336 printf(" file_i +di+xi+yi+mi\n");
Urvang Joshif903cba2012-10-31 16:30:41 -0700337 printf(" where: 'file_i' is the i'th animation frame (WebP format),\n");
James Zern974aaff2012-01-24 12:46:46 -0800338 printf(" 'di' is the pause duration before next frame.\n");
James Zern8fab1612013-03-07 19:15:37 -0800339 printf(" 'xi','yi' specify the image offset for this frame.\n");
Urvang Joshifa30c862012-11-01 15:34:46 -0700340 printf(" 'mi' is the dispose method for this frame (0 or 1).\n");
341
342 printf("\n");
343 printf("LOOP_COUNT:\n");
344 printf(" Number of times to repeat the animation.\n");
345 printf(" Valid range is 0 to 65535 [Default: 0 (infinite)].\n");
346
347 printf("\n");
348 printf("BACKGROUND_COLOR:\n");
349 printf(" Background color of the canvas.\n");
350 printf(" A,R,G,B\n");
351 printf(" where: 'A', 'R', 'G' and 'B' are integers in the range 0 to 255 "
352 "specifying\n");
353 printf(" the Alpha, Red, Green and Blue component values "
354 "respectively\n");
355 printf(" [Default: 255,255,255,255].\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530356
Urvang Joshif903cba2012-10-31 16:30:41 -0700357 printf("\nINPUT & OUTPUT are in WebP format.\n");
358
Urvang Joshifa30c862012-11-01 15:34:46 -0700359 printf("\nNote: The nature of EXIF, XMP and ICC data is not checked");
360 printf(" and is assumed to be\nvalid.\n");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530361}
362
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530363static int ReadFileToWebPData(const char* const filename,
364 WebPData* const webp_data) {
365 const uint8_t* data;
366 size_t size;
367 if (!ExUtilReadFile(filename, &data, &size)) return 0;
Urvang Joshia0770722012-10-30 14:54:46 -0700368 webp_data->bytes = data;
369 webp_data->size = size;
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530370 return 1;
371}
372
James Zern061263a2012-05-11 16:00:57 -0700373static int CreateMux(const char* const filename, WebPMux** mux) {
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530374 WebPData bitstream;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530375 assert(mux != NULL);
Urvang Joshi4fc4a472012-06-05 14:20:45 +0530376 if (!ReadFileToWebPData(filename, &bitstream)) return 0;
Urvang Joshi6d5c7972012-06-07 13:45:06 +0530377 *mux = WebPMuxCreate(&bitstream, 1);
Urvang Joshia0770722012-10-30 14:54:46 -0700378 free((void*)bitstream.bytes);
Urvang Joshi6d5c7972012-06-07 13:45:06 +0530379 if (*mux != NULL) return 1;
380 fprintf(stderr, "Failed to create mux object from file %s.\n", filename);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530381 return 0;
382}
383
James Zern0f7820e2012-01-24 14:08:27 -0800384static int WriteData(const char* filename, const WebPData* const webpdata) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530385 int ok = 0;
James Zern04e84cf2011-11-04 15:20:08 -0700386 FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530387 if (!fout) {
388 fprintf(stderr, "Error opening output WebP file %s!\n", filename);
389 return 0;
390 }
Urvang Joshia0770722012-10-30 14:54:46 -0700391 if (fwrite(webpdata->bytes, webpdata->size, 1, fout) != 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530392 fprintf(stderr, "Error writing file %s!\n", filename);
393 } else {
James Zern14d42af2013-03-20 16:59:35 -0700394 fprintf(stderr, "Saved file %s (%d bytes)\n",
395 filename, (int)webpdata->size);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530396 ok = 1;
397 }
398 if (fout != stdout) fclose(fout);
399 return ok;
400}
401
402static int WriteWebP(WebPMux* const mux, const char* filename) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530403 int ok;
Urvang Joshif1df5582012-06-07 11:04:57 +0530404 WebPData webp_data;
405 const WebPMuxError err = WebPMuxAssemble(mux, &webp_data);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530406 if (err != WEBP_MUX_OK) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530407 fprintf(stderr, "Error (%s) assembling the WebP file.\n", ErrorString(err));
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530408 return 0;
409 }
Urvang Joshif1df5582012-06-07 11:04:57 +0530410 ok = WriteData(filename, &webp_data);
411 WebPDataClear(&webp_data);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530412 return ok;
413}
414
Urvang Joshiab3234a2012-08-23 15:18:51 +0530415static int ParseFrameArgs(const char* args, WebPMuxFrameInfo* const info) {
skal48600082012-11-14 06:19:31 +0100416 int dispose_method, dummy;
417 const int num_args = sscanf(args, "+%d+%d+%d+%d+%d",
418 &info->duration, &info->x_offset, &info->y_offset,
419 &dispose_method, &dummy);
420 switch (num_args) {
421 case 1:
422 info->x_offset = info->y_offset = 0; // fall through
423 case 3:
424 dispose_method = 0; // fall through
425 case 4:
426 break;
427 default:
428 return 0;
Urvang Joshifa30c862012-11-01 15:34:46 -0700429 }
430 // Note: The sanity of the following conversion is checked by
431 // WebPMuxSetAnimationParams().
432 info->dispose_method = (WebPMuxAnimDispose)dispose_method;
Urvang Joshi62848542013-08-09 14:09:31 -0700433 // TODO(urvang): Add support for parsing blending method too.
434 info->blend_method = WEBP_MUX_BLEND;
Urvang Joshifa30c862012-11-01 15:34:46 -0700435 return 1;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530436}
437
Urvang Joshia00a3da2012-10-31 17:49:15 -0700438static int ParseFragmentArgs(const char* args, WebPMuxFrameInfo* const info) {
Urvang Joshia0770722012-10-30 14:54:46 -0700439 return (sscanf(args, "+%d+%d", &info->x_offset, &info->y_offset) == 2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530440}
441
Urvang Joshifa30c862012-11-01 15:34:46 -0700442static int ParseBgcolorArgs(const char* args, uint32_t* const bgcolor) {
443 uint32_t a, r, g, b;
444 if (sscanf(args, "%u,%u,%u,%u", &a, &r, &g, &b) != 4) return 0;
445 if (a >= 256 || r >= 256 || g >= 256 || b >= 256) return 0;
446 *bgcolor = (a << 24) | (r << 16) | (g << 8) | (b << 0);
447 return 1;
448}
449
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530450//------------------------------------------------------------------------------
451// Clean-up.
452
453static void DeleteConfig(WebPMuxConfig* config) {
454 if (config != NULL) {
455 free(config->feature_.args_);
456 free(config);
457 }
458}
459
460//------------------------------------------------------------------------------
461// Parsing.
462
463// Basic syntactic checks on the command-line arguments.
464// Returns 1 on valid, 0 otherwise.
465// Also fills up num_feature_args to be number of feature arguments given.
466// (e.g. if there are 4 '-frame's and 1 '-loop', then num_feature_args = 5).
467static int ValidateCommandLine(int argc, const char* argv[],
468 int* num_feature_args) {
469 int num_frame_args;
Urvang Joshia00a3da2012-10-31 17:49:15 -0700470 int num_frgm_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530471 int num_loop_args;
Urvang Joshifa30c862012-11-01 15:34:46 -0700472 int num_bgcolor_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530473 int ok = 1;
474
475 assert(num_feature_args != NULL);
476 *num_feature_args = 0;
477
478 // Simple checks.
James Zern04e84cf2011-11-04 15:20:08 -0700479 if (CountOccurrences(argv, argc, "-get") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530480 ERROR_GOTO1("ERROR: Multiple '-get' arguments specified.\n", ErrValidate);
481 }
James Zern04e84cf2011-11-04 15:20:08 -0700482 if (CountOccurrences(argv, argc, "-set") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530483 ERROR_GOTO1("ERROR: Multiple '-set' arguments specified.\n", ErrValidate);
484 }
James Zern04e84cf2011-11-04 15:20:08 -0700485 if (CountOccurrences(argv, argc, "-strip") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530486 ERROR_GOTO1("ERROR: Multiple '-strip' arguments specified.\n", ErrValidate);
487 }
James Zern04e84cf2011-11-04 15:20:08 -0700488 if (CountOccurrences(argv, argc, "-info") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530489 ERROR_GOTO1("ERROR: Multiple '-info' arguments specified.\n", ErrValidate);
490 }
James Zern04e84cf2011-11-04 15:20:08 -0700491 if (CountOccurrences(argv, argc, "-o") > 1) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530492 ERROR_GOTO1("ERROR: Multiple output files specified.\n", ErrValidate);
493 }
494
495 // Compound checks.
James Zern04e84cf2011-11-04 15:20:08 -0700496 num_frame_args = CountOccurrences(argv, argc, "-frame");
Urvang Joshia00a3da2012-10-31 17:49:15 -0700497 num_frgm_args = CountOccurrences(argv, argc, "-frgm");
James Zern04e84cf2011-11-04 15:20:08 -0700498 num_loop_args = CountOccurrences(argv, argc, "-loop");
Urvang Joshifa30c862012-11-01 15:34:46 -0700499 num_bgcolor_args = CountOccurrences(argv, argc, "-bgcolor");
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530500
501 if (num_loop_args > 1) {
502 ERROR_GOTO1("ERROR: Multiple loop counts specified.\n", ErrValidate);
503 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700504 if (num_bgcolor_args > 1) {
505 ERROR_GOTO1("ERROR: Multiple background colors specified.\n", ErrValidate);
506 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530507
Urvang Joshifa30c862012-11-01 15:34:46 -0700508 if ((num_frame_args == 0) && (num_loop_args + num_bgcolor_args > 0)) {
509 ERROR_GOTO1("ERROR: Loop count and background color are relevant only in "
510 "case of animation.\n", ErrValidate);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530511 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700512 if (num_frame_args > 0 && num_frgm_args > 0) {
513 ERROR_GOTO1("ERROR: Only one of frames & fragments can be specified at a "
514 "time.\n", ErrValidate);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530515 }
516
517 assert(ok == 1);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700518 if (num_frame_args == 0 && num_frgm_args == 0) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700519 // Single argument ('set' action for ICCP/EXIF/XMP, OR a 'get' action).
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530520 *num_feature_args = 1;
521 } else {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700522 // Multiple arguments ('set' action for animation or fragmented image).
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530523 if (num_frame_args > 0) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700524 *num_feature_args = num_frame_args + num_loop_args + num_bgcolor_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530525 } else {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700526 *num_feature_args = num_frgm_args;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530527 }
528 }
529
530 ErrValidate:
531 return ok;
532}
533
534#define ACTION_IS_NIL (config->action_type_ == NIL_ACTION)
535
536#define FEATURETYPE_IS_NIL (feature->type_ == NIL_FEATURE)
537
538#define CHECK_NUM_ARGS_LESS(NUM, LABEL) \
539 if (argc < i + (NUM)) { \
540 fprintf(stderr, "ERROR: Too few arguments for '%s'.\n", argv[i]); \
541 goto LABEL; \
542 }
543
544#define CHECK_NUM_ARGS_NOT_EQUAL(NUM, LABEL) \
545 if (argc != i + (NUM)) { \
546 fprintf(stderr, "ERROR: Too many arguments for '%s'.\n", argv[i]); \
547 goto LABEL; \
548 }
549
550// Parses command-line arguments to fill up config object. Also performs some
551// semantic checks.
552static int ParseCommandLine(int argc, const char* argv[],
553 WebPMuxConfig* config) {
554 int i = 0;
555 int feature_arg_index = 0;
556 int ok = 1;
557
558 while (i < argc) {
559 Feature* const feature = &config->feature_;
James Zern04e84cf2011-11-04 15:20:08 -0700560 FeatureArg* const arg = &feature->args_[feature_arg_index];
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530561 if (argv[i][0] == '-') { // One of the action types or output.
562 if (!strcmp(argv[i], "-set")) {
563 if (ACTION_IS_NIL) {
564 config->action_type_ = ACTION_SET;
565 } else {
566 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
567 }
568 ++i;
569 } else if (!strcmp(argv[i], "-get")) {
570 if (ACTION_IS_NIL) {
571 config->action_type_ = ACTION_GET;
572 } else {
573 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
574 }
575 ++i;
576 } else if (!strcmp(argv[i], "-strip")) {
577 if (ACTION_IS_NIL) {
578 config->action_type_ = ACTION_STRIP;
579 feature->arg_count_ = 0;
580 } else {
581 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
582 }
583 ++i;
584 } else if (!strcmp(argv[i], "-frame")) {
585 CHECK_NUM_ARGS_LESS(3, ErrParse);
586 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
587 config->action_type_ = ACTION_SET;
588 } else {
589 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
590 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700591 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_ANMF) {
592 feature->type_ = FEATURE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530593 } else {
594 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
595 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700596 arg->subtype_ = SUBTYPE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530597 arg->filename_ = argv[i + 1];
598 arg->params_ = argv[i + 2];
599 ++feature_arg_index;
600 i += 3;
Urvang Joshifa30c862012-11-01 15:34:46 -0700601 } else if (!strcmp(argv[i], "-loop") || !strcmp(argv[i], "-bgcolor")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530602 CHECK_NUM_ARGS_LESS(2, ErrParse);
603 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
604 config->action_type_ = ACTION_SET;
605 } else {
606 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
607 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700608 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_ANMF) {
609 feature->type_ = FEATURE_ANMF;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530610 } else {
611 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
612 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700613 arg->subtype_ =
614 !strcmp(argv[i], "-loop") ? SUBTYPE_LOOP : SUBTYPE_BGCOLOR;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530615 arg->params_ = argv[i + 1];
616 ++feature_arg_index;
617 i += 2;
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700618#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia00a3da2012-10-31 17:49:15 -0700619 } else if (!strcmp(argv[i], "-frgm")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530620 CHECK_NUM_ARGS_LESS(3, ErrParse);
621 if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) {
622 config->action_type_ = ACTION_SET;
623 } else {
624 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
625 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700626 if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_FRGM) {
627 feature->type_ = FEATURE_FRGM;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530628 } else {
629 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
630 }
631 arg->filename_ = argv[i + 1];
632 arg->params_ = argv[i + 2];
633 ++feature_arg_index;
634 i += 3;
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700635#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530636 } else if (!strcmp(argv[i], "-o")) {
637 CHECK_NUM_ARGS_LESS(2, ErrParse);
638 config->output_ = argv[i + 1];
639 i += 2;
640 } else if (!strcmp(argv[i], "-info")) {
641 CHECK_NUM_ARGS_NOT_EQUAL(2, ErrParse);
642 if (config->action_type_ != NIL_ACTION) {
643 ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse);
644 } else {
645 config->action_type_ = ACTION_INFO;
646 feature->arg_count_ = 0;
647 config->input_ = argv[i + 1];
648 }
649 i += 2;
650 } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help")) {
651 PrintHelp();
652 DeleteConfig(config);
James Zern974aaff2012-01-24 12:46:46 -0800653 exit(0);
Urvang Joshia5042a32013-02-26 14:22:06 -0800654 } else if (!strcmp(argv[i], "-version")) {
655 const int version = WebPGetMuxVersion();
656 printf("%d.%d.%d\n",
657 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
658 DeleteConfig(config);
659 exit(0);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530660 } else {
661 ERROR_GOTO2("ERROR: Unknown option: '%s'.\n", argv[i], ErrParse);
662 }
663 } else { // One of the feature types or input.
664 if (ACTION_IS_NIL) {
665 ERROR_GOTO1("ERROR: Action must be specified before other arguments.\n",
666 ErrParse);
667 }
Urvang Joshif903cba2012-10-31 16:30:41 -0700668 if (!strcmp(argv[i], "icc") || !strcmp(argv[i], "exif") ||
669 !strcmp(argv[i], "xmp")) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530670 if (FEATURETYPE_IS_NIL) {
671 feature->type_ = (!strcmp(argv[i], "icc")) ? FEATURE_ICCP :
Urvang Joshif903cba2012-10-31 16:30:41 -0700672 (!strcmp(argv[i], "exif")) ? FEATURE_EXIF : FEATURE_XMP;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530673 } else {
674 ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse);
675 }
676 if (config->action_type_ == ACTION_SET) {
677 CHECK_NUM_ARGS_LESS(2, ErrParse);
678 arg->filename_ = argv[i + 1];
679 ++feature_arg_index;
680 i += 2;
681 } else {
682 ++i;
683 }
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700684#ifdef WEBP_EXPERIMENTAL_FEATURES
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530685 } else if ((!strcmp(argv[i], "frame") ||
Urvang Joshia00a3da2012-10-31 17:49:15 -0700686 !strcmp(argv[i], "frgm")) &&
Urvang Joshi5dbd4032013-03-15 14:46:12 -0700687#else
688 } else if (!strcmp(argv[i], "frame") &&
689#endif
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530690 (config->action_type_ == ACTION_GET)) {
691 CHECK_NUM_ARGS_LESS(2, ErrParse);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700692 feature->type_ = (!strcmp(argv[i], "frame")) ? FEATURE_ANMF :
693 FEATURE_FRGM;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530694 arg->params_ = argv[i + 1];
695 ++feature_arg_index;
696 i += 2;
James Zern04e84cf2011-11-04 15:20:08 -0700697 } else { // Assume input file.
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530698 if (config->input_ == NULL) {
699 config->input_ = argv[i];
700 } else {
701 ERROR_GOTO2("ERROR at '%s': Multiple input files specified.\n",
702 argv[i], ErrParse);
703 }
704 ++i;
705 }
706 }
707 }
708 ErrParse:
709 return ok;
710}
711
James Zern04e84cf2011-11-04 15:20:08 -0700712// Additional checks after config is filled.
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530713static int ValidateConfig(WebPMuxConfig* config) {
714 int ok = 1;
715 Feature* const feature = &config->feature_;
716
717 // Action.
718 if (ACTION_IS_NIL) {
719 ERROR_GOTO1("ERROR: No action specified.\n", ErrValidate2);
720 }
721
722 // Feature type.
723 if (FEATURETYPE_IS_NIL && config->action_type_ != ACTION_INFO) {
724 ERROR_GOTO1("ERROR: No feature specified.\n", ErrValidate2);
725 }
726
727 // Input file.
728 if (config->input_ == NULL) {
729 if (config->action_type_ != ACTION_SET) {
730 ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2);
Urvang Joshia00a3da2012-10-31 17:49:15 -0700731 } else if (feature->type_ != FEATURE_ANMF &&
732 feature->type_ != FEATURE_FRGM) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530733 ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2);
734 }
735 }
736
737 // Output file.
738 if (config->output_ == NULL && config->action_type_ != ACTION_INFO) {
739 ERROR_GOTO1("ERROR: No output file specified.\n", ErrValidate2);
740 }
741
742 ErrValidate2:
743 return ok;
744}
745
746// Create config object from command-line arguments.
747static int InitializeConfig(int argc, const char* argv[],
748 WebPMuxConfig** config) {
749 int num_feature_args = 0;
750 int ok = 1;
751
752 assert(config != NULL);
753 *config = NULL;
754
755 // Validate command-line arguments.
756 if (!ValidateCommandLine(argc, argv, &num_feature_args)) {
757 ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
758 }
759
760 // Allocate memory.
761 *config = (WebPMuxConfig*)calloc(1, sizeof(**config));
762 if (*config == NULL) {
763 ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1);
764 }
765 (*config)->feature_.arg_count_ = num_feature_args;
766 (*config)->feature_.args_ =
767 (FeatureArg*)calloc(num_feature_args, sizeof(FeatureArg));
768 if ((*config)->feature_.args_ == NULL) {
769 ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1);
770 }
771
772 // Parse command-line.
James Zern04e84cf2011-11-04 15:20:08 -0700773 if (!ParseCommandLine(argc, argv, *config) ||
774 !ValidateConfig(*config)) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530775 ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
776 }
777
778 Err1:
779 return ok;
780}
781
782#undef ACTION_IS_NIL
783#undef FEATURETYPE_IS_NIL
784#undef CHECK_NUM_ARGS_LESS
785#undef CHECK_NUM_ARGS_MORE
786
787//------------------------------------------------------------------------------
788// Processing.
789
Urvang Joshia00a3da2012-10-31 17:49:15 -0700790static int GetFrameFragment(const WebPMux* mux,
Urvang Joshi7681bb92013-03-13 18:10:56 -0700791 const WebPMuxConfig* config, int is_frame) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530792 WebPMuxError err = WEBP_MUX_OK;
793 WebPMux* mux_single = NULL;
794 long num = 0;
795 int ok = 1;
Urvang Joshi7681bb92013-03-13 18:10:56 -0700796 const WebPChunkId id = is_frame ? WEBP_CHUNK_ANMF : WEBP_CHUNK_FRGM;
Urvang Joshiab3234a2012-08-23 15:18:51 +0530797 WebPMuxFrameInfo info;
Urvang Joshia0770722012-10-30 14:54:46 -0700798 WebPDataInit(&info.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530799
800 num = strtol(config->feature_.args_[0].params_, NULL, 10);
801 if (num < 0) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700802 ERROR_GOTO1("ERROR: Frame/Fragment index must be non-negative.\n", ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530803 }
804
Urvang Joshid0c79f02012-08-23 16:28:36 +0530805 err = WebPMuxGetFrame(mux, num, &info);
806 if (err == WEBP_MUX_OK && info.id != id) err = WEBP_MUX_NOT_FOUND;
807 if (err != WEBP_MUX_OK) {
808 ERROR_GOTO3("ERROR (%s): Could not get frame %ld.\n",
809 ErrorString(err), num, ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530810 }
811
812 mux_single = WebPMuxNew();
813 if (mux_single == NULL) {
814 err = WEBP_MUX_MEMORY_ERROR;
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530815 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
816 ErrorString(err), ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530817 }
Urvang Joshia0770722012-10-30 14:54:46 -0700818 err = WebPMuxSetImage(mux_single, &info.bitstream, 1);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530819 if (err != WEBP_MUX_OK) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530820 ERROR_GOTO2("ERROR (%s): Could not create single image mux object.\n",
821 ErrorString(err), ErrGet);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530822 }
Urvang Joshid0c79f02012-08-23 16:28:36 +0530823
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530824 ok = WriteWebP(mux_single, config->output_);
825
826 ErrGet:
Urvang Joshia0770722012-10-30 14:54:46 -0700827 WebPDataClear(&info.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530828 WebPMuxDelete(mux_single);
829 return ok;
830}
831
832// Read and process config.
James Zern04e84cf2011-11-04 15:20:08 -0700833static int Process(const WebPMuxConfig* config) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530834 WebPMux* mux = NULL;
Urvang Joshif903cba2012-10-31 16:30:41 -0700835 WebPData chunk;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530836 WebPMuxError err = WEBP_MUX_OK;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530837 int ok = 1;
James Zern04e84cf2011-11-04 15:20:08 -0700838 const Feature* const feature = &config->feature_;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530839
James Zern04e84cf2011-11-04 15:20:08 -0700840 switch (config->action_type_) {
skal0d19fbf2013-01-21 17:20:14 +0100841 case ACTION_GET: {
James Zern061263a2012-05-11 16:00:57 -0700842 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530843 if (!ok) goto Err2;
James Zern04e84cf2011-11-04 15:20:08 -0700844 switch (feature->type_) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700845 case FEATURE_ANMF:
Urvang Joshi7caab1d2012-11-07 16:04:08 -0800846 case FEATURE_FRGM:
847 ok = GetFrameFragment(mux, config,
848 (feature->type_ == FEATURE_ANMF) ? 1 : 0);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530849 break;
850
851 case FEATURE_ICCP:
Urvang Joshif903cba2012-10-31 16:30:41 -0700852 case FEATURE_EXIF:
853 case FEATURE_XMP:
854 err = WebPMuxGetChunk(mux, kFourccList[feature->type_], &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530855 if (err != WEBP_MUX_OK) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700856 ERROR_GOTO3("ERROR (%s): Could not get the %s.\n",
857 ErrorString(err), kDescriptions[feature->type_], Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530858 }
Urvang Joshif903cba2012-10-31 16:30:41 -0700859 ok = WriteData(config->output_, &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530860 break;
861
862 default:
863 ERROR_GOTO1("ERROR: Invalid feature for action 'get'.\n", Err2);
864 break;
865 }
866 break;
skal0d19fbf2013-01-21 17:20:14 +0100867 }
868 case ACTION_SET: {
James Zern04e84cf2011-11-04 15:20:08 -0700869 switch (feature->type_) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700870 case FEATURE_ANMF: {
skal0d19fbf2013-01-21 17:20:14 +0100871 int i;
Urvang Joshifa30c862012-11-01 15:34:46 -0700872 WebPMuxAnimParams params = { 0xFFFFFFFF, 0 };
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530873 mux = WebPMuxNew();
874 if (mux == NULL) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530875 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
876 ErrorString(WEBP_MUX_MEMORY_ERROR), Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530877 }
skal0d19fbf2013-01-21 17:20:14 +0100878 for (i = 0; i < feature->arg_count_; ++i) {
879 switch (feature->args_[i].subtype_) {
Urvang Joshifa30c862012-11-01 15:34:46 -0700880 case SUBTYPE_BGCOLOR: {
881 uint32_t bgcolor;
skal0d19fbf2013-01-21 17:20:14 +0100882 ok = ParseBgcolorArgs(feature->args_[i].params_, &bgcolor);
Urvang Joshifa30c862012-11-01 15:34:46 -0700883 if (!ok) {
884 ERROR_GOTO1("ERROR: Could not parse the background color \n",
885 Err2);
886 }
887 params.bgcolor = bgcolor;
888 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530889 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700890 case SUBTYPE_LOOP: {
891 const long loop_count =
skal0d19fbf2013-01-21 17:20:14 +0100892 strtol(feature->args_[i].params_, NULL, 10);
Urvang Joshifa30c862012-11-01 15:34:46 -0700893 if (loop_count != (int)loop_count) {
894 // Note: This is only a 'necessary' condition for loop_count
895 // to be valid. The 'sufficient' conditioned in checked in
896 // WebPMuxSetAnimationParams() method called later.
897 ERROR_GOTO1("ERROR: Loop count must be in the range 0 to "
898 "65535.\n", Err2);
899 }
900 params.loop_count = (int)loop_count;
901 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530902 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700903 case SUBTYPE_ANMF: {
904 WebPMuxFrameInfo frame;
905 frame.id = WEBP_CHUNK_ANMF;
skal0d19fbf2013-01-21 17:20:14 +0100906 ok = ReadFileToWebPData(feature->args_[i].filename_,
Urvang Joshifa30c862012-11-01 15:34:46 -0700907 &frame.bitstream);
908 if (!ok) goto Err2;
skal0d19fbf2013-01-21 17:20:14 +0100909 ok = ParseFrameArgs(feature->args_[i].params_, &frame);
Urvang Joshifa30c862012-11-01 15:34:46 -0700910 if (!ok) {
911 WebPDataClear(&frame.bitstream);
912 ERROR_GOTO1("ERROR: Could not parse frame properties.\n",
913 Err2);
914 }
915 err = WebPMuxPushFrame(mux, &frame, 1);
Urvang Joshia0770722012-10-30 14:54:46 -0700916 WebPDataClear(&frame.bitstream);
Urvang Joshifa30c862012-11-01 15:34:46 -0700917 if (err != WEBP_MUX_OK) {
918 ERROR_GOTO3("ERROR (%s): Could not add a frame at index %d."
skal0d19fbf2013-01-21 17:20:14 +0100919 "\n", ErrorString(err), i, Err2);
Urvang Joshifa30c862012-11-01 15:34:46 -0700920 }
921 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530922 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700923 default: {
924 ERROR_GOTO1("ERROR: Invalid subtype for 'frame'", Err2);
925 break;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530926 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530927 }
928 }
Urvang Joshifa30c862012-11-01 15:34:46 -0700929 err = WebPMuxSetAnimationParams(mux, &params);
930 if (err != WEBP_MUX_OK) {
931 ERROR_GOTO2("ERROR (%s): Could not set animation parameters.\n",
932 ErrorString(err), Err2);
933 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530934 break;
Urvang Joshifa30c862012-11-01 15:34:46 -0700935 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530936
skal0d19fbf2013-01-21 17:20:14 +0100937 case FEATURE_FRGM: {
938 int i;
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530939 mux = WebPMuxNew();
940 if (mux == NULL) {
Urvang Joshid11f6fc2012-06-27 16:55:01 +0530941 ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n",
942 ErrorString(WEBP_MUX_MEMORY_ERROR), Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530943 }
skal0d19fbf2013-01-21 17:20:14 +0100944 for (i = 0; i < feature->arg_count_; ++i) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700945 WebPMuxFrameInfo frgm;
946 frgm.id = WEBP_CHUNK_FRGM;
skal0d19fbf2013-01-21 17:20:14 +0100947 ok = ReadFileToWebPData(feature->args_[i].filename_,
Urvang Joshia00a3da2012-10-31 17:49:15 -0700948 &frgm.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530949 if (!ok) goto Err2;
skal0d19fbf2013-01-21 17:20:14 +0100950 ok = ParseFragmentArgs(feature->args_[i].params_, &frgm);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530951 if (!ok) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700952 WebPDataClear(&frgm.bitstream);
953 ERROR_GOTO1("ERROR: Could not parse fragment properties.\n",
954 Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530955 }
Urvang Joshia00a3da2012-10-31 17:49:15 -0700956 err = WebPMuxPushFrame(mux, &frgm, 1);
957 WebPDataClear(&frgm.bitstream);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530958 if (err != WEBP_MUX_OK) {
Urvang Joshia00a3da2012-10-31 17:49:15 -0700959 ERROR_GOTO3("ERROR (%s): Could not add a fragment at index %d.\n",
skal0d19fbf2013-01-21 17:20:14 +0100960 ErrorString(err), i, Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530961 }
962 }
963 break;
skal0d19fbf2013-01-21 17:20:14 +0100964 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530965
966 case FEATURE_ICCP:
Urvang Joshif903cba2012-10-31 16:30:41 -0700967 case FEATURE_EXIF:
skal0d19fbf2013-01-21 17:20:14 +0100968 case FEATURE_XMP: {
James Zern061263a2012-05-11 16:00:57 -0700969 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530970 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700971 ok = ReadFileToWebPData(feature->args_[0].filename_, &chunk);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530972 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700973 err = WebPMuxSetChunk(mux, kFourccList[feature->type_], &chunk, 1);
974 free((void*)chunk.bytes);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530975 if (err != WEBP_MUX_OK) {
Urvang Joshif903cba2012-10-31 16:30:41 -0700976 ERROR_GOTO3("ERROR (%s): Could not set the %s.\n",
977 ErrorString(err), kDescriptions[feature->type_], Err2);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530978 }
979 break;
skal0d19fbf2013-01-21 17:20:14 +0100980 }
981 default: {
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530982 ERROR_GOTO1("ERROR: Invalid feature for action 'set'.\n", Err2);
983 break;
skal0d19fbf2013-01-21 17:20:14 +0100984 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530985 }
986 ok = WriteWebP(mux, config->output_);
987 break;
skal0d19fbf2013-01-21 17:20:14 +0100988 }
989 case ACTION_STRIP: {
James Zern061263a2012-05-11 16:00:57 -0700990 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +0530991 if (!ok) goto Err2;
Urvang Joshif903cba2012-10-31 16:30:41 -0700992 if (feature->type_ == FEATURE_ICCP || feature->type_ == FEATURE_EXIF ||
993 feature->type_ == FEATURE_XMP) {
994 err = WebPMuxDeleteChunk(mux, kFourccList[feature->type_]);
995 if (err != WEBP_MUX_OK) {
996 ERROR_GOTO3("ERROR (%s): Could not strip the %s.\n",
997 ErrorString(err), kDescriptions[feature->type_], Err2);
998 }
999 } else {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301000 ERROR_GOTO1("ERROR: Invalid feature for action 'strip'.\n", Err2);
1001 break;
1002 }
1003 ok = WriteWebP(mux, config->output_);
1004 break;
skal0d19fbf2013-01-21 17:20:14 +01001005 }
1006 case ACTION_INFO: {
James Zern061263a2012-05-11 16:00:57 -07001007 ok = CreateMux(config->input_, &mux);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301008 if (!ok) goto Err2;
1009 ok = (DisplayInfo(mux) == WEBP_MUX_OK);
1010 break;
skal0d19fbf2013-01-21 17:20:14 +01001011 }
1012 default: {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301013 assert(0); // Invalid action.
1014 break;
skal0d19fbf2013-01-21 17:20:14 +01001015 }
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301016 }
1017
1018 Err2:
1019 WebPMuxDelete(mux);
1020 return ok;
1021}
1022
1023//------------------------------------------------------------------------------
1024// Main.
1025
1026int main(int argc, const char* argv[]) {
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301027 WebPMuxConfig* config;
James Zern974aaff2012-01-24 12:46:46 -08001028 int ok = InitializeConfig(argc - 1, argv + 1, &config);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301029 if (ok) {
James Zern974aaff2012-01-24 12:46:46 -08001030 ok = Process(config);
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301031 } else {
1032 PrintHelp();
1033 }
1034 DeleteConfig(config);
James Zern974aaff2012-01-24 12:46:46 -08001035 return !ok;
Urvang Joshia4f32ca2011-09-30 11:07:01 +05301036}
1037
1038//------------------------------------------------------------------------------