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