2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 #include "vpx_scale/yv12config.h"
14 #include "vpx_mem/vpx_mem.h"
15 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
16 #include "vp9/common/vp9_common.h"
19 /****************************************************************************
21 ****************************************************************************/
23 /****************************************************************************
25 ****************************************************************************/
26 #define yv12_align_addr(addr, align) \
27 (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align))
30 vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
32 // If libvpx is using frame buffer callbacks then buffer_alloc_sz must
34 if (ybf->buffer_alloc_sz > 0) {
35 vpx_free(ybf->buffer_alloc);
38 /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
39 u_buffer and v_buffer point to buffer_alloc and are used. Clear out
40 all of this so that a freed pointer isn't inadvertently used */
41 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
49 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
50 int width, int height, int border) {
52 int aligned_width = (width + 15) & ~15;
53 int aligned_height = (height + 15) & ~15;
54 int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
55 int yplane_size = (aligned_height + 2 * border) * y_stride;
56 int uv_width = aligned_width >> 1;
57 int uv_height = aligned_height >> 1;
58 /** There is currently a bunch of code which assumes
59 * uv_stride == y_stride/2, so enforce this here. */
60 int uv_stride = y_stride >> 1;
61 int uvplane_size = (uv_height + border) * uv_stride;
62 const int frame_size = yplane_size + 2 * uvplane_size;
64 if (!ybf->buffer_alloc) {
65 ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
66 ybf->buffer_alloc_sz = frame_size;
69 if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size)
72 /* Only support allocating buffers that have a border that's a multiple
73 * of 32. The border restriction is required to get 16-byte alignment of
74 * the start of the chroma rows without introducing an arbitrary gap
75 * between planes, which would break the semantics of things like
76 * vpx_img_set_rect(). */
80 ybf->y_crop_width = width;
81 ybf->y_crop_height = height;
82 ybf->y_width = aligned_width;
83 ybf->y_height = aligned_height;
84 ybf->y_stride = y_stride;
86 ybf->uv_crop_width = (width + 1) / 2;
87 ybf->uv_crop_height = (height + 1) / 2;
88 ybf->uv_width = uv_width;
89 ybf->uv_height = uv_height;
90 ybf->uv_stride = uv_stride;
93 ybf->alpha_height = 0;
94 ybf->alpha_stride = 0;
97 ybf->frame_size = frame_size;
99 ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
100 ybf->u_buffer = ybf->buffer_alloc + yplane_size + (border / 2 * uv_stride) + border / 2;
101 ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size + (border / 2 * uv_stride) + border / 2;
102 ybf->alpha_buffer = NULL;
104 ybf->corrupted = 0; /* assume not currupted by errors */
110 int vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
111 int width, int height, int border) {
113 vp8_yv12_de_alloc_frame_buffer(ybf);
114 return vp8_yv12_realloc_frame_buffer(ybf, width, height, border);
120 // TODO(jkoleszar): Maybe replace this with struct vpx_image
122 int vp9_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
124 if (ybf->buffer_alloc_sz > 0) {
125 vpx_free(ybf->buffer_alloc);
128 /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
129 u_buffer and v_buffer point to buffer_alloc and are used. Clear out
130 all of this so that a freed pointer isn't inadvertently used */
131 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
139 int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
140 int width, int height,
142 #if CONFIG_VP9_HIGHBITDEPTH
143 int use_highbitdepth,
147 vpx_codec_frame_buffer_t *fb,
148 vpx_get_frame_buffer_cb_fn_t cb,
151 const int vp9_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
152 const int aligned_width = (width + 7) & ~7;
153 const int aligned_height = (height + 7) & ~7;
154 const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
155 const uint64_t yplane_size = (aligned_height + 2 * border) *
156 (uint64_t)y_stride + byte_alignment;
157 const int uv_width = aligned_width >> ss_x;
158 const int uv_height = aligned_height >> ss_y;
159 const int uv_stride = y_stride >> ss_x;
160 const int uv_border_w = border >> ss_x;
161 const int uv_border_h = border >> ss_y;
162 const uint64_t uvplane_size = (uv_height + 2 * uv_border_h) *
163 (uint64_t)uv_stride + byte_alignment;
166 const int alpha_width = aligned_width;
167 const int alpha_height = aligned_height;
168 const int alpha_stride = y_stride;
169 const int alpha_border_w = border;
170 const int alpha_border_h = border;
171 const uint64_t alpha_plane_size = (alpha_height + 2 * alpha_border_h) *
172 (uint64_t)alpha_stride + byte_alignment;
173 #if CONFIG_VP9_HIGHBITDEPTH
174 const uint64_t frame_size = (1 + use_highbitdepth) *
175 (yplane_size + 2 * uvplane_size + alpha_plane_size);
177 const uint64_t frame_size = yplane_size + 2 * uvplane_size +
179 #endif // CONFIG_VP9_HIGHBITDEPTH
181 #if CONFIG_VP9_HIGHBITDEPTH
182 const uint64_t frame_size =
183 (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
185 const uint64_t frame_size = yplane_size + 2 * uvplane_size;
186 #endif // CONFIG_VP9_HIGHBITDEPTH
187 #endif // CONFIG_ALPHA
192 const int align_addr_extra_size = 31;
193 const uint64_t external_frame_size = frame_size + align_addr_extra_size;
197 if (external_frame_size != (size_t)external_frame_size)
200 // Allocation to hold larger frame, or first allocation.
201 if (cb(cb_priv, (size_t)external_frame_size, fb) < 0)
204 if (fb->data == NULL || fb->size < external_frame_size)
207 ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
208 } else if (frame_size > (size_t)ybf->buffer_alloc_sz) {
209 // Allocation to hold larger frame, or first allocation.
210 vpx_free(ybf->buffer_alloc);
211 ybf->buffer_alloc = NULL;
213 if (frame_size != (size_t)frame_size)
216 ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, (size_t)frame_size);
217 if (!ybf->buffer_alloc)
220 ybf->buffer_alloc_sz = (int)frame_size;
222 // This memset is needed for fixing valgrind error from C loop filter
223 // due to access uninitialized memory in frame border. It could be
224 // removed if border is totally removed.
225 vpx_memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
228 /* Only support allocating buffers that have a border that's a multiple
229 * of 32. The border restriction is required to get 16-byte alignment of
230 * the start of the chroma rows without introducing an arbitrary gap
231 * between planes, which would break the semantics of things like
232 * vpx_img_set_rect(). */
236 ybf->y_crop_width = width;
237 ybf->y_crop_height = height;
238 ybf->y_width = aligned_width;
239 ybf->y_height = aligned_height;
240 ybf->y_stride = y_stride;
242 ybf->uv_crop_width = (width + ss_x) >> ss_x;
243 ybf->uv_crop_height = (height + ss_y) >> ss_y;
244 ybf->uv_width = uv_width;
245 ybf->uv_height = uv_height;
246 ybf->uv_stride = uv_stride;
248 ybf->border = border;
249 ybf->frame_size = (int)frame_size;
250 ybf->subsampling_x = ss_x;
251 ybf->subsampling_y = ss_y;
253 buf = ybf->buffer_alloc;
254 #if CONFIG_VP9_HIGHBITDEPTH
255 if (use_highbitdepth) {
256 // Store uint16 addresses when using 16bit framebuffers
257 buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
258 ybf->flags = YV12_FLAG_HIGHBITDEPTH;
262 #endif // CONFIG_VP9_HIGHBITDEPTH
264 ybf->y_buffer = (uint8_t *)yv12_align_addr(
265 buf + (border * y_stride) + border, vp9_byte_align);
266 ybf->u_buffer = (uint8_t *)yv12_align_addr(
267 buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
269 ybf->v_buffer = (uint8_t *)yv12_align_addr(
270 buf + yplane_size + uvplane_size + (uv_border_h * uv_stride) +
271 uv_border_w, vp9_byte_align);
274 ybf->alpha_width = alpha_width;
275 ybf->alpha_height = alpha_height;
276 ybf->alpha_stride = alpha_stride;
277 ybf->alpha_buffer = (uint8_t *)yv12_align_addr(
278 buf + yplane_size + 2 * uvplane_size +
279 (alpha_border_h * alpha_stride) + alpha_border_w, vp9_byte_align);
281 ybf->corrupted = 0; /* assume not corrupted by errors */
287 int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
288 int width, int height,
290 #if CONFIG_VP9_HIGHBITDEPTH
291 int use_highbitdepth,
294 int byte_alignment) {
296 vp9_free_frame_buffer(ybf);
297 return vp9_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
298 #if CONFIG_VP9_HIGHBITDEPTH
301 border, byte_alignment, NULL, NULL, NULL);