Path: blob/master/3rdparty/libwebp/src/utils/utils.c
16358 views
// Copyright 2012 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Misc. common utility functions10//11// Author: Skal ([email protected])1213#include <stdlib.h>14#include <string.h> // for memcpy()15#include "src/webp/decode.h"16#include "src/webp/encode.h"17#include "src/webp/format_constants.h" // for MAX_PALETTE_SIZE18#include "src/utils/color_cache_utils.h"19#include "src/utils/utils.h"2021// If PRINT_MEM_INFO is defined, extra info (like total memory used, number of22// alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,23// and not multi-thread safe!).24// An interesting alternative is valgrind's 'massif' tool:25// http://valgrind.org/docs/manual/ms-manual.html26// Here is an example command line:27/* valgrind --tool=massif --massif-out-file=massif.out \28--stacks=yes --alloc-fn=WebPSafeMalloc --alloc-fn=WebPSafeCalloc29ms_print massif.out30*/31// In addition:32// * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles33// are printed.34// * if MALLOC_FAIL_AT is defined, the global environment variable35// $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc36// is called for the nth time. Example usage:37// export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png38// * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT39// sets the maximum amount of memory (in bytes) made available to libwebp.40// This can be used to emulate environment with very limited memory.41// Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp4243// #define PRINT_MEM_INFO44// #define PRINT_MEM_TRAFFIC45// #define MALLOC_FAIL_AT46// #define MALLOC_LIMIT4748//------------------------------------------------------------------------------49// Checked memory allocation5051#if defined(PRINT_MEM_INFO)5253#include <stdio.h>5455static int num_malloc_calls = 0;56static int num_calloc_calls = 0;57static int num_free_calls = 0;58static int countdown_to_fail = 0; // 0 = off5960typedef struct MemBlock MemBlock;61struct MemBlock {62void* ptr_;63size_t size_;64MemBlock* next_;65};6667static MemBlock* all_blocks = NULL;68static size_t total_mem = 0;69static size_t total_mem_allocated = 0;70static size_t high_water_mark = 0;71static size_t mem_limit = 0;7273static int exit_registered = 0;7475static void PrintMemInfo(void) {76fprintf(stderr, "\nMEMORY INFO:\n");77fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);78fprintf(stderr, " calloc = %4d\n", num_calloc_calls);79fprintf(stderr, " free = %4d\n", num_free_calls);80fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);81fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);82fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);83while (all_blocks != NULL) {84MemBlock* b = all_blocks;85all_blocks = b->next_;86free(b);87}88}8990static void Increment(int* const v) {91if (!exit_registered) {92#if defined(MALLOC_FAIL_AT)93{94const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");95if (malloc_fail_at_str != NULL) {96countdown_to_fail = atoi(malloc_fail_at_str);97}98}99#endif100#if defined(MALLOC_LIMIT)101{102const char* const malloc_limit_str = getenv("MALLOC_LIMIT");103if (malloc_limit_str != NULL) {104mem_limit = atoi(malloc_limit_str);105}106}107#endif108(void)countdown_to_fail;109(void)mem_limit;110atexit(PrintMemInfo);111exit_registered = 1;112}113++*v;114}115116static void AddMem(void* ptr, size_t size) {117if (ptr != NULL) {118MemBlock* const b = (MemBlock*)malloc(sizeof(*b));119if (b == NULL) abort();120b->next_ = all_blocks;121all_blocks = b;122b->ptr_ = ptr;123b->size_ = size;124total_mem += size;125total_mem_allocated += size;126#if defined(PRINT_MEM_TRAFFIC)127#if defined(MALLOC_FAIL_AT)128fprintf(stderr, "fail-count: %5d [mem=%u]\n",129num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);130#else131fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);132#endif133#endif134if (total_mem > high_water_mark) high_water_mark = total_mem;135}136}137138static void SubMem(void* ptr) {139if (ptr != NULL) {140MemBlock** b = &all_blocks;141// Inefficient search, but that's just for debugging.142while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_;143if (*b == NULL) {144fprintf(stderr, "Invalid pointer free! (%p)\n", ptr);145abort();146}147{148MemBlock* const block = *b;149*b = block->next_;150total_mem -= block->size_;151#if defined(PRINT_MEM_TRAFFIC)152fprintf(stderr, "Mem: %u (-%u)\n",153(uint32_t)total_mem, (uint32_t)block->size_);154#endif155free(block);156}157}158}159160#else161#define Increment(v) do {} while (0)162#define AddMem(p, s) do {} while (0)163#define SubMem(p) do {} while (0)164#endif165166// Returns 0 in case of overflow of nmemb * size.167static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {168const uint64_t total_size = nmemb * size;169if (nmemb == 0) return 1;170if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;171if (total_size != (size_t)total_size) return 0;172#if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)173if (countdown_to_fail > 0 && --countdown_to_fail == 0) {174return 0; // fake fail!175}176#endif177#if defined(MALLOC_LIMIT)178if (mem_limit > 0) {179const uint64_t new_total_mem = (uint64_t)total_mem + total_size;180if (new_total_mem != (size_t)new_total_mem ||181new_total_mem > mem_limit) {182return 0; // fake fail!183}184}185#endif186187return 1;188}189190void* WebPSafeMalloc(uint64_t nmemb, size_t size) {191void* ptr;192Increment(&num_malloc_calls);193if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;194assert(nmemb * size > 0);195ptr = malloc((size_t)(nmemb * size));196AddMem(ptr, (size_t)(nmemb * size));197return ptr;198}199200void* WebPSafeCalloc(uint64_t nmemb, size_t size) {201void* ptr;202Increment(&num_calloc_calls);203if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;204assert(nmemb * size > 0);205ptr = calloc((size_t)nmemb, size);206AddMem(ptr, (size_t)(nmemb * size));207return ptr;208}209210void WebPSafeFree(void* const ptr) {211if (ptr != NULL) {212Increment(&num_free_calls);213SubMem(ptr);214}215free(ptr);216}217218// Public API function.219void WebPFree(void* ptr) {220free(ptr);221}222223//------------------------------------------------------------------------------224225void WebPCopyPlane(const uint8_t* src, int src_stride,226uint8_t* dst, int dst_stride, int width, int height) {227assert(src != NULL && dst != NULL);228assert(src_stride >= width && dst_stride >= width);229while (height-- > 0) {230memcpy(dst, src, width);231src += src_stride;232dst += dst_stride;233}234}235236void WebPCopyPixels(const WebPPicture* const src, WebPPicture* const dst) {237assert(src != NULL && dst != NULL);238assert(src->width == dst->width && src->height == dst->height);239assert(src->use_argb && dst->use_argb);240WebPCopyPlane((uint8_t*)src->argb, 4 * src->argb_stride, (uint8_t*)dst->argb,2414 * dst->argb_stride, 4 * src->width, src->height);242}243244//------------------------------------------------------------------------------245246#define COLOR_HASH_SIZE (MAX_PALETTE_SIZE * 4)247#define COLOR_HASH_RIGHT_SHIFT 22 // 32 - log2(COLOR_HASH_SIZE).248249int WebPGetColorPalette(const WebPPicture* const pic, uint32_t* const palette) {250int i;251int x, y;252int num_colors = 0;253uint8_t in_use[COLOR_HASH_SIZE] = { 0 };254uint32_t colors[COLOR_HASH_SIZE];255const uint32_t* argb = pic->argb;256const int width = pic->width;257const int height = pic->height;258uint32_t last_pix = ~argb[0]; // so we're sure that last_pix != argb[0]259assert(pic != NULL);260assert(pic->use_argb);261262for (y = 0; y < height; ++y) {263for (x = 0; x < width; ++x) {264int key;265if (argb[x] == last_pix) {266continue;267}268last_pix = argb[x];269key = VP8LHashPix(last_pix, COLOR_HASH_RIGHT_SHIFT);270while (1) {271if (!in_use[key]) {272colors[key] = last_pix;273in_use[key] = 1;274++num_colors;275if (num_colors > MAX_PALETTE_SIZE) {276return MAX_PALETTE_SIZE + 1; // Exact count not needed.277}278break;279} else if (colors[key] == last_pix) {280break; // The color is already there.281} else {282// Some other color sits here, so do linear conflict resolution.283++key;284key &= (COLOR_HASH_SIZE - 1); // Key mask.285}286}287}288argb += pic->argb_stride;289}290291if (palette != NULL) { // Fill the colors into palette.292num_colors = 0;293for (i = 0; i < COLOR_HASH_SIZE; ++i) {294if (in_use[i]) {295palette[num_colors] = colors[i];296++num_colors;297}298}299}300return num_colors;301}302303#undef COLOR_HASH_SIZE304#undef COLOR_HASH_RIGHT_SHIFT305306//------------------------------------------------------------------------------307308#if defined(WEBP_NEED_LOG_TABLE_8BIT)309const uint8_t WebPLogTable8bit[256] = { // 31 ^ clz(i)3100, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,3114, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,3125, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,3135, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,3146, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,3156, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,3166, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,3176, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,3187, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3197, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3207, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3217, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3227, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3237, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3247, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,3257, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7326};327#endif328329//------------------------------------------------------------------------------330331332