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