picture.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright 2011 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. // WebPPicture class basis
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include "./vp8enci.h"
  16. #include "../utils/utils.h"
  17. //------------------------------------------------------------------------------
  18. // WebPPicture
  19. //------------------------------------------------------------------------------
  20. static int DummyWriter(const uint8_t* data, size_t data_size,
  21. const WebPPicture* const picture) {
  22. // The following are to prevent 'unused variable' error message.
  23. (void)data;
  24. (void)data_size;
  25. (void)picture;
  26. return 1;
  27. }
  28. int WebPPictureInitInternal(WebPPicture* picture, int version) {
  29. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {
  30. return 0; // caller/system version mismatch!
  31. }
  32. if (picture != NULL) {
  33. memset(picture, 0, sizeof(*picture));
  34. picture->writer = DummyWriter;
  35. WebPEncodingSetError(picture, VP8_ENC_OK);
  36. }
  37. return 1;
  38. }
  39. //------------------------------------------------------------------------------
  40. static void WebPPictureResetBufferARGB(WebPPicture* const picture) {
  41. picture->memory_argb_ = NULL;
  42. picture->argb = NULL;
  43. picture->argb_stride = 0;
  44. }
  45. static void WebPPictureResetBufferYUVA(WebPPicture* const picture) {
  46. picture->memory_ = NULL;
  47. picture->y = picture->u = picture->v = picture->a = NULL;
  48. picture->y_stride = picture->uv_stride = 0;
  49. picture->a_stride = 0;
  50. }
  51. void WebPPictureResetBuffers(WebPPicture* const picture) {
  52. WebPPictureResetBufferARGB(picture);
  53. WebPPictureResetBufferYUVA(picture);
  54. }
  55. int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height) {
  56. void* memory;
  57. const uint64_t argb_size = (uint64_t)width * height;
  58. assert(picture != NULL);
  59. WebPSafeFree(picture->memory_argb_);
  60. WebPPictureResetBufferARGB(picture);
  61. if (width <= 0 || height <= 0) {
  62. return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
  63. }
  64. // allocate a new buffer.
  65. memory = WebPSafeMalloc(argb_size, sizeof(*picture->argb));
  66. if (memory == NULL) {
  67. return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
  68. }
  69. // TODO(skal): align plane to cache line?
  70. picture->memory_argb_ = memory;
  71. picture->argb = (uint32_t*)memory;
  72. picture->argb_stride = width;
  73. return 1;
  74. }
  75. int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height) {
  76. const WebPEncCSP uv_csp = picture->colorspace & WEBP_CSP_UV_MASK;
  77. const int has_alpha = picture->colorspace & WEBP_CSP_ALPHA_BIT;
  78. const int y_stride = width;
  79. const int uv_width = (width + 1) >> 1;
  80. const int uv_height = (height + 1) >> 1;
  81. const int uv_stride = uv_width;
  82. int a_width, a_stride;
  83. uint64_t y_size, uv_size, a_size, total_size;
  84. uint8_t* mem;
  85. assert(picture != NULL);
  86. WebPSafeFree(picture->memory_);
  87. WebPPictureResetBufferYUVA(picture);
  88. if (uv_csp != WEBP_YUV420) {
  89. return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
  90. }
  91. // alpha
  92. a_width = has_alpha ? width : 0;
  93. a_stride = a_width;
  94. y_size = (uint64_t)y_stride * height;
  95. uv_size = (uint64_t)uv_stride * uv_height;
  96. a_size = (uint64_t)a_stride * height;
  97. total_size = y_size + a_size + 2 * uv_size;
  98. // Security and validation checks
  99. if (width <= 0 || height <= 0 || // luma/alpha param error
  100. uv_width < 0 || uv_height < 0) { // u/v param error
  101. return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
  102. }
  103. // allocate a new buffer.
  104. mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem));
  105. if (mem == NULL) {
  106. return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
  107. }
  108. // From now on, we're in the clear, we can no longer fail...
  109. picture->memory_ = (void*)mem;
  110. picture->y_stride = y_stride;
  111. picture->uv_stride = uv_stride;
  112. picture->a_stride = a_stride;
  113. // TODO(skal): we could align the y/u/v planes and adjust stride.
  114. picture->y = mem;
  115. mem += y_size;
  116. picture->u = mem;
  117. mem += uv_size;
  118. picture->v = mem;
  119. mem += uv_size;
  120. if (a_size > 0) {
  121. picture->a = mem;
  122. mem += a_size;
  123. }
  124. (void)mem; // makes the static analyzer happy
  125. return 1;
  126. }
  127. int WebPPictureAlloc(WebPPicture* picture) {
  128. if (picture != NULL) {
  129. const int width = picture->width;
  130. const int height = picture->height;
  131. WebPPictureFree(picture); // erase previous buffer
  132. if (!picture->use_argb) {
  133. return WebPPictureAllocYUVA(picture, width, height);
  134. } else {
  135. return WebPPictureAllocARGB(picture, width, height);
  136. }
  137. }
  138. return 1;
  139. }
  140. void WebPPictureFree(WebPPicture* picture) {
  141. if (picture != NULL) {
  142. WebPSafeFree(picture->memory_);
  143. WebPSafeFree(picture->memory_argb_);
  144. WebPPictureResetBuffers(picture);
  145. }
  146. }
  147. //------------------------------------------------------------------------------
  148. // WebPMemoryWriter: Write-to-memory
  149. void WebPMemoryWriterInit(WebPMemoryWriter* writer) {
  150. writer->mem = NULL;
  151. writer->size = 0;
  152. writer->max_size = 0;
  153. }
  154. int WebPMemoryWrite(const uint8_t* data, size_t data_size,
  155. const WebPPicture* picture) {
  156. WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr;
  157. uint64_t next_size;
  158. if (w == NULL) {
  159. return 1;
  160. }
  161. next_size = (uint64_t)w->size + data_size;
  162. if (next_size > w->max_size) {
  163. uint8_t* new_mem;
  164. uint64_t next_max_size = 2ULL * w->max_size;
  165. if (next_max_size < next_size) next_max_size = next_size;
  166. if (next_max_size < 8192ULL) next_max_size = 8192ULL;
  167. new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1);
  168. if (new_mem == NULL) {
  169. return 0;
  170. }
  171. if (w->size > 0) {
  172. memcpy(new_mem, w->mem, w->size);
  173. }
  174. WebPSafeFree(w->mem);
  175. w->mem = new_mem;
  176. // down-cast is ok, thanks to WebPSafeMalloc
  177. w->max_size = (size_t)next_max_size;
  178. }
  179. if (data_size > 0) {
  180. memcpy(w->mem + w->size, data, data_size);
  181. w->size += data_size;
  182. }
  183. return 1;
  184. }
  185. void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
  186. if (writer != NULL) {
  187. WebPSafeFree(writer->mem);
  188. writer->mem = NULL;
  189. writer->size = 0;
  190. writer->max_size = 0;
  191. }
  192. }
  193. //------------------------------------------------------------------------------
  194. // Simplest high-level calls:
  195. typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
  196. static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
  197. Importer import, float quality_factor, int lossless,
  198. uint8_t** output) {
  199. WebPPicture pic;
  200. WebPConfig config;
  201. WebPMemoryWriter wrt;
  202. int ok;
  203. if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) ||
  204. !WebPPictureInit(&pic)) {
  205. return 0; // shouldn't happen, except if system installation is broken
  206. }
  207. config.lossless = !!lossless;
  208. pic.use_argb = !!lossless;
  209. pic.width = width;
  210. pic.height = height;
  211. pic.writer = WebPMemoryWrite;
  212. pic.custom_ptr = &wrt;
  213. WebPMemoryWriterInit(&wrt);
  214. ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
  215. WebPPictureFree(&pic);
  216. if (!ok) {
  217. WebPMemoryWriterClear(&wrt);
  218. *output = NULL;
  219. return 0;
  220. }
  221. *output = wrt.mem;
  222. return wrt.size;
  223. }
  224. #define ENCODE_FUNC(NAME, IMPORTER) \
  225. size_t NAME(const uint8_t* in, int w, int h, int bps, float q, \
  226. uint8_t** out) { \
  227. return Encode(in, w, h, bps, IMPORTER, q, 0, out); \
  228. }
  229. ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB)
  230. ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR)
  231. ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA)
  232. ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA)
  233. #undef ENCODE_FUNC
  234. #define LOSSLESS_DEFAULT_QUALITY 70.
  235. #define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER) \
  236. size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) { \
  237. return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out); \
  238. }
  239. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB)
  240. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR)
  241. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA)
  242. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA)
  243. #undef LOSSLESS_ENCODE_FUNC
  244. //------------------------------------------------------------------------------