yv12config.c 9.5 KB

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