picture_enc.c 8.7 KB

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