buffer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 ([email protected])
  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. // strictly speaking, the very last (or first, if flipped) row
  31. // doesn't require padding.
  32. #define MIN_BUFFER_SIZE(WIDTH, HEIGHT, STRIDE) \
  33. (uint64_t)(STRIDE) * ((HEIGHT) - 1) + (WIDTH)
  34. static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
  35. int ok = 1;
  36. const WEBP_CSP_MODE mode = buffer->colorspace;
  37. const int width = buffer->width;
  38. const int height = buffer->height;
  39. if (!IsValidColorspace(mode)) {
  40. ok = 0;
  41. } else if (!WebPIsRGBMode(mode)) { // YUV checks
  42. const WebPYUVABuffer* const buf = &buffer->u.YUVA;
  43. const int uv_width = (width + 1) / 2;
  44. const int uv_height = (height + 1) / 2;
  45. const int y_stride = abs(buf->y_stride);
  46. const int u_stride = abs(buf->u_stride);
  47. const int v_stride = abs(buf->v_stride);
  48. const int a_stride = abs(buf->a_stride);
  49. const uint64_t y_size = MIN_BUFFER_SIZE(width, height, y_stride);
  50. const uint64_t u_size = MIN_BUFFER_SIZE(uv_width, uv_height, u_stride);
  51. const uint64_t v_size = MIN_BUFFER_SIZE(uv_width, uv_height, v_stride);
  52. const uint64_t a_size = MIN_BUFFER_SIZE(width, height, a_stride);
  53. ok &= (y_size <= buf->y_size);
  54. ok &= (u_size <= buf->u_size);
  55. ok &= (v_size <= buf->v_size);
  56. ok &= (y_stride >= width);
  57. ok &= (u_stride >= uv_width);
  58. ok &= (v_stride >= uv_width);
  59. ok &= (buf->y != NULL);
  60. ok &= (buf->u != NULL);
  61. ok &= (buf->v != NULL);
  62. if (mode == MODE_YUVA) {
  63. ok &= (a_stride >= width);
  64. ok &= (a_size <= buf->a_size);
  65. ok &= (buf->a != NULL);
  66. }
  67. } else { // RGB checks
  68. const WebPRGBABuffer* const buf = &buffer->u.RGBA;
  69. const int stride = abs(buf->stride);
  70. const uint64_t size = MIN_BUFFER_SIZE(width, height, stride);
  71. ok &= (size <= buf->size);
  72. ok &= (stride >= width * kModeBpp[mode]);
  73. ok &= (buf->rgba != NULL);
  74. }
  75. return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
  76. }
  77. #undef MIN_BUFFER_SIZE
  78. static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
  79. const int w = buffer->width;
  80. const int h = buffer->height;
  81. const WEBP_CSP_MODE mode = buffer->colorspace;
  82. if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
  83. return VP8_STATUS_INVALID_PARAM;
  84. }
  85. if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {
  86. uint8_t* output;
  87. int uv_stride = 0, a_stride = 0;
  88. uint64_t uv_size = 0, a_size = 0, total_size;
  89. // We need memory and it hasn't been allocated yet.
  90. // => initialize output buffer, now that dimensions are known.
  91. const int stride = w * kModeBpp[mode];
  92. const uint64_t size = (uint64_t)stride * h;
  93. if (!WebPIsRGBMode(mode)) {
  94. uv_stride = (w + 1) / 2;
  95. uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
  96. if (mode == MODE_YUVA) {
  97. a_stride = w;
  98. a_size = (uint64_t)a_stride * h;
  99. }
  100. }
  101. total_size = size + 2 * uv_size + a_size;
  102. // Security/sanity checks
  103. output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));
  104. if (output == NULL) {
  105. return VP8_STATUS_OUT_OF_MEMORY;
  106. }
  107. buffer->private_memory = output;
  108. if (!WebPIsRGBMode(mode)) { // YUVA initialization
  109. WebPYUVABuffer* const buf = &buffer->u.YUVA;
  110. buf->y = output;
  111. buf->y_stride = stride;
  112. buf->y_size = (size_t)size;
  113. buf->u = output + size;
  114. buf->u_stride = uv_stride;
  115. buf->u_size = (size_t)uv_size;
  116. buf->v = output + size + uv_size;
  117. buf->v_stride = uv_stride;
  118. buf->v_size = (size_t)uv_size;
  119. if (mode == MODE_YUVA) {
  120. buf->a = output + size + 2 * uv_size;
  121. }
  122. buf->a_size = (size_t)a_size;
  123. buf->a_stride = a_stride;
  124. } else { // RGBA initialization
  125. WebPRGBABuffer* const buf = &buffer->u.RGBA;
  126. buf->rgba = output;
  127. buf->stride = stride;
  128. buf->size = (size_t)size;
  129. }
  130. }
  131. return CheckDecBuffer(buffer);
  132. }
  133. VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {
  134. if (buffer == NULL) {
  135. return VP8_STATUS_INVALID_PARAM;
  136. }
  137. if (WebPIsRGBMode(buffer->colorspace)) {
  138. WebPRGBABuffer* const buf = &buffer->u.RGBA;
  139. buf->rgba += (buffer->height - 1) * buf->stride;
  140. buf->stride = -buf->stride;
  141. } else {
  142. WebPYUVABuffer* const buf = &buffer->u.YUVA;
  143. const int H = buffer->height;
  144. buf->y += (H - 1) * buf->y_stride;
  145. buf->y_stride = -buf->y_stride;
  146. buf->u += ((H - 1) >> 1) * buf->u_stride;
  147. buf->u_stride = -buf->u_stride;
  148. buf->v += ((H - 1) >> 1) * buf->v_stride;
  149. buf->v_stride = -buf->v_stride;
  150. if (buf->a != NULL) {
  151. buf->a += (H - 1) * buf->a_stride;
  152. buf->a_stride = -buf->a_stride;
  153. }
  154. }
  155. return VP8_STATUS_OK;
  156. }
  157. VP8StatusCode WebPAllocateDecBuffer(int w, int h,
  158. const WebPDecoderOptions* const options,
  159. WebPDecBuffer* const out) {
  160. VP8StatusCode status;
  161. if (out == NULL || w <= 0 || h <= 0) {
  162. return VP8_STATUS_INVALID_PARAM;
  163. }
  164. if (options != NULL) { // First, apply options if there is any.
  165. if (options->use_cropping) {
  166. const int cw = options->crop_width;
  167. const int ch = options->crop_height;
  168. const int x = options->crop_left & ~1;
  169. const int y = options->crop_top & ~1;
  170. if (x < 0 || y < 0 || cw <= 0 || ch <= 0 || x + cw > w || y + ch > h) {
  171. return VP8_STATUS_INVALID_PARAM; // out of frame boundary.
  172. }
  173. w = cw;
  174. h = ch;
  175. }
  176. if (options->use_scaling) {
  177. int scaled_width = options->scaled_width;
  178. int scaled_height = options->scaled_height;
  179. if (!WebPRescalerGetScaledDimensions(
  180. w, h, &scaled_width, &scaled_height)) {
  181. return VP8_STATUS_INVALID_PARAM;
  182. }
  183. w = scaled_width;
  184. h = scaled_height;
  185. }
  186. }
  187. out->width = w;
  188. out->height = h;
  189. // Then, allocate buffer for real.
  190. status = AllocateBuffer(out);
  191. if (status != VP8_STATUS_OK) return status;
  192. // Use the stride trick if vertical flip is needed.
  193. if (options != NULL && options->flip) {
  194. status = WebPFlipBuffer(out);
  195. }
  196. return status;
  197. }
  198. //------------------------------------------------------------------------------
  199. // constructors / destructors
  200. int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
  201. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  202. return 0; // version mismatch
  203. }
  204. if (buffer == NULL) return 0;
  205. memset(buffer, 0, sizeof(*buffer));
  206. return 1;
  207. }
  208. void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
  209. if (buffer != NULL) {
  210. if (buffer->is_external_memory <= 0) {
  211. WebPSafeFree(buffer->private_memory);
  212. }
  213. buffer->private_memory = NULL;
  214. }
  215. }
  216. void WebPCopyDecBuffer(const WebPDecBuffer* const src,
  217. WebPDecBuffer* const dst) {
  218. if (src != NULL && dst != NULL) {
  219. *dst = *src;
  220. if (src->private_memory != NULL) {
  221. dst->is_external_memory = 1; // dst buffer doesn't own the memory.
  222. dst->private_memory = NULL;
  223. }
  224. }
  225. }
  226. // Copy and transfer ownership from src to dst (beware of parameter order!)
  227. void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
  228. if (src != NULL && dst != NULL) {
  229. *dst = *src;
  230. if (src->private_memory != NULL) {
  231. src->is_external_memory = 1; // src relinquishes ownership
  232. src->private_memory = NULL;
  233. }
  234. }
  235. }
  236. VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,
  237. WebPDecBuffer* const dst_buf) {
  238. assert(src_buf != NULL && dst_buf != NULL);
  239. assert(src_buf->colorspace == dst_buf->colorspace);
  240. dst_buf->width = src_buf->width;
  241. dst_buf->height = src_buf->height;
  242. if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {
  243. return VP8_STATUS_INVALID_PARAM;
  244. }
  245. if (WebPIsRGBMode(src_buf->colorspace)) {
  246. const WebPRGBABuffer* const src = &src_buf->u.RGBA;
  247. const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;
  248. WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,
  249. src_buf->width * kModeBpp[src_buf->colorspace],
  250. src_buf->height);
  251. } else {
  252. const WebPYUVABuffer* const src = &src_buf->u.YUVA;
  253. const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;
  254. WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,
  255. src_buf->width, src_buf->height);
  256. WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,
  257. (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
  258. WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,
  259. (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
  260. if (WebPIsAlphaMode(src_buf->colorspace)) {
  261. WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,
  262. src_buf->width, src_buf->height);
  263. }
  264. }
  265. return VP8_STATUS_OK;
  266. }
  267. int WebPAvoidSlowMemory(const WebPDecBuffer* const output,
  268. const WebPBitstreamFeatures* const features) {
  269. assert(output != NULL);
  270. return (output->is_external_memory >= 2) &&
  271. WebPIsPremultipliedMode(output->colorspace) &&
  272. (features != NULL && features->has_alpha);
  273. }
  274. //------------------------------------------------------------------------------