]> granicus.if.org Git - libvpx/blob - vpx_scale/generic/yv12config.c
Revert "Revert "Add support for setting byte alignment.""
[libvpx] / vpx_scale / generic / yv12config.c
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
11 #include <assert.h>
12
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"
17 #endif
18
19 /****************************************************************************
20 *  Exports
21 ****************************************************************************/
22
23 /****************************************************************************
24  *
25  ****************************************************************************/
26 #define yv12_align_addr(addr, align) \
27     (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align))
28
29 int
30 vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
31   if (ybf) {
32     // If libvpx is using frame buffer callbacks then buffer_alloc_sz must
33     // not be set.
34     if (ybf->buffer_alloc_sz > 0) {
35       vpx_free(ybf->buffer_alloc);
36     }
37
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));
42   } else {
43     return -1;
44   }
45
46   return 0;
47 }
48
49 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
50                                   int width, int height, int border) {
51   if (ybf) {
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;
63
64     if (!ybf->buffer_alloc) {
65       ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
66       ybf->buffer_alloc_sz = frame_size;
67     }
68
69     if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size)
70       return -1;
71
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(). */
77     if (border & 0x1f)
78       return -3;
79
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;
85
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;
91
92     ybf->alpha_width = 0;
93     ybf->alpha_height = 0;
94     ybf->alpha_stride = 0;
95
96     ybf->border = border;
97     ybf->frame_size = frame_size;
98
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;
103
104     ybf->corrupted = 0; /* assume not currupted by errors */
105     return 0;
106   }
107   return -2;
108 }
109
110 int vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
111                                 int width, int height, int border) {
112   if (ybf) {
113     vp8_yv12_de_alloc_frame_buffer(ybf);
114     return vp8_yv12_realloc_frame_buffer(ybf, width, height, border);
115   }
116   return -2;
117 }
118
119 #if CONFIG_VP9
120 // TODO(jkoleszar): Maybe replace this with struct vpx_image
121
122 int vp9_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
123   if (ybf) {
124     if (ybf->buffer_alloc_sz > 0) {
125       vpx_free(ybf->buffer_alloc);
126     }
127
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));
132   } else {
133     return -1;
134   }
135
136   return 0;
137 }
138
139 int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
140                              int width, int height,
141                              int ss_x, int ss_y,
142 #if CONFIG_VP9_HIGHBITDEPTH
143                              int use_highbitdepth,
144 #endif
145                              int border,
146                              int byte_alignment,
147                              vpx_codec_frame_buffer_t *fb,
148                              vpx_get_frame_buffer_cb_fn_t cb,
149                              void *cb_priv) {
150   if (ybf) {
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;
164
165 #if CONFIG_ALPHA
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);
176 #else
177     const uint64_t frame_size = yplane_size + 2 * uvplane_size +
178                                 alpha_plane_size;
179 #endif  // CONFIG_VP9_HIGHBITDEPTH
180 #else
181 #if CONFIG_VP9_HIGHBITDEPTH
182     const uint64_t frame_size =
183         (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
184 #else
185     const uint64_t frame_size = yplane_size + 2 * uvplane_size;
186 #endif  // CONFIG_VP9_HIGHBITDEPTH
187 #endif  // CONFIG_ALPHA
188
189     uint8_t *buf = NULL;
190
191     if (cb != NULL) {
192       const int align_addr_extra_size = 31;
193       const uint64_t external_frame_size = frame_size + align_addr_extra_size;
194
195       assert(fb != NULL);
196
197       if (external_frame_size != (size_t)external_frame_size)
198         return -1;
199
200       // Allocation to hold larger frame, or first allocation.
201       if (cb(cb_priv, (size_t)external_frame_size, fb) < 0)
202         return -1;
203
204       if (fb->data == NULL || fb->size < external_frame_size)
205         return -1;
206
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;
212
213       if (frame_size != (size_t)frame_size)
214         return -1;
215
216       ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, (size_t)frame_size);
217       if (!ybf->buffer_alloc)
218         return -1;
219
220       ybf->buffer_alloc_sz = (int)frame_size;
221
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);
226     }
227
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(). */
233     if (border & 0x1f)
234       return -3;
235
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;
241
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;
247
248     ybf->border = border;
249     ybf->frame_size = (int)frame_size;
250     ybf->subsampling_x = ss_x;
251     ybf->subsampling_y = ss_y;
252
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;
259     } else {
260       ybf->flags = 0;
261     }
262 #endif  // CONFIG_VP9_HIGHBITDEPTH
263
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,
268         vp9_byte_align);
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);
272
273 #if CONFIG_ALPHA
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);
280 #endif
281     ybf->corrupted = 0; /* assume not corrupted by errors */
282     return 0;
283   }
284   return -2;
285 }
286
287 int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf,
288                            int width, int height,
289                            int ss_x, int ss_y,
290 #if CONFIG_VP9_HIGHBITDEPTH
291                            int use_highbitdepth,
292 #endif
293                            int border,
294                            int byte_alignment) {
295   if (ybf) {
296     vp9_free_frame_buffer(ybf);
297     return vp9_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
298 #if CONFIG_VP9_HIGHBITDEPTH
299                                     use_highbitdepth,
300 #endif
301                                     border, byte_alignment, NULL, NULL, NULL);
302   }
303   return -2;
304 }
305 #endif