buffer_dec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_dec.h"
  15. #include "./webpi_dec.h"
  16. #include "../utils/utils.h"
  17. //------------------------------------------------------------------------------
  18. // WebPDecBuffer
  19. // Number of bytes per pixel for the different color-spaces.
  20. static const uint8_t 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 =
  71. MIN_BUFFER_SIZE(width * kModeBpp[mode], height, stride);
  72. ok &= (size <= buf->size);
  73. ok &= (stride >= width * kModeBpp[mode]);
  74. ok &= (buf->rgba != NULL);
  75. }
  76. return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
  77. }
  78. #undef MIN_BUFFER_SIZE
  79. static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
  80. const int w = buffer->width;
  81. const int h = buffer->height;
  82. const WEBP_CSP_MODE mode = buffer->colorspace;
  83. if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
  84. return VP8_STATUS_INVALID_PARAM;
  85. }
  86. if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {
  87. uint8_t* output;
  88. int uv_stride = 0, a_stride = 0;
  89. uint64_t uv_size = 0, a_size = 0, total_size;
  90. // We need memory and it hasn't been allocated yet.
  91. // => initialize output buffer, now that dimensions are known.
  92. int stride;
  93. uint64_t size;
  94. if ((uint64_t)w * kModeBpp[mode] >= (1ull << 31)) {
  95. return VP8_STATUS_INVALID_PARAM;
  96. }
  97. stride = w * kModeBpp[mode];
  98. size = (uint64_t)stride * h;
  99. if (!WebPIsRGBMode(mode)) {
  100. uv_stride = (w + 1) / 2;
  101. uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
  102. if (mode == MODE_YUVA) {
  103. a_stride = w;
  104. a_size = (uint64_t)a_stride * h;
  105. }
  106. }
  107. total_size = size + 2 * uv_size + a_size;
  108. output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));
  109. if (output == NULL) {
  110. return VP8_STATUS_OUT_OF_MEMORY;
  111. }
  112. buffer->private_memory = output;
  113. if (!WebPIsRGBMode(mode)) { // YUVA initialization
  114. WebPYUVABuffer* const buf = &buffer->u.YUVA;
  115. buf->y = output;
  116. buf->y_stride = stride;
  117. buf->y_size = (size_t)size;
  118. buf->u = output + size;
  119. buf->u_stride = uv_stride;
  120. buf->u_size = (size_t)uv_size;
  121. buf->v = output + size + uv_size;
  122. buf->v_stride = uv_stride;
  123. buf->v_size = (size_t)uv_size;
  124. if (mode == MODE_YUVA) {
  125. buf->a = output + size + 2 * uv_size;
  126. }
  127. buf->a_size = (size_t)a_size;
  128. buf->a_stride = a_stride;
  129. } else { // RGBA initialization
  130. WebPRGBABuffer* const buf = &buffer->u.RGBA;
  131. buf->rgba = output;
  132. buf->stride = stride;
  133. buf->size = (size_t)size;
  134. }
  135. }
  136. return CheckDecBuffer(buffer);
  137. }
  138. VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {
  139. if (buffer == NULL) {
  140. return VP8_STATUS_INVALID_PARAM;
  141. }
  142. if (WebPIsRGBMode(buffer->colorspace)) {
  143. WebPRGBABuffer* const buf = &buffer->u.RGBA;
  144. buf->rgba += (int64_t)(buffer->height - 1) * buf->stride;
  145. buf->stride = -buf->stride;
  146. } else {
  147. WebPYUVABuffer* const buf = &buffer->u.YUVA;
  148. const int64_t H = buffer->height;
  149. buf->y += (H - 1) * buf->y_stride;
  150. buf->y_stride = -buf->y_stride;
  151. buf->u += ((H - 1) >> 1) * buf->u_stride;
  152. buf->u_stride = -buf->u_stride;
  153. buf->v += ((H - 1) >> 1) * buf->v_stride;
  154. buf->v_stride = -buf->v_stride;
  155. if (buf->a != NULL) {
  156. buf->a += (H - 1) * buf->a_stride;
  157. buf->a_stride = -buf->a_stride;
  158. }
  159. }
  160. return VP8_STATUS_OK;
  161. }
  162. VP8StatusCode WebPAllocateDecBuffer(int width, int height,
  163. const WebPDecoderOptions* const options,
  164. WebPDecBuffer* const buffer) {
  165. VP8StatusCode status;
  166. if (buffer == NULL || width <= 0 || height <= 0) {
  167. return VP8_STATUS_INVALID_PARAM;
  168. }
  169. if (options != NULL) { // First, apply options if there is any.
  170. if (options->use_cropping) {
  171. const int cw = options->crop_width;
  172. const int ch = options->crop_height;
  173. const int x = options->crop_left & ~1;
  174. const int y = options->crop_top & ~1;
  175. if (!WebPCheckCropDimensions(width, height, x, y, cw, ch)) {
  176. return VP8_STATUS_INVALID_PARAM; // out of frame boundary.
  177. }
  178. width = cw;
  179. height = ch;
  180. }
  181. if (options->use_scaling) {
  182. #if !defined(WEBP_REDUCE_SIZE)
  183. int scaled_width = options->scaled_width;
  184. int scaled_height = options->scaled_height;
  185. if (!WebPRescalerGetScaledDimensions(
  186. width, height, &scaled_width, &scaled_height)) {
  187. return VP8_STATUS_INVALID_PARAM;
  188. }
  189. width = scaled_width;
  190. height = scaled_height;
  191. #else
  192. return VP8_STATUS_INVALID_PARAM; // rescaling not supported
  193. #endif
  194. }
  195. }
  196. buffer->width = width;
  197. buffer->height = height;
  198. // Then, allocate buffer for real.
  199. status = AllocateBuffer(buffer);
  200. if (status != VP8_STATUS_OK) return status;
  201. // Use the stride trick if vertical flip is needed.
  202. if (options != NULL && options->flip) {
  203. status = WebPFlipBuffer(buffer);
  204. }
  205. return status;
  206. }
  207. //------------------------------------------------------------------------------
  208. // constructors / destructors
  209. int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
  210. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  211. return 0; // version mismatch
  212. }
  213. if (buffer == NULL) return 0;
  214. memset(buffer, 0, sizeof(*buffer));
  215. return 1;
  216. }
  217. void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
  218. if (buffer != NULL) {
  219. if (buffer->is_external_memory <= 0) {
  220. WebPSafeFree(buffer->private_memory);
  221. }
  222. buffer->private_memory = NULL;
  223. }
  224. }
  225. void WebPCopyDecBuffer(const WebPDecBuffer* const src,
  226. WebPDecBuffer* const dst) {
  227. if (src != NULL && dst != NULL) {
  228. *dst = *src;
  229. if (src->private_memory != NULL) {
  230. dst->is_external_memory = 1; // dst buffer doesn't own the memory.
  231. dst->private_memory = NULL;
  232. }
  233. }
  234. }
  235. // Copy and transfer ownership from src to dst (beware of parameter order!)
  236. void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
  237. if (src != NULL && dst != NULL) {
  238. *dst = *src;
  239. if (src->private_memory != NULL) {
  240. src->is_external_memory = 1; // src relinquishes ownership
  241. src->private_memory = NULL;
  242. }
  243. }
  244. }
  245. VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,
  246. WebPDecBuffer* const dst_buf) {
  247. assert(src_buf != NULL && dst_buf != NULL);
  248. assert(src_buf->colorspace == dst_buf->colorspace);
  249. dst_buf->width = src_buf->width;
  250. dst_buf->height = src_buf->height;
  251. if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {
  252. return VP8_STATUS_INVALID_PARAM;
  253. }
  254. if (WebPIsRGBMode(src_buf->colorspace)) {
  255. const WebPRGBABuffer* const src = &src_buf->u.RGBA;
  256. const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;
  257. WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,
  258. src_buf->width * kModeBpp[src_buf->colorspace],
  259. src_buf->height);
  260. } else {
  261. const WebPYUVABuffer* const src = &src_buf->u.YUVA;
  262. const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;
  263. WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,
  264. src_buf->width, src_buf->height);
  265. WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,
  266. (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
  267. WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,
  268. (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
  269. if (WebPIsAlphaMode(src_buf->colorspace)) {
  270. WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,
  271. src_buf->width, src_buf->height);
  272. }
  273. }
  274. return VP8_STATUS_OK;
  275. }
  276. int WebPAvoidSlowMemory(const WebPDecBuffer* const output,
  277. const WebPBitstreamFeatures* const features) {
  278. assert(output != NULL);
  279. return (output->is_external_memory >= 2) &&
  280. WebPIsPremultipliedMode(output->colorspace) &&
  281. (features != NULL && features->has_alpha);
  282. }
  283. //------------------------------------------------------------------------------