buffer.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // Everything about WebPDecBuffer
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <stdlib.h>
  14. #include "./vp8i.h"
  15. #include "./webpi.h"
  16. #include "../utils/utils.h"
  17. //------------------------------------------------------------------------------
  18. // WebPDecBuffer
  19. // Number of bytes per pixel for the different color-spaces.
  20. static const int kModeBpp[MODE_LAST] = {
  21. 3, 4, 3, 4, 4, 2, 2,
  22. 4, 4, 4, 2, // pre-multiplied modes
  23. 1, 1 };
  24. // Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE.
  25. // Convert to an integer to handle both the unsigned/signed enum cases
  26. // without the need for casting to remove type limit warnings.
  27. static int IsValidColorspace(int webp_csp_mode) {
  28. return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST);
  29. }
  30. static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
  31. int ok = 1;
  32. const WEBP_CSP_MODE mode = buffer->colorspace;
  33. const int width = buffer->width;
  34. const int height = buffer->height;
  35. if (!IsValidColorspace(mode)) {
  36. ok = 0;
  37. } else if (!WebPIsRGBMode(mode)) { // YUV checks
  38. const WebPYUVABuffer* const buf = &buffer->u.YUVA;
  39. const int y_stride = abs(buf->y_stride);
  40. const int u_stride = abs(buf->u_stride);
  41. const int v_stride = abs(buf->v_stride);
  42. const int a_stride = abs(buf->a_stride);
  43. const uint64_t y_size = (uint64_t)y_stride * height;
  44. const uint64_t u_size = (uint64_t)u_stride * ((height + 1) / 2);
  45. const uint64_t v_size = (uint64_t)v_stride * ((height + 1) / 2);
  46. const uint64_t a_size = (uint64_t)a_stride * height;
  47. ok &= (y_size <= buf->y_size);
  48. ok &= (u_size <= buf->u_size);
  49. ok &= (v_size <= buf->v_size);
  50. ok &= (y_stride >= width);
  51. ok &= (u_stride >= (width + 1) / 2);
  52. ok &= (v_stride >= (width + 1) / 2);
  53. ok &= (buf->y != NULL);
  54. ok &= (buf->u != NULL);
  55. ok &= (buf->v != NULL);
  56. if (mode == MODE_YUVA) {
  57. ok &= (a_stride >= width);
  58. ok &= (a_size <= buf->a_size);
  59. ok &= (buf->a != NULL);
  60. }
  61. } else { // RGB checks
  62. const WebPRGBABuffer* const buf = &buffer->u.RGBA;
  63. const int stride = abs(buf->stride);
  64. const uint64_t size = (uint64_t)stride * height;
  65. ok &= (size <= buf->size);
  66. ok &= (stride >= width * kModeBpp[mode]);
  67. ok &= (buf->rgba != NULL);
  68. }
  69. return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
  70. }
  71. static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
  72. const int w = buffer->width;
  73. const int h = buffer->height;
  74. const WEBP_CSP_MODE mode = buffer->colorspace;
  75. if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
  76. return VP8_STATUS_INVALID_PARAM;
  77. }
  78. if (!buffer->is_external_memory && buffer->private_memory == NULL) {
  79. uint8_t* output;
  80. int uv_stride = 0, a_stride = 0;
  81. uint64_t uv_size = 0, a_size = 0, total_size;
  82. // We need memory and it hasn't been allocated yet.
  83. // => initialize output buffer, now that dimensions are known.
  84. const int stride = w * kModeBpp[mode];
  85. const uint64_t size = (uint64_t)stride * h;
  86. if (!WebPIsRGBMode(mode)) {
  87. uv_stride = (w + 1) / 2;
  88. uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
  89. if (mode == MODE_YUVA) {
  90. a_stride = w;
  91. a_size = (uint64_t)a_stride * h;
  92. }
  93. }
  94. total_size = size + 2 * uv_size + a_size;
  95. // Security/sanity checks
  96. output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));
  97. if (output == NULL) {
  98. return VP8_STATUS_OUT_OF_MEMORY;
  99. }
  100. buffer->private_memory = output;
  101. if (!WebPIsRGBMode(mode)) { // YUVA initialization
  102. WebPYUVABuffer* const buf = &buffer->u.YUVA;
  103. buf->y = output;
  104. buf->y_stride = stride;
  105. buf->y_size = (size_t)size;
  106. buf->u = output + size;
  107. buf->u_stride = uv_stride;
  108. buf->u_size = (size_t)uv_size;
  109. buf->v = output + size + uv_size;
  110. buf->v_stride = uv_stride;
  111. buf->v_size = (size_t)uv_size;
  112. if (mode == MODE_YUVA) {
  113. buf->a = output + size + 2 * uv_size;
  114. }
  115. buf->a_size = (size_t)a_size;
  116. buf->a_stride = a_stride;
  117. } else { // RGBA initialization
  118. WebPRGBABuffer* const buf = &buffer->u.RGBA;
  119. buf->rgba = output;
  120. buf->stride = stride;
  121. buf->size = (size_t)size;
  122. }
  123. }
  124. return CheckDecBuffer(buffer);
  125. }
  126. VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {
  127. if (buffer == NULL) {
  128. return VP8_STATUS_INVALID_PARAM;
  129. }
  130. if (WebPIsRGBMode(buffer->colorspace)) {
  131. WebPRGBABuffer* const buf = &buffer->u.RGBA;
  132. buf->rgba += (buffer->height - 1) * buf->stride;
  133. buf->stride = -buf->stride;
  134. } else {
  135. WebPYUVABuffer* const buf = &buffer->u.YUVA;
  136. const int H = buffer->height;
  137. buf->y += (H - 1) * buf->y_stride;
  138. buf->y_stride = -buf->y_stride;
  139. buf->u += ((H - 1) >> 1) * buf->u_stride;
  140. buf->u_stride = -buf->u_stride;
  141. buf->v += ((H - 1) >> 1) * buf->v_stride;
  142. buf->v_stride = -buf->v_stride;
  143. if (buf->a != NULL) {
  144. buf->a += (H - 1) * buf->a_stride;
  145. buf->a_stride = -buf->a_stride;
  146. }
  147. }
  148. return VP8_STATUS_OK;
  149. }
  150. VP8StatusCode WebPAllocateDecBuffer(int w, int h,
  151. const WebPDecoderOptions* const options,
  152. WebPDecBuffer* const out) {
  153. VP8StatusCode status;
  154. if (out == NULL || w <= 0 || h <= 0) {
  155. return VP8_STATUS_INVALID_PARAM;
  156. }
  157. if (options != NULL) { // First, apply options if there is any.
  158. if (options->use_cropping) {
  159. const int cw = options->crop_width;
  160. const int ch = options->crop_height;
  161. const int x = options->crop_left & ~1;
  162. const int y = options->crop_top & ~1;
  163. if (x < 0 || y < 0 || cw <= 0 || ch <= 0 || x + cw > w || y + ch > h) {
  164. return VP8_STATUS_INVALID_PARAM; // out of frame boundary.
  165. }
  166. w = cw;
  167. h = ch;
  168. }
  169. if (options->use_scaling) {
  170. if (options->scaled_width <= 0 || options->scaled_height <= 0) {
  171. return VP8_STATUS_INVALID_PARAM;
  172. }
  173. w = options->scaled_width;
  174. h = options->scaled_height;
  175. }
  176. }
  177. out->width = w;
  178. out->height = h;
  179. // Then, allocate buffer for real.
  180. status = AllocateBuffer(out);
  181. if (status != VP8_STATUS_OK) return status;
  182. #if WEBP_DECODER_ABI_VERSION > 0x0203
  183. // Use the stride trick if vertical flip is needed.
  184. if (options != NULL && options->flip) {
  185. status = WebPFlipBuffer(out);
  186. }
  187. #endif
  188. return status;
  189. }
  190. //------------------------------------------------------------------------------
  191. // constructors / destructors
  192. int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
  193. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  194. return 0; // version mismatch
  195. }
  196. if (buffer == NULL) return 0;
  197. memset(buffer, 0, sizeof(*buffer));
  198. return 1;
  199. }
  200. void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
  201. if (buffer != NULL) {
  202. if (!buffer->is_external_memory) {
  203. WebPSafeFree(buffer->private_memory);
  204. }
  205. buffer->private_memory = NULL;
  206. }
  207. }
  208. void WebPCopyDecBuffer(const WebPDecBuffer* const src,
  209. WebPDecBuffer* const dst) {
  210. if (src != NULL && dst != NULL) {
  211. *dst = *src;
  212. if (src->private_memory != NULL) {
  213. dst->is_external_memory = 1; // dst buffer doesn't own the memory.
  214. dst->private_memory = NULL;
  215. }
  216. }
  217. }
  218. // Copy and transfer ownership from src to dst (beware of parameter order!)
  219. void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
  220. if (src != NULL && dst != NULL) {
  221. *dst = *src;
  222. if (src->private_memory != NULL) {
  223. src->is_external_memory = 1; // src relinquishes ownership
  224. src->private_memory = NULL;
  225. }
  226. }
  227. }
  228. //------------------------------------------------------------------------------