xref: /aosp_15_r20/external/libvpx/vpx_scale/generic/yv12config.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 #include <limits.h>
13 
14 #include "vpx_scale/yv12config.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_ports/mem.h"
17 
18 #if defined(VPX_MAX_ALLOCABLE_MEMORY)
19 #include "vp9/common/vp9_onyxc_int.h"
20 #endif  // VPX_MAX_ALLOCABLE_MEMORY
21 /****************************************************************************
22  *  Exports
23  ****************************************************************************/
24 
25 /****************************************************************************
26  *
27  ****************************************************************************/
28 #define yv12_align_addr(addr, align) \
29   (void *)(((size_t)(addr) + ((align)-1)) & (size_t) - (align))
30 
vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG * ybf)31 int vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
32   if (ybf) {
33     // If libvpx is using frame buffer callbacks then buffer_alloc_sz must
34     // not be set.
35     if (ybf->buffer_alloc_sz > 0) {
36       vpx_free(ybf->buffer_alloc);
37     }
38 
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     memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
43   } else {
44     return -1;
45   }
46 
47   return 0;
48 }
49 
vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG * ybf,int width,int height,int border)50 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width,
51                                   int height, int border) {
52   if (ybf) {
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 size_t frame_size = yplane_size + 2 * uvplane_size;
64 
65     if (!ybf->buffer_alloc) {
66       ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
67       if (!ybf->buffer_alloc) {
68         ybf->buffer_alloc_sz = 0;
69         return -1;
70       }
71 #if defined(__has_feature)
72 #if __has_feature(memory_sanitizer)
73       // This memset is needed for fixing the issue of using uninitialized
74       // value in msan test. It will cause a perf loss, so only do this for
75       // msan test.
76       memset(ybf->buffer_alloc, 0, frame_size);
77 #endif
78 #endif
79       ybf->buffer_alloc_sz = frame_size;
80     }
81 
82     if (ybf->buffer_alloc_sz < frame_size) return -1;
83 
84     /* Only support allocating buffers that have a border that's a multiple
85      * of 32. The border restriction is required to get 16-byte alignment of
86      * the start of the chroma rows without introducing an arbitrary gap
87      * between planes, which would break the semantics of things like
88      * vpx_img_set_rect(). */
89     if (border & 0x1f) return -3;
90 
91     ybf->y_crop_width = width;
92     ybf->y_crop_height = height;
93     ybf->y_width = aligned_width;
94     ybf->y_height = aligned_height;
95     ybf->y_stride = y_stride;
96 
97     ybf->uv_crop_width = (width + 1) / 2;
98     ybf->uv_crop_height = (height + 1) / 2;
99     ybf->uv_width = uv_width;
100     ybf->uv_height = uv_height;
101     ybf->uv_stride = uv_stride;
102 
103     ybf->alpha_width = 0;
104     ybf->alpha_height = 0;
105     ybf->alpha_stride = 0;
106 
107     ybf->border = border;
108     ybf->frame_size = frame_size;
109 
110     ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
111     ybf->u_buffer =
112         ybf->buffer_alloc + yplane_size + (border / 2 * uv_stride) + border / 2;
113     ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size +
114                     (border / 2 * uv_stride) + border / 2;
115     ybf->alpha_buffer = NULL;
116 
117     ybf->corrupted = 0; /* assume not currupted by errors */
118     return 0;
119   }
120   return -2;
121 }
122 
vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG * ybf,int width,int height,int border)123 int vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
124                                 int border) {
125   if (ybf) {
126     vp8_yv12_de_alloc_frame_buffer(ybf);
127     return vp8_yv12_realloc_frame_buffer(ybf, width, height, border);
128   }
129   return -2;
130 }
131 
132 #if CONFIG_VP9
133 // TODO(jkoleszar): Maybe replace this with struct vpx_image
134 
vpx_free_frame_buffer(YV12_BUFFER_CONFIG * ybf)135 int vpx_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
136   if (ybf) {
137     if (ybf->buffer_alloc_sz > 0) {
138       vpx_free(ybf->buffer_alloc);
139     }
140 
141     /* buffer_alloc isn't accessed by most functions.  Rather y_buffer,
142       u_buffer and v_buffer point to buffer_alloc and are used.  Clear out
143       all of this so that a freed pointer isn't inadvertently used */
144     memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
145   } else {
146     return -1;
147   }
148 
149   return 0;
150 }
151 
vpx_realloc_frame_buffer(YV12_BUFFER_CONFIG * ybf,int width,int height,int ss_x,int ss_y,int use_highbitdepth,int border,int byte_alignment,vpx_codec_frame_buffer_t * fb,vpx_get_frame_buffer_cb_fn_t cb,void * cb_priv)152 int vpx_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
153                              int ss_x, int ss_y,
154 #if CONFIG_VP9_HIGHBITDEPTH
155                              int use_highbitdepth,
156 #endif
157                              int border, int byte_alignment,
158                              vpx_codec_frame_buffer_t *fb,
159                              vpx_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
160 #if CONFIG_SIZE_LIMIT
161   if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT) return -1;
162 #endif
163 
164   /* Only support allocating buffers that have a border that's a multiple
165    * of 32. The border restriction is required to get 16-byte alignment of
166    * the start of the chroma rows without introducing an arbitrary gap
167    * between planes, which would break the semantics of things like
168    * vpx_img_set_rect(). */
169   if (border & 0x1f) return -3;
170 
171   if (ybf) {
172     const int vp9_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
173     const int aligned_width = (width + 7) & ~7;
174     const int aligned_height = (height + 7) & ~7;
175     const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
176     const uint64_t yplane_size =
177         (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment;
178     const int uv_width = aligned_width >> ss_x;
179     const int uv_height = aligned_height >> ss_y;
180     const int uv_stride = y_stride >> ss_x;
181     const int uv_border_w = border >> ss_x;
182     const int uv_border_h = border >> ss_y;
183     const uint64_t uvplane_size =
184         (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment;
185 
186 #if CONFIG_VP9_HIGHBITDEPTH
187     const uint64_t frame_size =
188         (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
189 #else
190     const uint64_t frame_size = yplane_size + 2 * uvplane_size;
191 #endif  // CONFIG_VP9_HIGHBITDEPTH
192 
193     uint8_t *buf = NULL;
194 
195 #if defined(VPX_MAX_ALLOCABLE_MEMORY)
196     // The decoder may allocate REF_FRAMES frame buffers in the frame buffer
197     // pool. Bound the total amount of allocated memory as if these REF_FRAMES
198     // frame buffers were allocated in a single allocation.
199     if (frame_size > VPX_MAX_ALLOCABLE_MEMORY / REF_FRAMES) return -1;
200 #endif  // VPX_MAX_ALLOCABLE_MEMORY
201 
202     // frame_size is stored in buffer_alloc_sz, which is a size_t. If it won't
203     // fit, fail early.
204     if (frame_size > SIZE_MAX) {
205       return -1;
206     }
207 
208     if (cb != NULL) {
209       const int align_addr_extra_size = 31;
210       const uint64_t external_frame_size = frame_size + align_addr_extra_size;
211 
212       assert(fb != NULL);
213 
214       if (external_frame_size != (size_t)external_frame_size) return -1;
215 
216       // Allocation to hold larger frame, or first allocation.
217       if (cb(cb_priv, (size_t)external_frame_size, fb) < 0) return -1;
218 
219       if (fb->data == NULL || fb->size < external_frame_size) return -1;
220 
221       ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
222 
223 #if defined(__has_feature)
224 #if __has_feature(memory_sanitizer)
225       // This memset is needed for fixing the issue of using uninitialized
226       // value in msan test. It will cause a perf loss, so only do this for
227       // msan test.
228       memset(ybf->buffer_alloc, 0, (size_t)frame_size);
229 #endif
230 #endif
231     } else if (frame_size > ybf->buffer_alloc_sz) {
232       // Allocation to hold larger frame, or first allocation.
233       vpx_free(ybf->buffer_alloc);
234       ybf->buffer_alloc = NULL;
235       ybf->buffer_alloc_sz = 0;
236 
237       ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, (size_t)frame_size);
238       if (!ybf->buffer_alloc) return -1;
239 
240       ybf->buffer_alloc_sz = (size_t)frame_size;
241 
242       // This memset is needed for fixing valgrind error from C loop filter
243       // due to access uninitialized memory in frame border. It could be
244       // removed if border is totally removed.
245       memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
246     }
247 
248     ybf->y_crop_width = width;
249     ybf->y_crop_height = height;
250     ybf->y_width = aligned_width;
251     ybf->y_height = aligned_height;
252     ybf->y_stride = y_stride;
253 
254     ybf->uv_crop_width = (width + ss_x) >> ss_x;
255     ybf->uv_crop_height = (height + ss_y) >> ss_y;
256     ybf->uv_width = uv_width;
257     ybf->uv_height = uv_height;
258     ybf->uv_stride = uv_stride;
259 
260     ybf->border = border;
261     ybf->frame_size = (size_t)frame_size;
262     ybf->subsampling_x = ss_x;
263     ybf->subsampling_y = ss_y;
264 
265     buf = ybf->buffer_alloc;
266 #if CONFIG_VP9_HIGHBITDEPTH
267     if (use_highbitdepth) {
268       // Store uint16 addresses when using 16bit framebuffers
269       buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
270       ybf->flags = YV12_FLAG_HIGHBITDEPTH;
271     } else {
272       ybf->flags = 0;
273     }
274 #endif  // CONFIG_VP9_HIGHBITDEPTH
275 
276     ybf->y_buffer = (uint8_t *)yv12_align_addr(
277         buf + (border * y_stride) + border, vp9_byte_align);
278     ybf->u_buffer = (uint8_t *)yv12_align_addr(
279         buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
280         vp9_byte_align);
281     ybf->v_buffer =
282         (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
283                                        (uv_border_h * uv_stride) + uv_border_w,
284                                    vp9_byte_align);
285 
286     ybf->corrupted = 0; /* assume not corrupted by errors */
287     return 0;
288   }
289   return -2;
290 }
291 
vpx_alloc_frame_buffer(YV12_BUFFER_CONFIG * ybf,int width,int height,int ss_x,int ss_y,int use_highbitdepth,int border,int byte_alignment)292 int vpx_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
293                            int ss_x, int ss_y,
294 #if CONFIG_VP9_HIGHBITDEPTH
295                            int use_highbitdepth,
296 #endif
297                            int border, int byte_alignment) {
298   if (ybf) {
299     vpx_free_frame_buffer(ybf);
300     return vpx_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
301 #if CONFIG_VP9_HIGHBITDEPTH
302                                     use_highbitdepth,
303 #endif
304                                     border, byte_alignment, NULL, NULL, NULL);
305   }
306   return -2;
307 }
308 #endif
309