yv12config.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <limits.h>
  12. #include "vpx_scale/yv12config.h"
  13. #include "vpx_mem/vpx_mem.h"
  14. #include "vpx_ports/mem.h"
  15. #if defined(VPX_MAX_ALLOCABLE_MEMORY)
  16. #include "vp9/common/vp9_onyxc_int.h"
  17. #endif // VPX_MAX_ALLOCABLE_MEMORY
  18. /****************************************************************************
  19. * Exports
  20. ****************************************************************************/
  21. /****************************************************************************
  22. *
  23. ****************************************************************************/
  24. #define yv12_align_addr(addr, align) \
  25. (void *)(((size_t)(addr) + ((align)-1)) & (size_t) - (align))
  26. int vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
  27. if (ybf) {
  28. // If libvpx is using frame buffer callbacks then buffer_alloc_sz must
  29. // not be set.
  30. if (ybf->buffer_alloc_sz > 0) {
  31. vpx_free(ybf->buffer_alloc);
  32. }
  33. /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
  34. u_buffer and v_buffer point to buffer_alloc and are used. Clear out
  35. all of this so that a freed pointer isn't inadvertently used */
  36. memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
  37. } else {
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width,
  43. int height, int border) {
  44. if (ybf) {
  45. int aligned_width = (width + 15) & ~15;
  46. int aligned_height = (height + 15) & ~15;
  47. int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
  48. int yplane_size = (aligned_height + 2 * border) * y_stride;
  49. int uv_width = aligned_width >> 1;
  50. int uv_height = aligned_height >> 1;
  51. /** There is currently a bunch of code which assumes
  52. * uv_stride == y_stride/2, so enforce this here. */
  53. int uv_stride = y_stride >> 1;
  54. int uvplane_size = (uv_height + border) * uv_stride;
  55. const size_t frame_size = yplane_size + 2 * uvplane_size;
  56. if (!ybf->buffer_alloc) {
  57. ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
  58. #if defined(__has_feature)
  59. #if __has_feature(memory_sanitizer)
  60. // This memset is needed for fixing the issue of using uninitialized
  61. // value in msan test. It will cause a perf loss, so only do this for
  62. // msan test.
  63. memset(ybf->buffer_alloc, 0, frame_size);
  64. #endif
  65. #endif
  66. ybf->buffer_alloc_sz = frame_size;
  67. }
  68. if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size) return -1;
  69. /* Only support allocating buffers that have a border that's a multiple
  70. * of 32. The border restriction is required to get 16-byte alignment of
  71. * the start of the chroma rows without introducing an arbitrary gap
  72. * between planes, which would break the semantics of things like
  73. * vpx_img_set_rect(). */
  74. if (border & 0x1f) return -3;
  75. ybf->y_crop_width = width;
  76. ybf->y_crop_height = height;
  77. ybf->y_width = aligned_width;
  78. ybf->y_height = aligned_height;
  79. ybf->y_stride = y_stride;
  80. ybf->uv_crop_width = (width + 1) / 2;
  81. ybf->uv_crop_height = (height + 1) / 2;
  82. ybf->uv_width = uv_width;
  83. ybf->uv_height = uv_height;
  84. ybf->uv_stride = uv_stride;
  85. ybf->alpha_width = 0;
  86. ybf->alpha_height = 0;
  87. ybf->alpha_stride = 0;
  88. ybf->border = border;
  89. ybf->frame_size = frame_size;
  90. ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
  91. ybf->u_buffer =
  92. ybf->buffer_alloc + yplane_size + (border / 2 * uv_stride) + border / 2;
  93. ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size +
  94. (border / 2 * uv_stride) + border / 2;
  95. ybf->alpha_buffer = NULL;
  96. ybf->corrupted = 0; /* assume not currupted by errors */
  97. return 0;
  98. }
  99. return -2;
  100. }
  101. int vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
  102. int border) {
  103. if (ybf) {
  104. vp8_yv12_de_alloc_frame_buffer(ybf);
  105. return vp8_yv12_realloc_frame_buffer(ybf, width, height, border);
  106. }
  107. return -2;
  108. }
  109. #if CONFIG_VP9
  110. // TODO(jkoleszar): Maybe replace this with struct vpx_image
  111. int vpx_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
  112. if (ybf) {
  113. if (ybf->buffer_alloc_sz > 0) {
  114. vpx_free(ybf->buffer_alloc);
  115. }
  116. /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
  117. u_buffer and v_buffer point to buffer_alloc and are used. Clear out
  118. all of this so that a freed pointer isn't inadvertently used */
  119. memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
  120. } else {
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. int vpx_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
  126. int ss_x, int ss_y,
  127. #if CONFIG_VP9_HIGHBITDEPTH
  128. int use_highbitdepth,
  129. #endif
  130. int border, int byte_alignment,
  131. vpx_codec_frame_buffer_t *fb,
  132. vpx_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
  133. #if CONFIG_SIZE_LIMIT
  134. if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT) return -1;
  135. #endif
  136. /* Only support allocating buffers that have a border that's a multiple
  137. * of 32. The border restriction is required to get 16-byte alignment of
  138. * the start of the chroma rows without introducing an arbitrary gap
  139. * between planes, which would break the semantics of things like
  140. * vpx_img_set_rect(). */
  141. if (border & 0x1f) return -3;
  142. if (ybf) {
  143. const int vp9_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
  144. const int aligned_width = (width + 7) & ~7;
  145. const int aligned_height = (height + 7) & ~7;
  146. const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
  147. const uint64_t yplane_size =
  148. (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment;
  149. const int uv_width = aligned_width >> ss_x;
  150. const int uv_height = aligned_height >> ss_y;
  151. const int uv_stride = y_stride >> ss_x;
  152. const int uv_border_w = border >> ss_x;
  153. const int uv_border_h = border >> ss_y;
  154. const uint64_t uvplane_size =
  155. (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment;
  156. #if CONFIG_VP9_HIGHBITDEPTH
  157. const uint64_t frame_size =
  158. (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
  159. #else
  160. const uint64_t frame_size = yplane_size + 2 * uvplane_size;
  161. #endif // CONFIG_VP9_HIGHBITDEPTH
  162. uint8_t *buf = NULL;
  163. #if defined(VPX_MAX_ALLOCABLE_MEMORY)
  164. // The decoder may allocate REF_FRAMES frame buffers in the frame buffer
  165. // pool. Bound the total amount of allocated memory as if these REF_FRAMES
  166. // frame buffers were allocated in a single allocation.
  167. if (frame_size > VPX_MAX_ALLOCABLE_MEMORY / REF_FRAMES) return -1;
  168. #endif // VPX_MAX_ALLOCABLE_MEMORY
  169. // frame_size is stored in buffer_alloc_sz, which is a size_t. If it won't
  170. // fit, fail early.
  171. if (frame_size > SIZE_MAX) {
  172. return -1;
  173. }
  174. if (cb != NULL) {
  175. const int align_addr_extra_size = 31;
  176. const uint64_t external_frame_size = frame_size + align_addr_extra_size;
  177. assert(fb != NULL);
  178. if (external_frame_size != (size_t)external_frame_size) return -1;
  179. // Allocation to hold larger frame, or first allocation.
  180. if (cb(cb_priv, (size_t)external_frame_size, fb) < 0) return -1;
  181. if (fb->data == NULL || fb->size < external_frame_size) return -1;
  182. ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
  183. #if defined(__has_feature)
  184. #if __has_feature(memory_sanitizer)
  185. // This memset is needed for fixing the issue of using uninitialized
  186. // value in msan test. It will cause a perf loss, so only do this for
  187. // msan test.
  188. memset(ybf->buffer_alloc, 0, (size_t)frame_size);
  189. #endif
  190. #endif
  191. } else if (frame_size > ybf->buffer_alloc_sz) {
  192. // Allocation to hold larger frame, or first allocation.
  193. vpx_free(ybf->buffer_alloc);
  194. ybf->buffer_alloc = NULL;
  195. ybf->buffer_alloc_sz = 0;
  196. ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, (size_t)frame_size);
  197. if (!ybf->buffer_alloc) return -1;
  198. ybf->buffer_alloc_sz = (size_t)frame_size;
  199. // This memset is needed for fixing valgrind error from C loop filter
  200. // due to access uninitialized memory in frame border. It could be
  201. // removed if border is totally removed.
  202. memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
  203. }
  204. ybf->y_crop_width = width;
  205. ybf->y_crop_height = height;
  206. ybf->y_width = aligned_width;
  207. ybf->y_height = aligned_height;
  208. ybf->y_stride = y_stride;
  209. ybf->uv_crop_width = (width + ss_x) >> ss_x;
  210. ybf->uv_crop_height = (height + ss_y) >> ss_y;
  211. ybf->uv_width = uv_width;
  212. ybf->uv_height = uv_height;
  213. ybf->uv_stride = uv_stride;
  214. ybf->border = border;
  215. ybf->frame_size = (size_t)frame_size;
  216. ybf->subsampling_x = ss_x;
  217. ybf->subsampling_y = ss_y;
  218. buf = ybf->buffer_alloc;
  219. #if CONFIG_VP9_HIGHBITDEPTH
  220. if (use_highbitdepth) {
  221. // Store uint16 addresses when using 16bit framebuffers
  222. buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
  223. ybf->flags = YV12_FLAG_HIGHBITDEPTH;
  224. } else {
  225. ybf->flags = 0;
  226. }
  227. #endif // CONFIG_VP9_HIGHBITDEPTH
  228. ybf->y_buffer = (uint8_t *)yv12_align_addr(
  229. buf + (border * y_stride) + border, vp9_byte_align);
  230. ybf->u_buffer = (uint8_t *)yv12_align_addr(
  231. buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
  232. vp9_byte_align);
  233. ybf->v_buffer =
  234. (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
  235. (uv_border_h * uv_stride) + uv_border_w,
  236. vp9_byte_align);
  237. ybf->corrupted = 0; /* assume not corrupted by errors */
  238. return 0;
  239. }
  240. return -2;
  241. }
  242. int vpx_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
  243. int ss_x, int ss_y,
  244. #if CONFIG_VP9_HIGHBITDEPTH
  245. int use_highbitdepth,
  246. #endif
  247. int border, int byte_alignment) {
  248. if (ybf) {
  249. vpx_free_frame_buffer(ybf);
  250. return vpx_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
  251. #if CONFIG_VP9_HIGHBITDEPTH
  252. use_highbitdepth,
  253. #endif
  254. border, byte_alignment, NULL, NULL, NULL);
  255. }
  256. return -2;
  257. }
  258. #endif