blob: ee15ae103c392dfc1b9c799bd3dc34c8ae1393cd [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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 Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13
14#include "aom_scale/yv12config.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#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 Xuf883b422016-08-30 14:01:10 -070028#if CONFIG_AV1
29// TODO(jkoleszar): Maybe replace this with struct aom_image
30
31int 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 Alaiwan71e87842017-04-12 16:03:28 +020037#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
David Barker557ce7b2016-11-16 10:22:24 +000038 if (ybf->y_buffer_8bit) free(ybf->y_buffer_8bit);
39#endif
40
Yaowu Xuf883b422016-08-30 14:01:10 -070041 /* 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
52int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
Yaowu Xuc27fc142016-08-22 16:08:15 -070053 int ss_x, int ss_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020054#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070055 int use_highbitdepth,
56#endif
57 int border, int byte_alignment,
Yaowu Xuf883b422016-08-30 14:01:10 -070058 aom_codec_frame_buffer_t *fb,
59 aom_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070060 if (ybf) {
Yaowu Xuf883b422016-08-30 14:01:10 -070061 const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
Yaowu Xuc27fc142016-08-22 16:08:15 -070062 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 Alaiwan71e87842017-04-12 16:03:28 +020075#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070076 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 Alaiwan71e87842017-04-12 16:03:28 +020080#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070081
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 Xuf883b422016-08-30 14:01:10 -0700107 } else if (frame_size > (size_t)ybf->buffer_alloc_sz) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108 // Allocation to hold larger frame, or first allocation.
Yaowu Xuf883b422016-08-30 14:01:10 -0700109 aom_free(ybf->buffer_alloc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110 ybf->buffer_alloc = NULL;
111
112 if (frame_size != (size_t)frame_size) return -1;
113
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700115 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 Xuf883b422016-08-30 14:01:10 -0700129 * aom_img_set_rect(). */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130 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 Alaiwan71e87842017-04-12 16:03:28 +0200150#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 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 Alaiwan71e87842017-04-12 16:03:28 +0200158#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159
160 ybf->y_buffer = (uint8_t *)yv12_align_addr(
Yaowu Xuf883b422016-08-30 14:01:10 -0700161 buf + (border * y_stride) + border, aom_byte_align);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162 ybf->u_buffer = (uint8_t *)yv12_align_addr(
163 buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
Yaowu Xuf883b422016-08-30 14:01:10 -0700164 aom_byte_align);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 ybf->v_buffer =
166 (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
167 (uv_border_h * uv_stride) + uv_border_w,
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 aom_byte_align);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200170#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
David Barker557ce7b2016-11-16 10:22:24 +0000171 if (ybf->y_buffer_8bit) {
172 free(ybf->y_buffer_8bit);
173 ybf->y_buffer_8bit = NULL;
174 }
175#endif
176
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 ybf->corrupted = 0; /* assume not corrupted by errors */
178 return 0;
179 }
180 return -2;
181}
182
Yaowu Xuf883b422016-08-30 14:01:10 -0700183int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 int ss_x, int ss_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200185#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 int use_highbitdepth,
187#endif
188 int border, int byte_alignment) {
189 if (ybf) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700190 aom_free_frame_buffer(ybf);
191 return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200192#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 use_highbitdepth,
194#endif
195 border, byte_alignment, NULL, NULL, NULL);
196 }
197 return -2;
198}
199#endif