• 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)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   if (ybf) {
55     const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
56     const uint64_t frame_size =
57         (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
58 
59     uint8_t *buf = NULL;
60 
61 #if defined AOM_MAX_ALLOCABLE_MEMORY
62     // The size of ybf->buffer_alloc.
63     uint64_t alloc_size = frame_size;
64     // The size of ybf->y_buffer_8bit.
65     if (use_highbitdepth) alloc_size += yplane_size;
66     // The decoder may allocate REF_FRAMES frame buffers in the frame buffer
67     // pool. Bound the total amount of allocated memory as if these REF_FRAMES
68     // frame buffers were allocated in a single allocation.
69     if (alloc_size > AOM_MAX_ALLOCABLE_MEMORY / REF_FRAMES)
70       return AOM_CODEC_MEM_ERROR;
71 #endif
72 
73     if (cb != NULL) {
74       const int align_addr_extra_size = 31;
75       const uint64_t external_frame_size = frame_size + align_addr_extra_size;
76 
77       assert(fb != NULL);
78 
79       if (external_frame_size != (size_t)external_frame_size)
80         return AOM_CODEC_MEM_ERROR;
81 
82       // Allocation to hold larger frame, or first allocation.
83       if (cb(cb_priv, (size_t)external_frame_size, fb) < 0)
84         return AOM_CODEC_MEM_ERROR;
85 
86       if (fb->data == NULL || fb->size < external_frame_size)
87         return AOM_CODEC_MEM_ERROR;
88 
89       ybf->buffer_alloc = (uint8_t *)aom_align_addr(fb->data, 32);
90 
91 #if defined(__has_feature)
92 #if __has_feature(memory_sanitizer)
93       // This memset is needed for fixing the issue of using uninitialized
94       // value in msan test. It will cause a perf loss, so only do this for
95       // msan test.
96       memset(ybf->buffer_alloc, 0, (size_t)frame_size);
97 #endif
98 #endif
99     } else if (frame_size > ybf->buffer_alloc_sz) {
100       // Allocation to hold larger frame, or first allocation.
101       aom_free(ybf->buffer_alloc);
102       ybf->buffer_alloc = NULL;
103       ybf->buffer_alloc_sz = 0;
104 
105       if (frame_size != (size_t)frame_size) return AOM_CODEC_MEM_ERROR;
106 
107       ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size);
108       if (!ybf->buffer_alloc) return AOM_CODEC_MEM_ERROR;
109 
110       ybf->buffer_alloc_sz = (size_t)frame_size;
111 
112       // This memset is needed for fixing valgrind error from C loop filter
113       // due to access uninitialized memory in frame border. It could be
114       // removed if border is totally removed.
115       memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
116     }
117 
118     ybf->y_crop_width = width;
119     ybf->y_crop_height = height;
120     ybf->y_width = aligned_width;
121     ybf->y_height = aligned_height;
122     ybf->y_stride = y_stride;
123 
124     ybf->uv_crop_width = (width + ss_x) >> ss_x;
125     ybf->uv_crop_height = (height + ss_y) >> ss_y;
126     ybf->uv_width = uv_width;
127     ybf->uv_height = uv_height;
128     ybf->uv_stride = uv_stride;
129 
130     ybf->border = border;
131     ybf->frame_size = (size_t)frame_size;
132     ybf->subsampling_x = ss_x;
133     ybf->subsampling_y = ss_y;
134 
135     buf = ybf->buffer_alloc;
136     if (use_highbitdepth) {
137       // Store uint16 addresses when using 16bit framebuffers
138       buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
139       ybf->flags = YV12_FLAG_HIGHBITDEPTH;
140     } else {
141       ybf->flags = 0;
142     }
143 
144     ybf->y_buffer = (uint8_t *)aom_align_addr(
145         buf + (border * y_stride) + border, aom_byte_align);
146     ybf->u_buffer = (uint8_t *)aom_align_addr(
147         buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
148         aom_byte_align);
149     ybf->v_buffer =
150         (uint8_t *)aom_align_addr(buf + yplane_size + uvplane_size +
151                                       (uv_border_h * uv_stride) + uv_border_w,
152                                   aom_byte_align);
153 
154     ybf->use_external_reference_buffers = 0;
155 
156     if (use_highbitdepth) {
157       if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
158       ybf->y_buffer_8bit = (uint8_t *)aom_memalign(32, (size_t)yplane_size);
159       if (!ybf->y_buffer_8bit) return AOM_CODEC_MEM_ERROR;
160     } else {
161       if (ybf->y_buffer_8bit) {
162         aom_free(ybf->y_buffer_8bit);
163         ybf->y_buffer_8bit = NULL;
164         ybf->buf_8bit_valid = 0;
165       }
166     }
167 
168     ybf->corrupted = 0; /* assume not corrupted by errors */
169     return 0;
170   }
171   return AOM_CODEC_MEM_ERROR;
172 }
173 
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 * y_stride,int * uv_stride,uint64_t * yplane_size,uint64_t * uvplane_size,const int uv_height)174 static int calc_stride_and_planesize(const int ss_x, const int ss_y,
175                                      const int aligned_width,
176                                      const int aligned_height, const int border,
177                                      const int byte_alignment, int *y_stride,
178                                      int *uv_stride, uint64_t *yplane_size,
179                                      uint64_t *uvplane_size,
180                                      const int uv_height) {
181   /* Only support allocating buffers that have a border that's a multiple
182    * of 32. The border restriction is required to get 16-byte alignment of
183    * the start of the chroma rows without introducing an arbitrary gap
184    * between planes, which would break the semantics of things like
185    * aom_img_set_rect(). */
186   if (border & 0x1f) return AOM_CODEC_MEM_ERROR;
187   *y_stride = ((aligned_width + 2 * border) + 31) & ~31;
188   *yplane_size =
189       (aligned_height + 2 * border) * (uint64_t)(*y_stride) + byte_alignment;
190 
191   *uv_stride = *y_stride >> ss_x;
192   *uvplane_size = (uv_height + 2 * (border >> ss_y)) * (uint64_t)(*uv_stride) +
193                   byte_alignment;
194   return 0;
195 }
196 
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)197 int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
198                              int ss_x, int ss_y, int use_highbitdepth,
199                              int border, int byte_alignment,
200                              aom_codec_frame_buffer_t *fb,
201                              aom_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
202 #if CONFIG_SIZE_LIMIT
203   if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
204     return AOM_CODEC_MEM_ERROR;
205 #endif
206 
207   if (ybf) {
208     int y_stride = 0;
209     int uv_stride = 0;
210     uint64_t yplane_size = 0;
211     uint64_t uvplane_size = 0;
212     const int aligned_width = (width + 7) & ~7;
213     const int aligned_height = (height + 7) & ~7;
214     const int uv_width = aligned_width >> ss_x;
215     const int uv_height = aligned_height >> ss_y;
216     const int uv_border_w = border >> ss_x;
217     const int uv_border_h = border >> ss_y;
218 
219     int error = calc_stride_and_planesize(
220         ss_x, ss_y, aligned_width, aligned_height, border, byte_alignment,
221         &y_stride, &uv_stride, &yplane_size, &uvplane_size, uv_height);
222     if (error) return error;
223     return realloc_frame_buffer_aligned(
224         ybf, width, height, ss_x, ss_y, use_highbitdepth, border,
225         byte_alignment, fb, cb, cb_priv, y_stride, yplane_size, uvplane_size,
226         aligned_width, aligned_height, uv_width, uv_height, uv_stride,
227         uv_border_w, uv_border_h);
228   }
229   return AOM_CODEC_MEM_ERROR;
230 }
231 
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)232 int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
233                            int ss_x, int ss_y, int use_highbitdepth, int border,
234                            int byte_alignment) {
235   if (ybf) {
236     aom_free_frame_buffer(ybf);
237     return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
238                                     use_highbitdepth, border, byte_alignment,
239                                     NULL, NULL, NULL);
240   }
241   return AOM_CODEC_MEM_ERROR;
242 }
243 
aom_remove_metadata_from_frame_buffer(YV12_BUFFER_CONFIG * ybf)244 void aom_remove_metadata_from_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
245   if (ybf && ybf->metadata) {
246     aom_img_metadata_array_free(ybf->metadata);
247     ybf->metadata = NULL;
248   }
249 }
250 
aom_copy_metadata_to_frame_buffer(YV12_BUFFER_CONFIG * ybf,const aom_metadata_array_t * arr)251 int aom_copy_metadata_to_frame_buffer(YV12_BUFFER_CONFIG *ybf,
252                                       const aom_metadata_array_t *arr) {
253   if (!ybf || !arr || !arr->metadata_array) return -1;
254   aom_remove_metadata_from_frame_buffer(ybf);
255   ybf->metadata = aom_img_metadata_array_alloc(arr->sz);
256   if (!ybf->metadata) return -1;
257   for (size_t i = 0; i < ybf->metadata->sz; i++) {
258     ybf->metadata->metadata_array[i] = aom_img_metadata_alloc(
259         arr->metadata_array[i]->type, arr->metadata_array[i]->payload,
260         arr->metadata_array[i]->sz, arr->metadata_array[i]->insert_flag);
261     if (ybf->metadata->metadata_array[i] == NULL) {
262       aom_img_metadata_array_free(ybf->metadata);
263       ybf->metadata = NULL;
264       return -1;
265     }
266   }
267   ybf->metadata->sz = arr->sz;
268   return 0;
269 }
270