• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <assert.h>
13 
14 #include "aom/internal/aom_image_internal.h"
15 #include "aom_mem/aom_mem.h"
16 #include "aom_ports/mem.h"
17 #include "aom_scale/yv12config.h"
18 #include "av1/common/enums.h"
19 
20 /****************************************************************************
21  *  Exports
22  ****************************************************************************/
23 
24 /****************************************************************************
25  *
26  ****************************************************************************/
27 
28 // TODO(jkoleszar): Maybe replace this with struct aom_image
aom_free_frame_buffer(YV12_BUFFER_CONFIG * ybf)29 int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
30   if (ybf) {
31     if (ybf->buffer_alloc_sz > 0) {
32       aom_free(ybf->buffer_alloc);
33     }
34     if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
35     aom_remove_metadata_from_frame_buffer(ybf);
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     memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
40     return 0;
41   }
42 
43   return AOM_CODEC_MEM_ERROR;
44 }
45 
realloc_frame_buffer_aligned(YV12_BUFFER_CONFIG * ybf,int width,int height,int ss_x,int ss_y,int use_highbitdepth,int border,int byte_alignment,aom_codec_frame_buffer_t * fb,aom_get_frame_buffer_cb_fn_t cb,void * cb_priv,const int y_stride,const uint64_t yplane_size,const uint64_t uvplane_size,const int aligned_width,const int aligned_height,const int uv_width,const int uv_height,const int uv_stride,const int uv_border_w,const int uv_border_h,int alloc_y_buffer_8bit,int alloc_y_plane_only)46 static int realloc_frame_buffer_aligned(
47     YV12_BUFFER_CONFIG *ybf, int width, int height, int ss_x, int ss_y,
48     int use_highbitdepth, int border, int byte_alignment,
49     aom_codec_frame_buffer_t *fb, aom_get_frame_buffer_cb_fn_t cb,
50     void *cb_priv, const int y_stride, const uint64_t yplane_size,
51     const uint64_t uvplane_size, const int aligned_width,
52     const int aligned_height, const int uv_width, const int uv_height,
53     const int uv_stride, const int uv_border_w, const int uv_border_h,
54     int alloc_y_buffer_8bit, int alloc_y_plane_only) {
55   if (ybf) {
56     const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
57     const uint64_t frame_size =
58         (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
59 
60     uint8_t *buf = NULL;
61 
62 #if defined AOM_MAX_ALLOCABLE_MEMORY
63     // The size of ybf->buffer_alloc.
64     uint64_t alloc_size = frame_size;
65     // The size of ybf->y_buffer_8bit.
66     if (use_highbitdepth) alloc_size += yplane_size;
67     // The decoder may allocate REF_FRAMES frame buffers in the frame buffer
68     // pool. Bound the total amount of allocated memory as if these REF_FRAMES
69     // frame buffers were allocated in a single allocation.
70     if (alloc_size > AOM_MAX_ALLOCABLE_MEMORY / REF_FRAMES)
71       return AOM_CODEC_MEM_ERROR;
72 #endif
73 
74     if (cb != NULL) {
75       const int align_addr_extra_size = 31;
76       const uint64_t external_frame_size = frame_size + align_addr_extra_size;
77 
78       assert(fb != NULL);
79 
80       if (external_frame_size != (size_t)external_frame_size)
81         return AOM_CODEC_MEM_ERROR;
82 
83       // Allocation to hold larger frame, or first allocation.
84       if (cb(cb_priv, (size_t)external_frame_size, fb) < 0)
85         return AOM_CODEC_MEM_ERROR;
86 
87       if (fb->data == NULL || fb->size < external_frame_size)
88         return AOM_CODEC_MEM_ERROR;
89 
90       ybf->buffer_alloc = (uint8_t *)aom_align_addr(fb->data, 32);
91 
92 #if defined(__has_feature)
93 #if __has_feature(memory_sanitizer)
94       // This memset is needed for fixing the issue of using uninitialized
95       // value in msan test. It will cause a perf loss, so only do this for
96       // msan test.
97       memset(ybf->buffer_alloc, 0, (size_t)frame_size);
98 #endif
99 #endif
100     } else if (frame_size > ybf->buffer_alloc_sz) {
101       // Allocation to hold larger frame, or first allocation.
102       aom_free(ybf->buffer_alloc);
103       ybf->buffer_alloc = NULL;
104       ybf->buffer_alloc_sz = 0;
105 
106       if (frame_size != (size_t)frame_size) return AOM_CODEC_MEM_ERROR;
107 
108       ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size);
109       if (!ybf->buffer_alloc) return AOM_CODEC_MEM_ERROR;
110 
111       ybf->buffer_alloc_sz = (size_t)frame_size;
112 
113       // This memset is needed for fixing valgrind error from C loop filter
114       // due to access uninitialized memory in frame border. It could be
115       // removed if border is totally removed.
116       memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
117     }
118 
119     ybf->y_crop_width = width;
120     ybf->y_crop_height = height;
121     ybf->y_width = aligned_width;
122     ybf->y_height = aligned_height;
123     ybf->y_stride = y_stride;
124 
125     ybf->uv_crop_width = (width + ss_x) >> ss_x;
126     ybf->uv_crop_height = (height + ss_y) >> ss_y;
127     ybf->uv_width = uv_width;
128     ybf->uv_height = uv_height;
129     ybf->uv_stride = uv_stride;
130 
131     ybf->border = border;
132     ybf->frame_size = (size_t)frame_size;
133     ybf->subsampling_x = ss_x;
134     ybf->subsampling_y = ss_y;
135 
136     buf = ybf->buffer_alloc;
137     if (use_highbitdepth) {
138       // Store uint16 addresses when using 16bit framebuffers
139       buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
140       ybf->flags = YV12_FLAG_HIGHBITDEPTH;
141     } else {
142       ybf->flags = 0;
143     }
144 
145     ybf->y_buffer = (uint8_t *)aom_align_addr(
146         buf + (border * y_stride) + border, aom_byte_align);
147     if (!alloc_y_plane_only) {
148       ybf->u_buffer = (uint8_t *)aom_align_addr(
149           buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
150           aom_byte_align);
151       ybf->v_buffer =
152           (uint8_t *)aom_align_addr(buf + yplane_size + uvplane_size +
153                                         (uv_border_h * uv_stride) + uv_border_w,
154                                     aom_byte_align);
155     } else {
156       ybf->u_buffer = NULL;
157       ybf->v_buffer = NULL;
158     }
159 
160     ybf->use_external_reference_buffers = 0;
161 
162     if (use_highbitdepth && alloc_y_buffer_8bit) {
163       if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
164       ybf->y_buffer_8bit = (uint8_t *)aom_memalign(32, (size_t)yplane_size);
165       if (!ybf->y_buffer_8bit) return AOM_CODEC_MEM_ERROR;
166     } else {
167       if (ybf->y_buffer_8bit) {
168         aom_free(ybf->y_buffer_8bit);
169         ybf->y_buffer_8bit = NULL;
170         ybf->buf_8bit_valid = 0;
171       }
172     }
173 
174     ybf->corrupted = 0; /* assume not corrupted by errors */
175     return 0;
176   }
177   return AOM_CODEC_MEM_ERROR;
178 }
179 
calc_stride_and_planesize(const int ss_x,const int ss_y,const int aligned_width,const int aligned_height,const int border,const int byte_alignment,int alloc_y_plane_only,int * y_stride,int * uv_stride,uint64_t * yplane_size,uint64_t * uvplane_size,const int uv_height)180 static int calc_stride_and_planesize(
181     const int ss_x, const int ss_y, const int aligned_width,
182     const int aligned_height, const int border, const int byte_alignment,
183     int alloc_y_plane_only, int *y_stride, int *uv_stride,
184     uint64_t *yplane_size, uint64_t *uvplane_size, const int uv_height) {
185   /* Only support allocating buffers that have a border that's a multiple
186    * of 32. The border restriction is required to get 16-byte alignment of
187    * the start of the chroma rows without introducing an arbitrary gap
188    * between planes, which would break the semantics of things like
189    * aom_img_set_rect(). */
190   if (border & 0x1f) return AOM_CODEC_MEM_ERROR;
191   *y_stride = aom_calc_y_stride(aligned_width, border);
192   *yplane_size =
193       (aligned_height + 2 * border) * (uint64_t)(*y_stride) + byte_alignment;
194 
195   if (!alloc_y_plane_only) {
196     *uv_stride = *y_stride >> ss_x;
197     *uvplane_size =
198         (uv_height + 2 * (border >> ss_y)) * (uint64_t)(*uv_stride) +
199         byte_alignment;
200   } else {
201     *uv_stride = 0;
202     *uvplane_size = 0;
203   }
204   return 0;
205 }
206 
aom_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,aom_codec_frame_buffer_t * fb,aom_get_frame_buffer_cb_fn_t cb,void * cb_priv,int alloc_y_buffer_8bit,int alloc_y_plane_only)207 int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
208                              int ss_x, int ss_y, int use_highbitdepth,
209                              int border, int byte_alignment,
210                              aom_codec_frame_buffer_t *fb,
211                              aom_get_frame_buffer_cb_fn_t cb, void *cb_priv,
212                              int alloc_y_buffer_8bit, int alloc_y_plane_only) {
213 #if CONFIG_SIZE_LIMIT
214   if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
215     return AOM_CODEC_MEM_ERROR;
216 #endif
217 
218   if (ybf) {
219     int y_stride = 0;
220     int uv_stride = 0;
221     uint64_t yplane_size = 0;
222     uint64_t uvplane_size = 0;
223     const int aligned_width = (width + 7) & ~7;
224     const int aligned_height = (height + 7) & ~7;
225     const int uv_width = aligned_width >> ss_x;
226     const int uv_height = aligned_height >> ss_y;
227     const int uv_border_w = border >> ss_x;
228     const int uv_border_h = border >> ss_y;
229 
230     int error = calc_stride_and_planesize(
231         ss_x, ss_y, aligned_width, aligned_height, border, byte_alignment,
232         alloc_y_plane_only, &y_stride, &uv_stride, &yplane_size, &uvplane_size,
233         uv_height);
234     if (error) return error;
235     return realloc_frame_buffer_aligned(
236         ybf, width, height, ss_x, ss_y, use_highbitdepth, border,
237         byte_alignment, fb, cb, cb_priv, y_stride, yplane_size, uvplane_size,
238         aligned_width, aligned_height, uv_width, uv_height, uv_stride,
239         uv_border_w, uv_border_h, alloc_y_buffer_8bit, alloc_y_plane_only);
240   }
241   return AOM_CODEC_MEM_ERROR;
242 }
243 
aom_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,int alloc_y_plane_only)244 int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
245                            int ss_x, int ss_y, int use_highbitdepth, int border,
246                            int byte_alignment, int alloc_y_plane_only) {
247   if (ybf) {
248     aom_free_frame_buffer(ybf);
249     return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
250                                     use_highbitdepth, border, byte_alignment,
251                                     NULL, NULL, NULL, 0, alloc_y_plane_only);
252   }
253   return AOM_CODEC_MEM_ERROR;
254 }
255 
aom_remove_metadata_from_frame_buffer(YV12_BUFFER_CONFIG * ybf)256 void aom_remove_metadata_from_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
257   if (ybf && ybf->metadata) {
258     aom_img_metadata_array_free(ybf->metadata);
259     ybf->metadata = NULL;
260   }
261 }
262 
aom_copy_metadata_to_frame_buffer(YV12_BUFFER_CONFIG * ybf,const aom_metadata_array_t * arr)263 int aom_copy_metadata_to_frame_buffer(YV12_BUFFER_CONFIG *ybf,
264                                       const aom_metadata_array_t *arr) {
265   if (!ybf || !arr || !arr->metadata_array) return -1;
266   if (ybf->metadata == arr) return 0;
267   aom_remove_metadata_from_frame_buffer(ybf);
268   ybf->metadata = aom_img_metadata_array_alloc(arr->sz);
269   if (!ybf->metadata) return -1;
270   for (size_t i = 0; i < ybf->metadata->sz; i++) {
271     ybf->metadata->metadata_array[i] = aom_img_metadata_alloc(
272         arr->metadata_array[i]->type, arr->metadata_array[i]->payload,
273         arr->metadata_array[i]->sz, arr->metadata_array[i]->insert_flag);
274     if (ybf->metadata->metadata_array[i] == NULL) {
275       aom_img_metadata_array_free(ybf->metadata);
276       ybf->metadata = NULL;
277       return -1;
278     }
279   }
280   ybf->metadata->sz = arr->sz;
281   return 0;
282 }
283