Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | |
| 14 | #include "aom_scale/yv12config.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 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 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 28 | #if CONFIG_AV1 |
| 29 | // TODO(jkoleszar): Maybe replace this with struct aom_image |
| 30 | |
| 31 | int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) { |
| 32 | if (ybf) { |
| 33 | if (ybf->buffer_alloc_sz > 0) { |
| 34 | aom_free(ybf->buffer_alloc); |
| 35 | } |
| 36 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 37 | #if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION |
David Barker | 557ce7b | 2016-11-16 10:22:24 +0000 | [diff] [blame] | 38 | if (ybf->y_buffer_8bit) free(ybf->y_buffer_8bit); |
| 39 | #endif |
| 40 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 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 | memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG)); |
| 45 | } else { |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 53 | int ss_x, int ss_y, |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 54 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 55 | int use_highbitdepth, |
| 56 | #endif |
| 57 | int border, int byte_alignment, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 58 | aom_codec_frame_buffer_t *fb, |
| 59 | aom_get_frame_buffer_cb_fn_t cb, void *cb_priv) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 60 | if (ybf) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 61 | const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 62 | const int aligned_width = (width + 7) & ~7; |
| 63 | const int aligned_height = (height + 7) & ~7; |
| 64 | const int y_stride = ((aligned_width + 2 * border) + 31) & ~31; |
| 65 | const uint64_t yplane_size = |
| 66 | (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment; |
| 67 | const int uv_width = aligned_width >> ss_x; |
| 68 | const int uv_height = aligned_height >> ss_y; |
| 69 | const int uv_stride = y_stride >> ss_x; |
| 70 | const int uv_border_w = border >> ss_x; |
| 71 | const int uv_border_h = border >> ss_y; |
| 72 | const uint64_t uvplane_size = |
| 73 | (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment; |
| 74 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 75 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | const uint64_t frame_size = |
| 77 | (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size); |
| 78 | #else |
| 79 | const uint64_t frame_size = yplane_size + 2 * uvplane_size; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 80 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 81 | |
| 82 | uint8_t *buf = NULL; |
| 83 | |
| 84 | if (cb != NULL) { |
| 85 | const int align_addr_extra_size = 31; |
| 86 | const uint64_t external_frame_size = frame_size + align_addr_extra_size; |
| 87 | |
| 88 | 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 | if (cb(cb_priv, (size_t)external_frame_size, fb) < 0) return -1; |
| 94 | |
| 95 | if (fb->data == NULL || fb->size < external_frame_size) return -1; |
| 96 | |
| 97 | 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 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 107 | } else if (frame_size > (size_t)ybf->buffer_alloc_sz) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 108 | // Allocation to hold larger frame, or first allocation. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 109 | aom_free(ybf->buffer_alloc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 110 | ybf->buffer_alloc = NULL; |
| 111 | |
| 112 | if (frame_size != (size_t)frame_size) return -1; |
| 113 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 114 | ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 115 | if (!ybf->buffer_alloc) return -1; |
| 116 | |
| 117 | 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 | 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 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 129 | * aom_img_set_rect(). */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 130 | if (border & 0x1f) return -3; |
| 131 | |
| 132 | ybf->y_crop_width = width; |
| 133 | ybf->y_crop_height = height; |
| 134 | ybf->y_width = aligned_width; |
| 135 | ybf->y_height = aligned_height; |
| 136 | ybf->y_stride = y_stride; |
| 137 | |
| 138 | ybf->uv_crop_width = (width + ss_x) >> ss_x; |
| 139 | ybf->uv_crop_height = (height + ss_y) >> ss_y; |
| 140 | ybf->uv_width = uv_width; |
| 141 | ybf->uv_height = uv_height; |
| 142 | ybf->uv_stride = uv_stride; |
| 143 | |
| 144 | ybf->border = border; |
| 145 | ybf->frame_size = (size_t)frame_size; |
| 146 | ybf->subsampling_x = ss_x; |
| 147 | ybf->subsampling_y = ss_y; |
| 148 | |
| 149 | buf = ybf->buffer_alloc; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 150 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 151 | if (use_highbitdepth) { |
| 152 | // Store uint16 addresses when using 16bit framebuffers |
| 153 | buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc); |
| 154 | ybf->flags = YV12_FLAG_HIGHBITDEPTH; |
| 155 | } else { |
| 156 | ybf->flags = 0; |
| 157 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 158 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 159 | |
| 160 | ybf->y_buffer = (uint8_t *)yv12_align_addr( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 161 | buf + (border * y_stride) + border, aom_byte_align); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 162 | ybf->u_buffer = (uint8_t *)yv12_align_addr( |
| 163 | buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 164 | aom_byte_align); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 165 | ybf->v_buffer = |
| 166 | (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size + |
| 167 | (uv_border_h * uv_stride) + uv_border_w, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 168 | aom_byte_align); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 169 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 170 | #if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION |
David Barker | 557ce7b | 2016-11-16 10:22:24 +0000 | [diff] [blame] | 171 | if (ybf->y_buffer_8bit) { |
| 172 | free(ybf->y_buffer_8bit); |
| 173 | ybf->y_buffer_8bit = NULL; |
| 174 | } |
| 175 | #endif |
| 176 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 177 | ybf->corrupted = 0; /* assume not corrupted by errors */ |
| 178 | return 0; |
| 179 | } |
| 180 | return -2; |
| 181 | } |
| 182 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 183 | int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 184 | int ss_x, int ss_y, |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 185 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 186 | int use_highbitdepth, |
| 187 | #endif |
| 188 | int border, int byte_alignment) { |
| 189 | if (ybf) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 190 | aom_free_frame_buffer(ybf); |
| 191 | return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y, |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 192 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 193 | use_highbitdepth, |
| 194 | #endif |
| 195 | border, byte_alignment, NULL, NULL, NULL); |
| 196 | } |
| 197 | return -2; |
| 198 | } |
| 199 | #endif |