blob: 38a7d2e2487487c9ebcbeb6aff3af0f4e2a2704b [file] [log] [blame]
Pascal Massiminof61d14a2011-02-18 23:33:46 -08001// Copyright 2011 Google Inc.
2//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// simple command line calling the WebPEncode function.
9// Encodes a raw .YUV into WebP bitstream
10//
11// Author: Skal (pascal.massimino@gmail.com)
12
13#include <stdio.h>
Pascal Massimino4b0b0d62011-03-26 09:27:45 -070014#include <stdlib.h>
Pascal Massiminof61d14a2011-02-18 23:33:46 -080015#include <string.h>
16
James Zern31f9dc62011-06-06 17:56:50 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Pascal Massiminof61d14a2011-02-18 23:33:46 -080021#ifdef WEBP_HAVE_PNG
22#include <png.h>
23#endif
24
25#ifdef WEBP_HAVE_JPEG
26#include <setjmp.h> // note: this must be included *after* png.h
27#include <jpeglib.h>
28#endif
29
James Zern31f9dc62011-06-06 17:56:50 -070030#ifdef HAVE_WINCODEC_H
31#ifdef __MINGW32__
32#define INITGUID // Without this GUIDs are declared extern and fail to link
33#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -080034#define CINTERFACE
35#define COBJMACROS
36#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
37 // code with COBJMACROS.
38#include <shlwapi.h>
39#include <windows.h>
40#include <wincodec.h>
James Zern31f9dc62011-06-06 17:56:50 -070041
42#ifndef GUID_WICPixelFormat24bppRGB
43// From Microsoft SDK 7.0a
44DEFINE_GUID(GUID_WICPixelFormat24bppRGB,
45 0x6fddc324, 0x4e03, 0x4bfe, 0xb1, 0x85, 0x3d, 0x77, 0x76, 0x8d, 0xc9, 0x0d);
Pascal Massiminof61d14a2011-02-18 23:33:46 -080046#endif
James Zern3cfe0882011-06-21 18:29:30 -070047#ifndef GUID_WICPixelFormat32bppRGBA
48DEFINE_GUID(GUID_WICPixelFormat32bppRGBA,
49 0xf5c7ad2d, 0x6a8d, 0x43dd, 0xa7, 0xa8, 0xa2, 0x99, 0x35, 0x26, 0x1a, 0xe9);
50#endif
James Zern31f9dc62011-06-06 17:56:50 -070051#endif /* HAVE_WINCODEC_H */
Pascal Massiminof61d14a2011-02-18 23:33:46 -080052
53
54#include "webp/encode.h"
55#include "stopwatch.h"
Pascal Massiminoa11009d2011-06-10 15:10:18 -070056extern void* VP8EncGetCPUInfo; // opaque forward declaration.
Pascal Massiminocfbf88a2011-04-22 12:14:45 -070057
Pascal Massiminof61d14a2011-02-18 23:33:46 -080058//-----------------------------------------------------------------------------
59
60static int verbose = 0;
61
62static int ReadYUV(FILE* in_file, WebPPicture* const pic) {
63 const int uv_width = (pic->width + 1) / 2;
64 const int uv_height = (pic->height + 1) / 2;
65 int y;
66 int ok = 0;
67
68 if (!WebPPictureAlloc(pic)) return ok;
69
70 for (y = 0; y < pic->height; ++y) {
71 if (fread(pic->y + y * pic->y_stride, pic->width, 1, in_file) != 1) {
72 goto End;
73 }
74 }
75 for (y = 0; y < uv_height; ++y) {
76 if (fread(pic->u + y * pic->uv_stride, uv_width, 1, in_file) != 1)
77 goto End;
78 }
79 for (y = 0; y < uv_height; ++y) {
80 if (fread(pic->v + y * pic->uv_stride, uv_width, 1, in_file) != 1)
81 goto End;
82 }
83 ok = 1;
84
85 End:
86 return ok;
87}
88
James Zern31f9dc62011-06-06 17:56:50 -070089#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -080090
91#define IFS(fn) \
92 do { \
93 if (SUCCEEDED(hr)) \
94 { \
95 hr = (fn); \
96 if (FAILED(hr) && verbose) \
97 printf(#fn " failed %08x\n", hr); \
98 } \
99 } while (0)
100
101#ifdef __cplusplus
102#define MAKE_REFGUID(x) (x)
103#else
104#define MAKE_REFGUID(x) &(x)
105#endif
106
107static HRESULT OpenInputStream(const char* filename, IStream** ppStream) {
108 HRESULT hr = S_OK;
109 IFS(SHCreateStreamOnFileA(filename, STGM_READ, ppStream));
110 if (FAILED(hr))
111 printf("Error opening input file %s (%08x)\n", filename, hr);
112 return hr;
113}
114
115static HRESULT ReadPictureWithWIC(const char* filename,
James Zern3cfe0882011-06-21 18:29:30 -0700116 WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800117 HRESULT hr = S_OK;
118 IWICBitmapFrameDecode* pFrame = NULL;
119 IWICFormatConverter* pConverter = NULL;
120 IWICImagingFactory* pFactory = NULL;
121 IWICBitmapDecoder* pDecoder = NULL;
122 IStream* pStream = NULL;
123 UINT frameCount = 0;
124 UINT width, height = 0;
125 BYTE* rgb = NULL;
James Zern3cfe0882011-06-21 18:29:30 -0700126 WICPixelFormatGUID srcPixelFormat = { 0 };
127 GUID srcContainerFormat = { 0 };
128 const GUID* alphaContainers[] = {
129 &GUID_ContainerFormatBmp,
130 &GUID_ContainerFormatPng,
131 &GUID_ContainerFormatTiff
132 };
133 int has_alpha = 0;
134 int i, stride;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800135
136 IFS(CoInitialize(NULL));
137 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
138 CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
139 (LPVOID*)&pFactory));
140 if (hr == REGDB_E_CLASSNOTREG) {
141 printf("Couldn't access Windows Imaging Component (are you running \n");
142 printf("Windows XP SP3 or newer?). Most formats not available.\n");
143 printf("Use -s for the available YUV input.\n");
144 }
145 // Prepare for image decoding.
146 IFS(OpenInputStream(filename, &pStream));
147 IFS(IWICImagingFactory_CreateDecoderFromStream(pFactory, pStream, NULL,
148 WICDecodeMetadataCacheOnDemand, &pDecoder));
149 IFS(IWICBitmapDecoder_GetFrameCount(pDecoder, &frameCount));
150 if (SUCCEEDED(hr) && frameCount == 0) {
151 printf("No frame found in input file.\n");
152 hr = E_FAIL;
153 }
154 IFS(IWICBitmapDecoder_GetFrame(pDecoder, 0, &pFrame));
James Zern3cfe0882011-06-21 18:29:30 -0700155 IFS(IWICBitmapFrameDecode_GetPixelFormat(pFrame, &srcPixelFormat));
156 IFS(IWICBitmapDecoder_GetContainerFormat(pDecoder, &srcContainerFormat));
157
158 has_alpha = keep_alpha;
159 for (i = 0;
160 has_alpha && i < sizeof(alphaContainers)/sizeof(alphaContainers[0]);
161 ++i) {
162 if (IsEqualGUID(&srcContainerFormat, alphaContainers[i])) {
163 has_alpha =
164 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppRGBA) ||
165 IsEqualGUID(&srcPixelFormat, &GUID_WICPixelFormat32bppBGRA);
166 break;
167 }
168 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800169
170 // Prepare for pixel format conversion (if necessary).
171 IFS(IWICImagingFactory_CreateFormatConverter(pFactory, &pConverter));
172 IFS(IWICFormatConverter_Initialize(pConverter, (IWICBitmapSource*)pFrame,
James Zern3cfe0882011-06-21 18:29:30 -0700173 has_alpha ? MAKE_REFGUID(GUID_WICPixelFormat32bppRGBA)
174 : MAKE_REFGUID(GUID_WICPixelFormat24bppRGB),
175 WICBitmapDitherTypeNone,
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800176 NULL, 0.0, WICBitmapPaletteTypeCustom));
177
178 // Decode.
179 IFS(IWICFormatConverter_GetSize(pConverter, &width, &height));
James Zern3cfe0882011-06-21 18:29:30 -0700180 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800181 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700182 rgb = (BYTE*)malloc(stride * height);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800183 if (rgb == NULL)
184 hr = E_OUTOFMEMORY;
185 }
James Zern3cfe0882011-06-21 18:29:30 -0700186 IFS(IWICFormatConverter_CopyPixels(pConverter, NULL, stride,
187 stride * height, rgb));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800188
189 // WebP conversion.
190 if (SUCCEEDED(hr)) {
James Zern3cfe0882011-06-21 18:29:30 -0700191 int ok;
192#ifdef WEBP_EXPERIMENTAL_FEATURES
193 if (has_alpha) {
194 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
195 }
196#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800197 pic->width = width;
198 pic->height = height;
James Zern3cfe0882011-06-21 18:29:30 -0700199 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
200 : WebPPictureImportRGB(pic, rgb, stride);
201 if (!ok)
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800202 hr = E_FAIL;
203 }
204
205 // Cleanup.
206 if (pConverter != NULL) IUnknown_Release(pConverter);
207 if (pFrame != NULL) IUnknown_Release(pFrame);
208 if (pDecoder != NULL) IUnknown_Release(pDecoder);
209 if (pFactory != NULL) IUnknown_Release(pFactory);
210 if (pStream != NULL) IUnknown_Release(pStream);
211 free(rgb);
212 return hr;
213}
214
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700215static int ReadPicture(const char* const filename, WebPPicture* const pic,
216 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800217 int ok;
218 if (pic->width != 0 && pic->height != 0) {
219 // If image size is specified, infer it as YUV format.
220 FILE* in_file = fopen(filename, "rb");
221 if (in_file == NULL) {
222 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
223 return 0;
224 }
225 ok = ReadYUV(in_file, pic);
226 fclose(in_file);
227 } else {
228 // If no size specified, try to decode it using WIC.
James Zern3cfe0882011-06-21 18:29:30 -0700229 ok = SUCCEEDED(ReadPictureWithWIC(filename, pic, keep_alpha));
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800230 }
231 if (!ok) {
232 fprintf(stderr, "Error! Could not process file %s\n", filename);
233 }
234 return ok;
235}
236
James Zern31f9dc62011-06-06 17:56:50 -0700237#else // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800238
239#ifdef WEBP_HAVE_JPEG
240struct my_error_mgr {
241 struct jpeg_error_mgr pub;
242 jmp_buf setjmp_buffer;
243};
244
245static void my_error_exit(j_common_ptr dinfo) {
246 struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
247 (*dinfo->err->output_message) (dinfo);
248 longjmp(myerr->setjmp_buffer, 1);
249}
250
251static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
252 int ok = 0;
253 int stride, width, height;
254 uint8_t* rgb = NULL;
255 uint8_t* row_ptr = NULL;
256 struct jpeg_decompress_struct dinfo;
257 struct my_error_mgr jerr;
258 JSAMPARRAY buffer;
259
260 dinfo.err = jpeg_std_error(&jerr.pub);
261 jerr.pub.error_exit = my_error_exit;
262
263 if (setjmp (jerr.setjmp_buffer)) {
264 Error:
265 jpeg_destroy_decompress(&dinfo);
266 goto End;
267 }
268
269 jpeg_create_decompress(&dinfo);
270 jpeg_stdio_src(&dinfo, in_file);
271 jpeg_read_header(&dinfo, TRUE);
272
273 dinfo.out_color_space = JCS_RGB;
274 dinfo.dct_method = JDCT_IFAST;
275 dinfo.do_fancy_upsampling = TRUE;
276
277 jpeg_start_decompress(&dinfo);
278
279 if (dinfo.output_components != 3) {
280 goto Error;
281 }
282
283 width = dinfo.output_width;
284 height = dinfo.output_height;
285 stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
286
287 rgb = (uint8_t*)malloc(stride * height);
288 if (rgb == NULL) {
289 goto End;
290 }
291 row_ptr = rgb;
292
293 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
294 JPOOL_IMAGE, stride, 1);
295 if (buffer == NULL) {
296 goto End;
297 }
298
299 while (dinfo.output_scanline < dinfo.output_height) {
300 if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
301 goto End;
302 }
303 memcpy(row_ptr, buffer[0], stride);
304 row_ptr += stride;
305 }
306
307 jpeg_finish_decompress (&dinfo);
308 jpeg_destroy_decompress (&dinfo);
309
310 // WebP conversion.
311 pic->width = width;
312 pic->height = height;
313 ok = WebPPictureImportRGB(pic, rgb, stride);
314
315 End:
316 if (rgb) {
317 free(rgb);
318 }
319 return ok;
320}
321
322#else
323static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
324 printf("JPEG support not compiled. Please install the libjpeg development "
325 "package before building.\n");
326 return 0;
327}
328#endif
329
330#ifdef WEBP_HAVE_PNG
331static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700332 (void)dummy; // remove variable-unused warning
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800333 longjmp(png_jmpbuf(png), 1);
334}
335
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700336static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800337 png_structp png;
338 png_infop info;
339 int color_type, bit_depth, interlaced;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700340 int has_alpha;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800341 int num_passes;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700342 int p;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800343 int ok = 0;
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700344 png_uint_32 width, height, y;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800345 int stride;
346 uint8_t* rgb = NULL;
347
348 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
349 if (png == NULL) {
350 goto End;
351 }
352
353 png_set_error_fn(png, 0, error_function, NULL);
354 if (setjmp(png_jmpbuf(png))) {
355 Error:
356 png_destroy_read_struct(&png, NULL, NULL);
357 if (rgb) free(rgb);
358 goto End;
359 }
360
361 info = png_create_info_struct(png);
362 if (info == NULL) goto Error;
363
364 png_init_io(png, in_file);
365 png_read_info(png, info);
366 if (!png_get_IHDR(png, info,
367 &width, &height, &bit_depth, &color_type, &interlaced,
368 NULL, NULL)) goto Error;
369
370 png_set_strip_16(png);
371 png_set_packing(png);
372 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
373 if (color_type == PNG_COLOR_TYPE_GRAY) {
374 if (bit_depth < 8) {
375 png_set_expand_gray_1_2_4_to_8(png);
376 }
377 png_set_gray_to_rgb(png);
378 }
379 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
380 png_set_tRNS_to_alpha(png);
381 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700382 has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800383
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700384 if (!keep_alpha) {
385 png_set_strip_alpha(png);
386 has_alpha = 0;
387 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700388#ifdef WEBP_EXPERIMENTAL_FEATURES
389 if (has_alpha) {
390 pic->colorspace |= WEBP_CSP_ALPHA_BIT;
391 }
392#endif
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700393
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800394 num_passes = png_set_interlace_handling(png);
395 png_read_update_info(png, info);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700396 stride = (has_alpha ? 4 : 3) * width * sizeof(*rgb);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800397 rgb = (uint8_t*)malloc(stride * height);
398 if (rgb == NULL) goto Error;
399 for (p = 0; p < num_passes; ++p) {
400 for (y = 0; y < height; ++y) {
401 png_bytep row = rgb + y * stride;
402 png_read_rows(png, &row, NULL, 1);
403 }
404 }
405 png_read_end(png, info);
406 png_destroy_read_struct(&png, &info, NULL);
407
408 pic->width = width;
409 pic->height = height;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700410 ok = has_alpha ? WebPPictureImportRGBA(pic, rgb, stride)
411 : WebPPictureImportRGB(pic, rgb, stride);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800412 free(rgb);
413
414 End:
415 return ok;
416}
417#else
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700418static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800419 printf("PNG support not compiled. Please install the libpng development "
420 "package before building.\n");
421 return 0;
422}
423#endif
424
425typedef enum {
426 PNG = 0,
427 JPEG,
428 UNSUPPORTED,
429} InputFileFormat;
430
431static InputFileFormat GetImageType(FILE* in_file) {
432 InputFileFormat format = UNSUPPORTED;
433 unsigned int magic;
434 unsigned char buf[4];
435
436 if ((fread(&buf[0], 4, 1, in_file) != 1) ||
437 (fseek(in_file, 0, SEEK_SET) != 0)) {
438 return format;
439 }
440
441 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
442 if (magic == 0x89504E47U) {
443 format = PNG;
444 } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) {
445 format = JPEG;
446 }
447 return format;
448}
449
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700450static int ReadPicture(const char* const filename, WebPPicture* const pic,
451 int keep_alpha) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800452 int ok = 0;
453 FILE* in_file = fopen(filename, "rb");
454 if (in_file == NULL) {
455 fprintf(stderr, "Error! Cannot open input file '%s'\n", filename);
456 return ok;
457 }
458
459 if (pic->width == 0 || pic->height == 0) {
460 // If no size specified, try to decode it as PNG/JPEG (as appropriate).
461 const InputFileFormat format = GetImageType(in_file);
462 if (format == PNG) {
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700463 ok = ReadPNG(in_file, pic, keep_alpha);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800464 } else if (format == JPEG) {
465 ok = ReadJPEG(in_file, pic);
466 }
467 } else {
468 // If image size is specified, infer it as YUV format.
469 ok = ReadYUV(in_file, pic);
470 }
471 if (!ok) {
472 fprintf(stderr, "Error! Could not process file %s\n", filename);
473 }
474
475 fclose(in_file);
476 return ok;
477}
478
James Zern31f9dc62011-06-06 17:56:50 -0700479#endif // !HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800480
481static void AllocExtraInfo(WebPPicture* const pic) {
482 const int mb_w = (pic->width + 15) / 16;
483 const int mb_h = (pic->height + 15) / 16;
484 pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
485}
486
487static void PrintByteCount(const int bytes[4], int total_size,
488 int* const totals) {
489 int s;
490 int total = 0;
491 for (s = 0; s < 4; ++s) {
492 fprintf(stderr, "| %7d ", bytes[s]);
493 total += bytes[s];
494 if (totals) totals[s] += bytes[s];
495 }
496 fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size);
497}
498
499static void PrintPercents(const int counts[4], int total) {
500 int s;
501 for (s = 0; s < 4; ++s) {
502 fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
503 }
504 fprintf(stderr,"| %7d\n", total);
505}
506
507static void PrintValues(const int values[4]) {
508 int s;
509 for (s = 0; s < 4; ++s) {
510 fprintf(stderr, "| %7d ", values[s]);
511 }
512 fprintf(stderr,"|\n");
513}
514
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700515static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800516 const WebPAuxStats* const stats = pic->stats;
517 if (short_output) {
518 fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
519 } else{
520 const int num_i4 = stats->block_count[0];
521 const int num_i16 = stats->block_count[1];
522 const int num_skip = stats->block_count[2];
523 const int total = num_i4 + num_i16;
524 fprintf(stderr,
525 "%7d bytes Y-U-V-All-PSNR %2.2f %2.2f %2.2f %2.2f dB\n",
526 stats->coded_size,
527 stats->PSNR[0], stats->PSNR[1], stats->PSNR[2], stats->PSNR[3]);
528 if (total > 0) {
529 int totals[4] = { 0, 0, 0, 0 };
530 fprintf(stderr, "block count: intra4: %d\n"
531 " intra16: %d (-> %.2f%%)\n",
532 num_i4, num_i16, 100.f * num_i16 / total);
533 fprintf(stderr, " skipped block: %d (%.2f%%)\n",
534 num_skip, 100.f * num_skip / total);
535 fprintf(stderr, "bytes used: header: %6d (%.1f%%)\n"
536 " mode-partition: %6d (%.1f%%)\n",
537 stats->header_bytes[0],
538 100.f * stats->header_bytes[0] / stats->coded_size,
539 stats->header_bytes[1],
540 100.f * stats->header_bytes[1] / stats->coded_size);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700541 if (stats->alpha_data_size) {
542 fprintf(stderr, " transparency: %6d\n",
543 stats->alpha_data_size);
544 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700545 if (stats->layer_data_size) {
546 fprintf(stderr, " enhancement: %6d\n",
547 stats->layer_data_size);
548 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800549 fprintf(stderr, " Residuals bytes "
550 "|segment 1|segment 2|segment 3"
551 "|segment 4| total\n");
552 fprintf(stderr, " intra4-coeffs: ");
553 PrintByteCount(stats->residual_bytes[0], stats->coded_size, totals);
554 fprintf(stderr, " intra16-coeffs: ");
555 PrintByteCount(stats->residual_bytes[1], stats->coded_size, totals);
556 fprintf(stderr, " chroma coeffs: ");
557 PrintByteCount(stats->residual_bytes[2], stats->coded_size, totals);
558 fprintf(stderr, " macroblocks: ");
559 PrintPercents(stats->segment_size, total);
560 fprintf(stderr, " quantizer: ");
561 PrintValues(stats->segment_quant);
562 fprintf(stderr, " filter level: ");
563 PrintValues(stats->segment_level);
564 fprintf(stderr, "------------------+---------");
565 fprintf(stderr, "+---------+---------+---------+-----------------\n");
566 fprintf(stderr, " segments total: ");
567 PrintByteCount(totals, stats->coded_size, NULL);
568 }
569 }
570 if (pic->extra_info) {
571 const int mb_w = (pic->width + 15) / 16;
572 const int mb_h = (pic->height + 15) / 16;
573 const int type = pic->extra_info_type;
574 int x, y;
575 for (y = 0; y < mb_h; ++y) {
576 for (x = 0; x < mb_w; ++x) {
577 const int c = pic->extra_info[x + y * mb_w];
578 if (type == 1) { // intra4/intra16
579 printf("%c", "+."[c]);
580 } else if (type == 2) { // segments
581 printf("%c", ".-*X"[c]);
582 } else if (type == 3) { // quantizers
583 printf("%.2d ", c);
584 } else if (type == 6 || type == 7) {
585 printf("%3d ", c);
586 } else {
587 printf("0x%.2x ", c);
588 }
589 }
590 printf("\n");
591 }
592 }
593}
594
595//-----------------------------------------------------------------------------
596
597static int MyWriter(const uint8_t* data, size_t data_size,
598 const WebPPicture* const pic) {
599 FILE* const out = (FILE*)pic->custom_ptr;
600 return data_size ? (fwrite(data, data_size, 1, out) == 1) : 1;
601}
602
603// Dumps a picture as a PGM file using the IMC4 layout.
604static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
605 int y;
606 const int uv_width = (picture->width + 1) / 2;
607 const int uv_height = (picture->height + 1) / 2;
608 const int stride = (picture->width + 1) & ~1;
609 const int height = picture->height + uv_height;
610 FILE* const f = fopen(PGM_name, "wb");
611 if (!f) return 0;
612 fprintf(f, "P5\n%d %d\n255\n", stride, height);
613 for (y = 0; y < picture->height; ++y) {
614 if (fwrite(picture->y + y * picture->y_stride, picture->width, 1, f) != 1)
615 return 0;
616 if (picture->width & 1) fputc(0, f); // pad
617 }
618 for (y = 0; y < uv_height; ++y) {
619 if (fwrite(picture->u + y * picture->uv_stride, uv_width, 1, f) != 1)
620 return 0;
621 if (fwrite(picture->v + y * picture->uv_stride, uv_width, 1, f) != 1)
622 return 0;
623 }
624 fclose(f);
625 return 1;
626}
627
628//-----------------------------------------------------------------------------
629
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700630static void HelpShort(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800631 printf("Usage:\n\n");
632 printf(" cwebp [options] -q quality input.png -o output.webp\n\n");
633 printf("where quality is between 0 (poor) to 100 (very good).\n");
634 printf("Typical value is around 80.\n\n");
635 printf("Try -longhelp for an exhaustive list of advanced options.\n");
636}
637
Pascal Massiminof8db5d52011-03-25 15:04:11 -0700638static void HelpLong(void) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800639 printf("Usage:\n");
640 printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n");
641 printf("If input size (-s) for an image is not specified, "
642 "it is assumed to be a PNG or JPEG file.\n");
James Zern31f9dc62011-06-06 17:56:50 -0700643#ifdef HAVE_WINCODEC_H
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800644 printf("Windows builds can take as input any of the files handled by WIC\n");
645#endif
646 printf("options:\n");
647 printf(" -h / -help ............ short help\n");
648 printf(" -H / -longhelp ........ long help\n");
649 printf(" -q <float> ............. quality factor (0:small..100:big)\n");
650 printf(" -preset <string> ....... Preset setting, one of:\n");
651 printf(" default, photo, picture,\n");
652 printf(" drawing, icon, text\n");
653 printf(" -preset must come first, as it overwrites other parameters.");
654 printf("\n");
655 printf(" -m <int> ............... compression method (0=fast, 6=slowest)\n");
656 printf(" -segments <int> ........ number of segments to use (1..4)\n");
Pascal Massimino38369c02011-05-04 22:59:51 -0700657 printf(" -size <int> ............ Target size (in bytes)\n");
658 printf(" -psnr <float> .......... Target PSNR (in dB. typically: 42)\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800659 printf("\n");
660 printf(" -s <int> <int> ......... Input size (width x height) for YUV\n");
661 printf(" -sns <int> ............. Spatial Noise Shaping (0:off, 100:max)\n");
662 printf(" -f <int> ............... filter strength (0=off..100)\n");
663 printf(" -sharpness <int> ....... "
664 "filter sharpness (0:most .. 7:least sharp)\n");
665 printf(" -strong ................ use strong filter instead of simple.\n");
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700666 printf(" -alpha_comp <int> ...... set the transparency-compression\n");
667 printf(" -noalpha ............... discard any transparency information.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800668 printf(" -pass <int> ............ analysis pass number (1..10)\n");
669 printf(" -crop <x> <y> <w> <h> .. crop picture with the given rectangle\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700670 printf(" -resize <w> <h> ........ resize picture (after any cropping)\n");
671#ifdef WEBP_EXPERIMENTAL_FEATURES
672 printf(" -444 / -422 / -gray ..... Change colorspace\n");
673#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800674 printf(" -map <int> ............. print map of extra info.\n");
675 printf(" -d <file.pgm> .......... dump the compressed output (PGM file).\n");
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700676
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800677 printf("\n");
678 printf(" -short ................. condense printed message\n");
679 printf(" -quiet ................. don't print anything.\n");
Pascal Massimino650ffa32011-03-24 16:17:10 -0700680 printf(" -version ............... print version number and exit.\n");
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700681 printf(" -noasm ................. disable all assembly optimizations.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800682 printf(" -v ..................... verbose, e.g. print encoding/decoding "
683 "times\n");
684 printf("\n");
685 printf("Experimental Options:\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800686 printf(" -af .................... auto-adjust filter strength.\n");
687 printf(" -pre <int> ............. pre-processing filter\n");
688 printf("\n");
689}
690
691//-----------------------------------------------------------------------------
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700692// Error messages
693
694static const char* const kErrorMessages[] = {
695 "OK",
696 "OUT_OF_MEMORY: Out of memory allocating objects",
697 "BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer",
698 "NULL_PARAMETER: NULL parameter passed to function",
699 "INVALID_CONFIGURATION: configuration is invalid",
700 "BAD_DIMENSION: Bad picture dimension",
701 "PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k",
702 "PARTITION_OVERFLOW: Partition is too big to fir 16M",
703 "BAD_WRITE: Picture writer returned an error"
704};
705
706//-----------------------------------------------------------------------------
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800707
708int main(int argc, const char *argv[]) {
709 const char *in_file = NULL, *out_file = NULL, *dump_file = NULL;
710 FILE *out = NULL;
711 int c;
712 int short_output = 0;
713 int quiet = 0;
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700714 int keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800715 int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700716 int resize_w = 0, resize_h = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800717 WebPPicture picture;
718 WebPConfig config;
719 WebPAuxStats stats;
720 Stopwatch stop_watch;
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700721
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700722#ifdef WEBP_EXPERIMENTAL_FEATURES
723 keep_alpha = 1;
724#endif
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700725
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800726 if (!WebPPictureInit(&picture) || !WebPConfigInit(&config)) {
727 fprintf(stderr, "Error! Version mismatch!\n");
728 goto Error;
729 }
730
731 if (argc == 1) {
732 HelpShort();
733 return 0;
734 }
735
736 for (c = 1; c < argc; ++c) {
737 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
738 HelpShort();
739 return 0;
740 } else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
741 HelpLong();
742 return 0;
743 } else if (!strcmp(argv[c], "-o") && c < argc - 1) {
744 out_file = argv[++c];
745 } else if (!strcmp(argv[c], "-d") && c < argc - 1) {
746 dump_file = argv[++c];
747 config.show_compressed = 1;
748 } else if (!strcmp(argv[c], "-short")) {
749 short_output++;
750 } else if (!strcmp(argv[c], "-s") && c < argc - 2) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700751 picture.width = strtol(argv[++c], NULL, 0);
752 picture.height = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800753 } else if (!strcmp(argv[c], "-m") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700754 config.method = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800755 } else if (!strcmp(argv[c], "-q") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700756 config.quality = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800757 } else if (!strcmp(argv[c], "-size") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700758 config.target_size = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800759 } else if (!strcmp(argv[c], "-psnr") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700760 config.target_PSNR = strtod(argv[++c], NULL);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800761 } else if (!strcmp(argv[c], "-sns") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700762 config.sns_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800763 } else if (!strcmp(argv[c], "-f") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700764 config.filter_strength = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800765 } else if (!strcmp(argv[c], "-af")) {
766 config.autofilter = 1;
Pascal Massimino842c0092011-05-05 18:10:08 -0700767 } else if (!strcmp(argv[c], "-strong")) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800768 config.filter_type = 1;
769 } else if (!strcmp(argv[c], "-sharpness") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700770 config.filter_sharpness = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800771 } else if (!strcmp(argv[c], "-pass") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700772 config.pass = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800773 } else if (!strcmp(argv[c], "-pre") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700774 config.preprocessing = strtol(argv[++c], NULL, 0);
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800775 } else if (!strcmp(argv[c], "-segments") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700776 config.segments = strtol(argv[++c], NULL, 0);
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700777 } else if (!strcmp(argv[c], "-alpha_comp") && c < argc - 1) {
778 config.alpha_compression = strtol(argv[++c], NULL, 0);
779 } else if (!strcmp(argv[c], "-noalpha")) {
780 keep_alpha = 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800781 } else if (!strcmp(argv[c], "-map") && c < argc - 1) {
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700782 picture.extra_info_type = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700783#ifdef WEBP_EXPERIMENTAL_FEATURES
784 } else if (!strcmp(argv[c], "-444")) {
785 picture.colorspace = WEBP_YUV444;
786 } else if (!strcmp(argv[c], "-422")) {
787 picture.colorspace = WEBP_YUV422;
788 } else if (!strcmp(argv[c], "-gray")) {
789 picture.colorspace = WEBP_YUV400;
790#endif
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800791 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
792 crop = 1;
Pascal Massimino4b0b0d62011-03-26 09:27:45 -0700793 crop_x = strtol(argv[++c], NULL, 0);
794 crop_y = strtol(argv[++c], NULL, 0);
795 crop_w = strtol(argv[++c], NULL, 0);
796 crop_h = strtol(argv[++c], NULL, 0);
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700797 } else if (!strcmp(argv[c], "-resize") && c < argc - 2) {
798 resize_w = strtol(argv[++c], NULL, 0);
799 resize_h = strtol(argv[++c], NULL, 0);
Pascal Massiminocfbf88a2011-04-22 12:14:45 -0700800 } else if (!strcmp(argv[c], "-noasm")) {
Pascal Massiminoa11009d2011-06-10 15:10:18 -0700801 VP8EncGetCPUInfo = NULL;
Pascal Massimino650ffa32011-03-24 16:17:10 -0700802 } else if (!strcmp(argv[c], "-version")) {
803 const int version = WebPGetEncoderVersion();
804 printf("%d.%d.%d\n",
805 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
806 return 0;
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800807 } else if (!strcmp(argv[c], "-quiet")) {
808 quiet = 1;
809 } else if (!strcmp(argv[c], "-preset") && c < argc - 1) {
810 WebPPreset preset;
811 ++c;
812 if (!strcmp(argv[c], "default")) {
813 preset = WEBP_PRESET_DEFAULT;
814 } else if (!strcmp(argv[c], "photo")) {
815 preset = WEBP_PRESET_PHOTO;
816 } else if (!strcmp(argv[c], "picture")) {
817 preset = WEBP_PRESET_PICTURE;
818 } else if (!strcmp(argv[c], "drawing")) {
819 preset = WEBP_PRESET_DRAWING;
820 } else if (!strcmp(argv[c], "icon")) {
821 preset = WEBP_PRESET_ICON;
822 } else if (!strcmp(argv[c], "text")) {
823 preset = WEBP_PRESET_TEXT;
824 } else {
825 fprintf(stderr, "Error! Unrecognized preset: %s\n", argv[c]);
826 goto Error;
827 }
828 if (!WebPConfigPreset(&config, preset, config.quality)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700829 fprintf(stderr, "Error! Could initialize configuration with preset.\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800830 goto Error;
831 }
832 } else if (!strcmp(argv[c], "-v")) {
833 verbose = 1;
834 } else if (argv[c][0] == '-') {
835 fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
836 HelpLong();
837 return -1;
838 } else {
839 in_file = argv[c];
840 }
841 }
842
843 if (!WebPValidateConfig(&config)) {
844 fprintf(stderr, "Error! Invalid configuration.\n");
845 goto Error;
846 }
847
Pascal Massimino0744e842011-02-25 12:03:27 -0800848 // Read the input
Pascal Massimino38369c02011-05-04 22:59:51 -0700849 if (verbose) {
Pascal Massimino0744e842011-02-25 12:03:27 -0800850 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700851 }
Pascal Massimino2ab4b722011-04-25 16:58:04 -0700852 if (!ReadPicture(in_file, &picture, keep_alpha)) {
Pascal Massimino6d978a62011-03-17 14:49:19 -0700853 fprintf(stderr, "Error! Cannot read input picture\n");
854 goto Error;
855 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800856 if (verbose) {
857 const double time = StopwatchReadAndReset(&stop_watch);
858 fprintf(stderr, "Time to read input: %.3fs\n", time);
859 }
860
861 // Open the output
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800862 if (out_file) {
863 out = fopen(out_file, "wb");
864 if (!out) {
865 fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
866 goto Error;
867 } else {
868 if (!short_output && !quiet) {
869 fprintf(stderr, "Saving file '%s'\n", out_file);
870 }
871 }
872 picture.writer = MyWriter;
873 picture.custom_ptr = (void*)out;
874 } else {
875 out = NULL;
876 if (!quiet && !short_output) {
877 fprintf(stderr, "No output file specified (no -o flag). Encoding will\n");
878 fprintf(stderr, "be performed, but its results discarded.\n\n");
879 }
880 }
881 picture.stats = &stats;
882
Pascal Massimino0744e842011-02-25 12:03:27 -0800883 // Compress
Pascal Massimino38369c02011-05-04 22:59:51 -0700884 if (verbose) {
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800885 StopwatchReadAndReset(&stop_watch);
Pascal Massimino38369c02011-05-04 22:59:51 -0700886 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700887 if (crop != 0 && !WebPPictureCrop(&picture, crop_x, crop_y, crop_w, crop_h)) {
888 fprintf(stderr, "Error! Cannot crop picture\n");
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800889 goto Error;
Pascal Massimino6d978a62011-03-17 14:49:19 -0700890 }
Pascal Massimino6d0e66c2011-05-02 17:19:00 -0700891 if ((resize_w | resize_h) > 0) {
892 if (!WebPPictureRescale(&picture, resize_w, resize_h)) {
893 fprintf(stderr, "Error! Cannot resize picture\n");
894 goto Error;
895 }
896 }
897 if (picture.extra_info_type > 0) {
898 AllocExtraInfo(&picture);
899 }
Pascal Massimino6d978a62011-03-17 14:49:19 -0700900 if (!WebPEncode(&config, &picture)) {
901 fprintf(stderr, "Error! Cannot encode picture as WebP\n");
Pascal Massiminoca7a2fd2011-06-02 06:55:03 -0700902 fprintf(stderr, "Error code: %d (%s)\n",
903 picture.error_code, kErrorMessages[picture.error_code]);
Pascal Massimino6d978a62011-03-17 14:49:19 -0700904 goto Error;
905 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800906 if (verbose) {
907 const double time = StopwatchReadAndReset(&stop_watch);
908 fprintf(stderr, "Time to encode picture: %.3fs\n", time);
909 }
Pascal Massimino0744e842011-02-25 12:03:27 -0800910
911 // Write info
Pascal Massimino38369c02011-05-04 22:59:51 -0700912 if (dump_file) {
913 DumpPicture(&picture, dump_file);
914 }
915 if (!quiet) {
916 PrintExtraInfo(&picture, short_output);
917 }
Pascal Massiminof61d14a2011-02-18 23:33:46 -0800918
919 Error:
920 free(picture.extra_info);
921 WebPPictureFree(&picture);
922 if (out != NULL) {
923 fclose(out);
924 }
925
926 return 0;
927}
928
929//-----------------------------------------------------------------------------