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