utils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // 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.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Misc. common utility functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <stdlib.h>
  14. #include <string.h> // for memcpy()
  15. #include "../webp/decode.h"
  16. #include "../webp/encode.h"
  17. #include "../webp/format_constants.h" // for MAX_PALETTE_SIZE
  18. #include "./utils.h"
  19. // If PRINT_MEM_INFO is defined, extra info (like total memory used, number of
  20. // alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,
  21. // and not multi-thread safe!).
  22. // An interesting alternative is valgrind's 'massif' tool:
  23. // http://valgrind.org/docs/manual/ms-manual.html
  24. // Here is an example command line:
  25. /* valgrind --tool=massif --massif-out-file=massif.out \
  26. --stacks=yes --alloc-fn=WebPSafeMalloc --alloc-fn=WebPSafeCalloc
  27. ms_print massif.out
  28. */
  29. // In addition:
  30. // * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles
  31. // are printed.
  32. // * if MALLOC_FAIL_AT is defined, the global environment variable
  33. // $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc
  34. // is called for the nth time. Example usage:
  35. // export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png
  36. // * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT
  37. // sets the maximum amount of memory (in bytes) made available to libwebp.
  38. // This can be used to emulate environment with very limited memory.
  39. // Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp
  40. // #define PRINT_MEM_INFO
  41. // #define PRINT_MEM_TRAFFIC
  42. // #define MALLOC_FAIL_AT
  43. // #define MALLOC_LIMIT
  44. //------------------------------------------------------------------------------
  45. // Checked memory allocation
  46. #if defined(PRINT_MEM_INFO)
  47. #include <stdio.h>
  48. static int num_malloc_calls = 0;
  49. static int num_calloc_calls = 0;
  50. static int num_free_calls = 0;
  51. static int countdown_to_fail = 0; // 0 = off
  52. typedef struct MemBlock MemBlock;
  53. struct MemBlock {
  54. void* ptr_;
  55. size_t size_;
  56. MemBlock* next_;
  57. };
  58. static MemBlock* all_blocks = NULL;
  59. static size_t total_mem = 0;
  60. static size_t total_mem_allocated = 0;
  61. static size_t high_water_mark = 0;
  62. static size_t mem_limit = 0;
  63. static int exit_registered = 0;
  64. static void PrintMemInfo(void) {
  65. fprintf(stderr, "\nMEMORY INFO:\n");
  66. fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);
  67. fprintf(stderr, " calloc = %4d\n", num_calloc_calls);
  68. fprintf(stderr, " free = %4d\n", num_free_calls);
  69. fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);
  70. fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);
  71. fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);
  72. while (all_blocks != NULL) {
  73. MemBlock* b = all_blocks;
  74. all_blocks = b->next_;
  75. free(b);
  76. }
  77. }
  78. static void Increment(int* const v) {
  79. if (!exit_registered) {
  80. #if defined(MALLOC_FAIL_AT)
  81. {
  82. const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");
  83. if (malloc_fail_at_str != NULL) {
  84. countdown_to_fail = atoi(malloc_fail_at_str);
  85. }
  86. }
  87. #endif
  88. #if defined(MALLOC_LIMIT)
  89. {
  90. const char* const malloc_limit_str = getenv("MALLOC_LIMIT");
  91. if (malloc_limit_str != NULL) {
  92. mem_limit = atoi(malloc_limit_str);
  93. }
  94. }
  95. #endif
  96. (void)countdown_to_fail;
  97. (void)mem_limit;
  98. atexit(PrintMemInfo);
  99. exit_registered = 1;
  100. }
  101. ++*v;
  102. }
  103. static void AddMem(void* ptr, size_t size) {
  104. if (ptr != NULL) {
  105. MemBlock* const b = (MemBlock*)malloc(sizeof(*b));
  106. if (b == NULL) abort();
  107. b->next_ = all_blocks;
  108. all_blocks = b;
  109. b->ptr_ = ptr;
  110. b->size_ = size;
  111. total_mem += size;
  112. total_mem_allocated += size;
  113. #if defined(PRINT_MEM_TRAFFIC)
  114. #if defined(MALLOC_FAIL_AT)
  115. fprintf(stderr, "fail-count: %5d [mem=%u]\n",
  116. num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);
  117. #else
  118. fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);
  119. #endif
  120. #endif
  121. if (total_mem > high_water_mark) high_water_mark = total_mem;
  122. }
  123. }
  124. static void SubMem(void* ptr) {
  125. if (ptr != NULL) {
  126. MemBlock** b = &all_blocks;
  127. // Inefficient search, but that's just for debugging.
  128. while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_;
  129. if (*b == NULL) {
  130. fprintf(stderr, "Invalid pointer free! (%p)\n", ptr);
  131. abort();
  132. }
  133. {
  134. MemBlock* const block = *b;
  135. *b = block->next_;
  136. total_mem -= block->size_;
  137. #if defined(PRINT_MEM_TRAFFIC)
  138. fprintf(stderr, "Mem: %u (-%u)\n",
  139. (uint32_t)total_mem, (uint32_t)block->size_);
  140. #endif
  141. free(block);
  142. }
  143. }
  144. }
  145. #else
  146. #define Increment(v) do {} while (0)
  147. #define AddMem(p, s) do {} while (0)
  148. #define SubMem(p) do {} while (0)
  149. #endif
  150. // Returns 0 in case of overflow of nmemb * size.
  151. static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
  152. const uint64_t total_size = nmemb * size;
  153. if (nmemb == 0) return 1;
  154. if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
  155. if (total_size != (size_t)total_size) return 0;
  156. #if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)
  157. if (countdown_to_fail > 0 && --countdown_to_fail == 0) {
  158. return 0; // fake fail!
  159. }
  160. #endif
  161. #if defined(MALLOC_LIMIT)
  162. if (mem_limit > 0) {
  163. const uint64_t new_total_mem = (uint64_t)total_mem + total_size;
  164. if (new_total_mem != (size_t)new_total_mem ||
  165. new_total_mem > mem_limit) {
  166. return 0; // fake fail!
  167. }
  168. }
  169. #endif
  170. return 1;
  171. }
  172. void* WebPSafeMalloc(uint64_t nmemb, size_t size) {
  173. void* ptr;
  174. Increment(&num_malloc_calls);
  175. if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
  176. assert(nmemb * size > 0);
  177. ptr = malloc((size_t)(nmemb * size));
  178. AddMem(ptr, (size_t)(nmemb * size));
  179. return ptr;
  180. }
  181. void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
  182. void* ptr;
  183. Increment(&num_calloc_calls);
  184. if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
  185. assert(nmemb * size > 0);
  186. ptr = calloc((size_t)nmemb, size);
  187. AddMem(ptr, (size_t)(nmemb * size));
  188. return ptr;
  189. }
  190. void WebPSafeFree(void* const ptr) {
  191. if (ptr != NULL) {
  192. Increment(&num_free_calls);
  193. SubMem(ptr);
  194. }
  195. free(ptr);
  196. }
  197. // Public API function.
  198. void WebPFree(void* ptr) {
  199. free(ptr);
  200. }
  201. //------------------------------------------------------------------------------
  202. void WebPCopyPlane(const uint8_t* src, int src_stride,
  203. uint8_t* dst, int dst_stride, int width, int height) {
  204. assert(src != NULL && dst != NULL);
  205. assert(src_stride >= width && dst_stride >= width);
  206. while (height-- > 0) {
  207. memcpy(dst, src, width);
  208. src += src_stride;
  209. dst += dst_stride;
  210. }
  211. }
  212. void WebPCopyPixels(const WebPPicture* const src, WebPPicture* const dst) {
  213. assert(src != NULL && dst != NULL);
  214. assert(src->width == dst->width && src->height == dst->height);
  215. assert(src->use_argb && dst->use_argb);
  216. WebPCopyPlane((uint8_t*)src->argb, 4 * src->argb_stride, (uint8_t*)dst->argb,
  217. 4 * dst->argb_stride, 4 * src->width, src->height);
  218. }
  219. //------------------------------------------------------------------------------
  220. #define COLOR_HASH_SIZE (MAX_PALETTE_SIZE * 4)
  221. #define COLOR_HASH_RIGHT_SHIFT 22 // 32 - log2(COLOR_HASH_SIZE).
  222. int WebPGetColorPalette(const WebPPicture* const pic, uint32_t* const palette) {
  223. int i;
  224. int x, y;
  225. int num_colors = 0;
  226. uint8_t in_use[COLOR_HASH_SIZE] = { 0 };
  227. uint32_t colors[COLOR_HASH_SIZE];
  228. static const uint64_t kHashMul = 0x1e35a7bdull;
  229. const uint32_t* argb = pic->argb;
  230. const int width = pic->width;
  231. const int height = pic->height;
  232. uint32_t last_pix = ~argb[0]; // so we're sure that last_pix != argb[0]
  233. assert(pic != NULL);
  234. assert(pic->use_argb);
  235. for (y = 0; y < height; ++y) {
  236. for (x = 0; x < width; ++x) {
  237. int key;
  238. if (argb[x] == last_pix) {
  239. continue;
  240. }
  241. last_pix = argb[x];
  242. key = ((last_pix * kHashMul) & 0xffffffffu) >> COLOR_HASH_RIGHT_SHIFT;
  243. while (1) {
  244. if (!in_use[key]) {
  245. colors[key] = last_pix;
  246. in_use[key] = 1;
  247. ++num_colors;
  248. if (num_colors > MAX_PALETTE_SIZE) {
  249. return MAX_PALETTE_SIZE + 1; // Exact count not needed.
  250. }
  251. break;
  252. } else if (colors[key] == last_pix) {
  253. break; // The color is already there.
  254. } else {
  255. // Some other color sits here, so do linear conflict resolution.
  256. ++key;
  257. key &= (COLOR_HASH_SIZE - 1); // Key mask.
  258. }
  259. }
  260. }
  261. argb += pic->argb_stride;
  262. }
  263. if (palette != NULL) { // Fill the colors into palette.
  264. num_colors = 0;
  265. for (i = 0; i < COLOR_HASH_SIZE; ++i) {
  266. if (in_use[i]) {
  267. palette[num_colors] = colors[i];
  268. ++num_colors;
  269. }
  270. }
  271. }
  272. return num_colors;
  273. }
  274. #undef COLOR_HASH_SIZE
  275. #undef COLOR_HASH_RIGHT_SHIFT
  276. //------------------------------------------------------------------------------
  277. #if defined(WEBP_NEED_LOG_TABLE_8BIT)
  278. const uint8_t WebPLogTable8bit[256] = { // 31 ^ clz(i)
  279. 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  280. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  281. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  282. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  283. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  284. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  285. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  286. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  287. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  288. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  289. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  290. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  291. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  292. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  293. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  294. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
  295. };
  296. #endif
  297. //------------------------------------------------------------------------------