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_config.h"
14 #include "vpx_scale/yv12config.h"
15 #include "vpx_mem/vpx_mem.h"
16 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
17 #include "vp9/common/vp9_common.h"
20 /****************************************************************************
22 ****************************************************************************/
24 /****************************************************************************
26 ****************************************************************************/
27 #define yv12_align_addr(addr, align) \
28 (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align))
31 vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
33 // If libvpx is using frame buffer callbacks then buffer_alloc_sz must
35 if (ybf->buffer_alloc_sz > 0) {
36 vpx_free(ybf->buffer_alloc);
39 /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
40 u_buffer and v_buffer point to buffer_alloc and are used. Clear out
41 all of this so that a freed pointer isn't inadvertently used */
42 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
50 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
51 int width, int height, int border) {
53 int aligned_width = (width + 15) & ~15;
54 int aligned_height = (height + 15) & ~15;
55 int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
56 int yplane_size = (aligned_height + 2 * border) * y_stride;
57 int uv_width = aligned_width >> 1;
58 int uv_height = aligned_height >> 1;
59 /** There is currently a bunch of code which assumes
60 * uv_stride == y_stride/2, so enforce this here. */
61 int uv_stride = y_stride >> 1;
62 int uvplane_size = (uv_height + border) * uv_stride;
63 const int frame_size = yplane_size + 2 * uvplane_size;
65 if (!ybf->buffer_alloc) {
66 ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
67 ybf->buffer_alloc_sz = frame_size;
70 if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size)
73 /* Only support allocating buffers that have a border that's a multiple
74 * of 32. The border restriction is required to get 16-byte alignment of
75 * the start of the chroma rows without introducing an arbitrary gap
76 * between planes, which would break the semantics of things like
77 * vpx_img_set_rect(). */
81 ybf->y_crop_width = width;
82 ybf->y_crop_height = height;
83 ybf->y_width = aligned_width;
84 ybf->y_height = aligned_height;
85 ybf->y_stride = y_stride;
87 ybf->uv_crop_width = (width + 1) / 2;
88 ybf->uv_crop_height = (height + 1) / 2;
89 ybf->uv_width = uv_width;
90 ybf->uv_height = uv_height;
91 ybf->uv_stride = uv_stride;
94 ybf->alpha_height = 0;
95 ybf->alpha_stride = 0;
98 ybf->frame_size = frame_size;
100 ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
101 ybf->u_buffer = ybf->buffer_alloc + yplane_size + (border / 2 * uv_stride) + border / 2;
102 ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size + (border / 2 * uv_stride) + border / 2;
103 ybf->alpha_buffer = NULL;
105 ybf->corrupted = 0; /* assume not currupted by errors */
111 int vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
112 int width, int height, int border) {
114 vp8_yv12_de_alloc_frame_buffer(ybf);
115 return vp8_yv12_realloc_frame_buffer(ybf, width, height, border);
121 // TODO(jkoleszar): Maybe replace this with struct vpx_image
123 int vp9_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
125 if (ybf->buffer_alloc_sz > 0) {
126 vpx_free(ybf->buffer_alloc);
129 /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
130 u_buffer and v_buffer point to buffer_alloc and are used. Clear out
131 all of this so that a freed pointer isn't inadvertently used */
132 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
140 int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
141 int width, int height,
143 #if CONFIG_VP9_HIGHBITDEPTH
144 int use_highbitdepth,
147 vpx_codec_frame_buffer_t *fb,
148 vpx_get_frame_buffer_cb_fn_t cb,
151 const int aligned_width = (width + 7) & ~7;
152 const int aligned_height = (height + 7) & ~7;
153 const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
154 const uint64_t yplane_size = (aligned_height + 2 * border) *
156 const int uv_width = aligned_width >> ss_x;
157 const int uv_height = aligned_height >> ss_y;
158 const int uv_stride = y_stride >> ss_x;
159 const int uv_border_w = border >> ss_x;
160 const int uv_border_h = border >> ss_y;
161 const uint64_t uvplane_size = (uv_height + 2 * uv_border_h) *
164 const int alpha_width = aligned_width;
165 const int alpha_height = aligned_height;
166 const int alpha_stride = y_stride;
167 const int alpha_border_w = border;
168 const int alpha_border_h = border;
169 const uint64_t alpha_plane_size = (alpha_height + 2 * alpha_border_h) *
170 (uint64_t)alpha_stride;
171 #if CONFIG_VP9_HIGHBITDEPTH
172 const uint64_t frame_size = (1 + use_highbitdepth) *
173 (yplane_size + 2 * uvplane_size + alpha_plane_size);
175 const uint64_t frame_size = yplane_size + 2 * uvplane_size +
177 #endif // CONFIG_VP9_HIGHBITDEPTH
179 #if CONFIG_VP9_HIGHBITDEPTH
180 const uint64_t frame_size =
181 (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
183 const uint64_t frame_size = yplane_size + 2 * uvplane_size;
184 #endif // CONFIG_VP9_HIGHBITDEPTH
185 #endif // CONFIG_ALPHA
187 const int align_addr_extra_size = 31;
188 const uint64_t external_frame_size = frame_size + align_addr_extra_size;
192 if (external_frame_size != (size_t)external_frame_size)
195 // Allocation to hold larger frame, or first allocation.
196 if (cb(cb_priv, (size_t)external_frame_size, fb) < 0)
199 if (fb->data == NULL || fb->size < external_frame_size)
202 // This memset is needed for fixing valgrind error from C loop filter
203 // due to access uninitialized memory in frame border. It could be
204 // removed if border is totally removed.
205 vpx_memset(fb->data, 0, fb->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;
251 #if CONFIG_VP9_HIGHBITDEPTH
252 if (use_highbitdepth) {
253 // Store uint16 addresses when using 16bit framebuffers
254 uint8_t *p = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
255 ybf->y_buffer = p + (border * y_stride) + border;
256 ybf->u_buffer = p + yplane_size +
257 (uv_border_h * uv_stride) + uv_border_w;
258 ybf->v_buffer = p + yplane_size + uvplane_size +
259 (uv_border_h * uv_stride) + uv_border_w;
260 ybf->flags = YV12_FLAG_HIGHBITDEPTH;
262 ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
263 ybf->u_buffer = ybf->buffer_alloc + yplane_size +
264 (uv_border_h * uv_stride) + uv_border_w;
265 ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size +
266 (uv_border_h * uv_stride) + uv_border_w;
270 ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
271 ybf->u_buffer = ybf->buffer_alloc + yplane_size +
272 (uv_border_h * uv_stride) + uv_border_w;
273 ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size +
274 (uv_border_h * uv_stride) + uv_border_w;
275 #endif // CONFIG_VP9_HIGHBITDEPTH
278 ybf->alpha_width = alpha_width;
279 ybf->alpha_height = alpha_height;
280 ybf->alpha_stride = alpha_stride;
281 ybf->alpha_buffer = ybf->buffer_alloc + yplane_size + 2 * uvplane_size +
282 (alpha_border_h * alpha_stride) + alpha_border_w;
284 ybf->corrupted = 0; /* assume not corrupted by errors */
290 int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
291 int width, int height,
293 #if CONFIG_VP9_HIGHBITDEPTH
294 int use_highbitdepth,
298 vp9_free_frame_buffer(ybf);
299 return vp9_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
300 #if CONFIG_VP9_HIGHBITDEPTH
303 border, NULL, NULL, NULL);