picture_enc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 + WEBP_ALIGN_CST, sizeof(*picture->argb));
  67. if (memory == NULL) {
  68. return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
  69. }
  70. picture->memory_argb_ = memory;
  71. picture->argb = (uint32_t*)WEBP_ALIGN(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 =
  77. (WebPEncCSP)((int)picture->colorspace & WEBP_CSP_UV_MASK);
  78. const int has_alpha = (int)picture->colorspace & WEBP_CSP_ALPHA_BIT;
  79. const int y_stride = width;
  80. const int uv_width = (int)(((int64_t)width + 1) >> 1);
  81. const int uv_height = (int)(((int64_t)height + 1) >> 1);
  82. const int uv_stride = uv_width;
  83. int a_width, a_stride;
  84. uint64_t y_size, uv_size, a_size, total_size;
  85. uint8_t* mem;
  86. assert(picture != NULL);
  87. WebPSafeFree(picture->memory_);
  88. WebPPictureResetBufferYUVA(picture);
  89. if (uv_csp != WEBP_YUV420) {
  90. return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
  91. }
  92. // alpha
  93. a_width = has_alpha ? width : 0;
  94. a_stride = a_width;
  95. y_size = (uint64_t)y_stride * height;
  96. uv_size = (uint64_t)uv_stride * uv_height;
  97. a_size = (uint64_t)a_stride * height;
  98. total_size = y_size + a_size + 2 * uv_size;
  99. // Security and validation checks
  100. if (width <= 0 || height <= 0 || // luma/alpha param error
  101. uv_width <= 0 || uv_height <= 0) { // u/v param error
  102. return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
  103. }
  104. // allocate a new buffer.
  105. mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem));
  106. if (mem == NULL) {
  107. return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
  108. }
  109. // From now on, we're in the clear, we can no longer fail...
  110. picture->memory_ = (void*)mem;
  111. picture->y_stride = y_stride;
  112. picture->uv_stride = uv_stride;
  113. picture->a_stride = a_stride;
  114. // TODO(skal): we could align the y/u/v planes and adjust stride.
  115. picture->y = mem;
  116. mem += y_size;
  117. picture->u = mem;
  118. mem += uv_size;
  119. picture->v = mem;
  120. mem += uv_size;
  121. if (a_size > 0) {
  122. picture->a = mem;
  123. mem += a_size;
  124. }
  125. (void)mem; // makes the static analyzer happy
  126. return 1;
  127. }
  128. int WebPPictureAlloc(WebPPicture* picture) {
  129. if (picture != NULL) {
  130. const int width = picture->width;
  131. const int height = picture->height;
  132. WebPPictureFree(picture); // erase previous buffer
  133. if (!picture->use_argb) {
  134. return WebPPictureAllocYUVA(picture, width, height);
  135. } else {
  136. return WebPPictureAllocARGB(picture, width, height);
  137. }
  138. }
  139. return 1;
  140. }
  141. void WebPPictureFree(WebPPicture* picture) {
  142. if (picture != NULL) {
  143. WebPSafeFree(picture->memory_);
  144. WebPSafeFree(picture->memory_argb_);
  145. WebPPictureResetBuffers(picture);
  146. }
  147. }
  148. //------------------------------------------------------------------------------
  149. // WebPMemoryWriter: Write-to-memory
  150. void WebPMemoryWriterInit(WebPMemoryWriter* writer) {
  151. writer->mem = NULL;
  152. writer->size = 0;
  153. writer->max_size = 0;
  154. }
  155. int WebPMemoryWrite(const uint8_t* data, size_t data_size,
  156. const WebPPicture* picture) {
  157. WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr;
  158. uint64_t next_size;
  159. if (w == NULL) {
  160. return 1;
  161. }
  162. next_size = (uint64_t)w->size + data_size;
  163. if (next_size > w->max_size) {
  164. uint8_t* new_mem;
  165. uint64_t next_max_size = 2ULL * w->max_size;
  166. if (next_max_size < next_size) next_max_size = next_size;
  167. if (next_max_size < 8192ULL) next_max_size = 8192ULL;
  168. new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1);
  169. if (new_mem == NULL) {
  170. return 0;
  171. }
  172. if (w->size > 0) {
  173. memcpy(new_mem, w->mem, w->size);
  174. }
  175. WebPSafeFree(w->mem);
  176. w->mem = new_mem;
  177. // down-cast is ok, thanks to WebPSafeMalloc
  178. w->max_size = (size_t)next_max_size;
  179. }
  180. if (data_size > 0) {
  181. memcpy(w->mem + w->size, data, data_size);
  182. w->size += data_size;
  183. }
  184. return 1;
  185. }
  186. void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
  187. if (writer != NULL) {
  188. WebPSafeFree(writer->mem);
  189. writer->mem = NULL;
  190. writer->size = 0;
  191. writer->max_size = 0;
  192. }
  193. }
  194. //------------------------------------------------------------------------------
  195. // Simplest high-level calls:
  196. typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
  197. static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
  198. Importer import, float quality_factor, int lossless,
  199. uint8_t** output) {
  200. WebPPicture pic;
  201. WebPConfig config;
  202. WebPMemoryWriter wrt;
  203. int ok;
  204. if (output == NULL) return 0;
  205. if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) ||
  206. !WebPPictureInit(&pic)) {
  207. return 0; // shouldn't happen, except if system installation is broken
  208. }
  209. config.lossless = !!lossless;
  210. pic.use_argb = !!lossless;
  211. pic.width = width;
  212. pic.height = height;
  213. pic.writer = WebPMemoryWrite;
  214. pic.custom_ptr = &wrt;
  215. WebPMemoryWriterInit(&wrt);
  216. ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
  217. WebPPictureFree(&pic);
  218. if (!ok) {
  219. WebPMemoryWriterClear(&wrt);
  220. *output = NULL;
  221. return 0;
  222. }
  223. *output = wrt.mem;
  224. return wrt.size;
  225. }
  226. #define ENCODE_FUNC(NAME, IMPORTER) \
  227. size_t NAME(const uint8_t* in, int w, int h, int bps, float q, \
  228. uint8_t** out) { \
  229. return Encode(in, w, h, bps, IMPORTER, q, 0, out); \
  230. }
  231. ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB)
  232. ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA)
  233. #if !defined(WEBP_REDUCE_CSP)
  234. ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR)
  235. ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA)
  236. #endif // WEBP_REDUCE_CSP
  237. #undef ENCODE_FUNC
  238. #define LOSSLESS_DEFAULT_QUALITY 70.
  239. #define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER) \
  240. size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) { \
  241. return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out); \
  242. }
  243. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB)
  244. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA)
  245. #if !defined(WEBP_REDUCE_CSP)
  246. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR)
  247. LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA)
  248. #endif // WEBP_REDUCE_CSP
  249. #undef LOSSLESS_ENCODE_FUNC
  250. //------------------------------------------------------------------------------