Line data Source code
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_scale/yv12config.h"
15 : #include "aom_mem/aom_mem.h"
16 : #include "aom_ports/mem.h"
17 :
18 : /****************************************************************************
19 : * Exports
20 : ****************************************************************************/
21 :
22 : /****************************************************************************
23 : *
24 : ****************************************************************************/
25 : #define yv12_align_addr(addr, align) \
26 : (void *)(((size_t)(addr) + ((align)-1)) & (size_t) - (align))
27 :
28 : #if CONFIG_AV1
29 : // TODO(jkoleszar): Maybe replace this with struct aom_image
30 :
31 0 : int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
32 0 : if (ybf) {
33 0 : if (ybf->buffer_alloc_sz > 0) {
34 0 : aom_free(ybf->buffer_alloc);
35 : }
36 :
37 : #if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
38 0 : if (ybf->y_buffer_8bit) free(ybf->y_buffer_8bit);
39 : #endif
40 :
41 : /* buffer_alloc isn't accessed by most functions. Rather y_buffer,
42 : u_buffer and v_buffer point to buffer_alloc and are used. Clear out
43 : all of this so that a freed pointer isn't inadvertently used */
44 0 : memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
45 : } else {
46 0 : return -1;
47 : }
48 :
49 0 : return 0;
50 : }
51 :
52 0 : int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
53 : int ss_x, int ss_y,
54 : #if CONFIG_HIGHBITDEPTH
55 : int use_highbitdepth,
56 : #endif
57 : int border, int byte_alignment,
58 : aom_codec_frame_buffer_t *fb,
59 : aom_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
60 0 : if (ybf) {
61 0 : const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
62 0 : const int aligned_width = (width + 7) & ~7;
63 0 : const int aligned_height = (height + 7) & ~7;
64 0 : const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
65 0 : const uint64_t yplane_size =
66 0 : (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment;
67 0 : const int uv_width = aligned_width >> ss_x;
68 0 : const int uv_height = aligned_height >> ss_y;
69 0 : const int uv_stride = y_stride >> ss_x;
70 0 : const int uv_border_w = border >> ss_x;
71 0 : const int uv_border_h = border >> ss_y;
72 0 : const uint64_t uvplane_size =
73 0 : (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment;
74 :
75 : #if CONFIG_HIGHBITDEPTH
76 0 : const uint64_t frame_size =
77 0 : (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
78 : #else
79 : const uint64_t frame_size = yplane_size + 2 * uvplane_size;
80 : #endif // CONFIG_HIGHBITDEPTH
81 :
82 0 : uint8_t *buf = NULL;
83 :
84 0 : if (cb != NULL) {
85 0 : const int align_addr_extra_size = 31;
86 0 : const uint64_t external_frame_size = frame_size + align_addr_extra_size;
87 :
88 0 : assert(fb != NULL);
89 :
90 : if (external_frame_size != (size_t)external_frame_size) return -1;
91 :
92 : // Allocation to hold larger frame, or first allocation.
93 0 : if (cb(cb_priv, (size_t)external_frame_size, fb) < 0) return -1;
94 :
95 0 : if (fb->data == NULL || fb->size < external_frame_size) return -1;
96 :
97 0 : ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
98 :
99 : #if defined(__has_feature)
100 : #if __has_feature(memory_sanitizer)
101 : // This memset is needed for fixing the issue of using uninitialized
102 : // value in msan test. It will cause a perf loss, so only do this for
103 : // msan test.
104 : memset(ybf->buffer_alloc, 0, (int)frame_size);
105 : #endif
106 : #endif
107 0 : } else if (frame_size > (size_t)ybf->buffer_alloc_sz) {
108 : // Allocation to hold larger frame, or first allocation.
109 0 : aom_free(ybf->buffer_alloc);
110 0 : ybf->buffer_alloc = NULL;
111 :
112 : if (frame_size != (size_t)frame_size) return -1;
113 :
114 0 : ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size);
115 0 : if (!ybf->buffer_alloc) return -1;
116 :
117 0 : ybf->buffer_alloc_sz = (size_t)frame_size;
118 :
119 : // This memset is needed for fixing valgrind error from C loop filter
120 : // due to access uninitialized memory in frame border. It could be
121 : // removed if border is totally removed.
122 0 : memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
123 : }
124 :
125 : /* Only support allocating buffers that have a border that's a multiple
126 : * of 32. The border restriction is required to get 16-byte alignment of
127 : * the start of the chroma rows without introducing an arbitrary gap
128 : * between planes, which would break the semantics of things like
129 : * aom_img_set_rect(). */
130 0 : if (border & 0x1f) return -3;
131 :
132 0 : ybf->y_crop_width = width;
133 0 : ybf->y_crop_height = height;
134 0 : ybf->y_width = aligned_width;
135 0 : ybf->y_height = aligned_height;
136 0 : ybf->y_stride = y_stride;
137 :
138 0 : ybf->uv_crop_width = (width + ss_x) >> ss_x;
139 0 : ybf->uv_crop_height = (height + ss_y) >> ss_y;
140 0 : ybf->uv_width = uv_width;
141 0 : ybf->uv_height = uv_height;
142 0 : ybf->uv_stride = uv_stride;
143 :
144 0 : ybf->border = border;
145 0 : ybf->frame_size = (size_t)frame_size;
146 0 : ybf->subsampling_x = ss_x;
147 0 : ybf->subsampling_y = ss_y;
148 :
149 0 : buf = ybf->buffer_alloc;
150 : #if CONFIG_HIGHBITDEPTH
151 0 : if (use_highbitdepth) {
152 : // Store uint16 addresses when using 16bit framebuffers
153 0 : buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
154 0 : ybf->flags = YV12_FLAG_HIGHBITDEPTH;
155 : } else {
156 0 : ybf->flags = 0;
157 : }
158 : #endif // CONFIG_HIGHBITDEPTH
159 :
160 0 : ybf->y_buffer = (uint8_t *)yv12_align_addr(
161 : buf + (border * y_stride) + border, aom_byte_align);
162 0 : ybf->u_buffer = (uint8_t *)yv12_align_addr(
163 : buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
164 : aom_byte_align);
165 0 : ybf->v_buffer =
166 0 : (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
167 : (uv_border_h * uv_stride) + uv_border_w,
168 : aom_byte_align);
169 :
170 : #if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
171 0 : if (ybf->y_buffer_8bit) {
172 0 : free(ybf->y_buffer_8bit);
173 0 : ybf->y_buffer_8bit = NULL;
174 : }
175 : #endif
176 :
177 0 : ybf->corrupted = 0; /* assume not corrupted by errors */
178 0 : return 0;
179 : }
180 0 : return -2;
181 : }
182 :
183 0 : int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
184 : int ss_x, int ss_y,
185 : #if CONFIG_HIGHBITDEPTH
186 : int use_highbitdepth,
187 : #endif
188 : int border, int byte_alignment) {
189 0 : if (ybf) {
190 0 : aom_free_frame_buffer(ybf);
191 0 : return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
192 : #if CONFIG_HIGHBITDEPTH
193 : use_highbitdepth,
194 : #endif
195 : border, byte_alignment, NULL, NULL, NULL);
196 : }
197 0 : return -2;
198 : }
199 : #endif
|