xref: /aosp_15_r20/external/libaom/aom_dsp/pyramid.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2022, 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 "aom_dsp/pyramid.h"
13*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
14*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/bitops.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_pthread.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker // TODO(rachelbarker): Move needed code from av1/ to aom_dsp/
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/resize.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
21*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
22*77c1e3ccSAndroid Build Coastguard Worker 
23*77c1e3ccSAndroid Build Coastguard Worker // Lifecycle:
24*77c1e3ccSAndroid Build Coastguard Worker // * Frame buffer alloc code calls aom_get_pyramid_alloc_size()
25*77c1e3ccSAndroid Build Coastguard Worker //   to work out how much space is needed for a given number of pyramid
26*77c1e3ccSAndroid Build Coastguard Worker //   levels. This is counted in the size checked against the max allocation
27*77c1e3ccSAndroid Build Coastguard Worker //   limit
28*77c1e3ccSAndroid Build Coastguard Worker // * Then calls aom_alloc_pyramid() to actually create the pyramid
29*77c1e3ccSAndroid Build Coastguard Worker // * Pyramid is initially marked as containing no valid data
30*77c1e3ccSAndroid Build Coastguard Worker // * Each pyramid layer is computed on-demand, the first time it is requested
31*77c1e3ccSAndroid Build Coastguard Worker // * Whenever frame buffer is reused, reset the counter of filled levels.
32*77c1e3ccSAndroid Build Coastguard Worker //   This invalidates all of the existing pyramid levels.
33*77c1e3ccSAndroid Build Coastguard Worker // * Whenever frame buffer is resized, reallocate pyramid
34*77c1e3ccSAndroid Build Coastguard Worker 
aom_get_pyramid_alloc_size(int width,int height,bool image_is_16bit)35*77c1e3ccSAndroid Build Coastguard Worker size_t aom_get_pyramid_alloc_size(int width, int height, bool image_is_16bit) {
36*77c1e3ccSAndroid Build Coastguard Worker   // Allocate the maximum possible number of layers for this width and height
37*77c1e3ccSAndroid Build Coastguard Worker   const int msb = get_msb(AOMMIN(width, height));
38*77c1e3ccSAndroid Build Coastguard Worker   const int n_levels = AOMMAX(msb - MIN_PYRAMID_SIZE_LOG2, 1);
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker   size_t alloc_size = 0;
41*77c1e3ccSAndroid Build Coastguard Worker   alloc_size += sizeof(ImagePyramid);
42*77c1e3ccSAndroid Build Coastguard Worker   alloc_size += n_levels * sizeof(PyramidLayer);
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker   // Calculate how much memory is needed for downscaled frame buffers
45*77c1e3ccSAndroid Build Coastguard Worker   size_t buffer_size = 0;
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker   // Work out if we need to allocate a few extra bytes for alignment.
48*77c1e3ccSAndroid Build Coastguard Worker   // aom_memalign() will ensure that the start of the allocation is aligned
49*77c1e3ccSAndroid Build Coastguard Worker   // to a multiple of PYRAMID_ALIGNMENT. But we want the first image pixel
50*77c1e3ccSAndroid Build Coastguard Worker   // to be aligned, not the first byte of the allocation.
51*77c1e3ccSAndroid Build Coastguard Worker   //
52*77c1e3ccSAndroid Build Coastguard Worker   // In the loop below, we ensure that the stride of every image is a multiple
53*77c1e3ccSAndroid Build Coastguard Worker   // of PYRAMID_ALIGNMENT. Thus the allocated size of each pyramid level will
54*77c1e3ccSAndroid Build Coastguard Worker   // also be a multiple of PYRAMID_ALIGNMENT. Thus, as long as we can get the
55*77c1e3ccSAndroid Build Coastguard Worker   // first pixel in the first pyramid layer aligned properly, that will
56*77c1e3ccSAndroid Build Coastguard Worker   // automatically mean that the first pixel of every row of every layer is
57*77c1e3ccSAndroid Build Coastguard Worker   // properly aligned too.
58*77c1e3ccSAndroid Build Coastguard Worker   //
59*77c1e3ccSAndroid Build Coastguard Worker   // Thus all we need to consider is the first pixel in the first layer.
60*77c1e3ccSAndroid Build Coastguard Worker   // This is located at offset
61*77c1e3ccSAndroid Build Coastguard Worker   //   extra_bytes + level_stride * PYRAMID_PADDING + PYRAMID_PADDING
62*77c1e3ccSAndroid Build Coastguard Worker   // bytes into the buffer. Since level_stride is a multiple of
63*77c1e3ccSAndroid Build Coastguard Worker   // PYRAMID_ALIGNMENT, we can ignore that. So we need
64*77c1e3ccSAndroid Build Coastguard Worker   //   extra_bytes + PYRAMID_PADDING = multiple of PYRAMID_ALIGNMENT
65*77c1e3ccSAndroid Build Coastguard Worker   //
66*77c1e3ccSAndroid Build Coastguard Worker   // To solve this, we can round PYRAMID_PADDING up to the next multiple
67*77c1e3ccSAndroid Build Coastguard Worker   // of PYRAMID_ALIGNMENT, then subtract the orginal value to calculate
68*77c1e3ccSAndroid Build Coastguard Worker   // how many extra bytes are needed.
69*77c1e3ccSAndroid Build Coastguard Worker   size_t first_px_offset =
70*77c1e3ccSAndroid Build Coastguard Worker       (PYRAMID_PADDING + PYRAMID_ALIGNMENT - 1) & ~(PYRAMID_ALIGNMENT - 1);
71*77c1e3ccSAndroid Build Coastguard Worker   size_t extra_bytes = first_px_offset - PYRAMID_PADDING;
72*77c1e3ccSAndroid Build Coastguard Worker   buffer_size += extra_bytes;
73*77c1e3ccSAndroid Build Coastguard Worker 
74*77c1e3ccSAndroid Build Coastguard Worker   // If the original image is stored in an 8-bit buffer, then we can point the
75*77c1e3ccSAndroid Build Coastguard Worker   // lowest pyramid level at that buffer rather than allocating a new one.
76*77c1e3ccSAndroid Build Coastguard Worker   int first_allocated_level = image_is_16bit ? 0 : 1;
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker   for (int level = first_allocated_level; level < n_levels; level++) {
79*77c1e3ccSAndroid Build Coastguard Worker     int level_width = width >> level;
80*77c1e3ccSAndroid Build Coastguard Worker     int level_height = height >> level;
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker     // Allocate padding for each layer
83*77c1e3ccSAndroid Build Coastguard Worker     int padded_width = level_width + 2 * PYRAMID_PADDING;
84*77c1e3ccSAndroid Build Coastguard Worker     int padded_height = level_height + 2 * PYRAMID_PADDING;
85*77c1e3ccSAndroid Build Coastguard Worker 
86*77c1e3ccSAndroid Build Coastguard Worker     // Align the layer stride to be a multiple of PYRAMID_ALIGNMENT
87*77c1e3ccSAndroid Build Coastguard Worker     // This ensures that, as long as the top-left pixel in this pyramid level is
88*77c1e3ccSAndroid Build Coastguard Worker     // properly aligned, then so will the leftmost pixel in every row of the
89*77c1e3ccSAndroid Build Coastguard Worker     // pyramid level.
90*77c1e3ccSAndroid Build Coastguard Worker     int level_stride =
91*77c1e3ccSAndroid Build Coastguard Worker         (padded_width + PYRAMID_ALIGNMENT - 1) & ~(PYRAMID_ALIGNMENT - 1);
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker     buffer_size += level_stride * padded_height;
94*77c1e3ccSAndroid Build Coastguard Worker   }
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker   alloc_size += buffer_size;
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker   return alloc_size;
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker 
aom_alloc_pyramid(int width,int height,bool image_is_16bit)101*77c1e3ccSAndroid Build Coastguard Worker ImagePyramid *aom_alloc_pyramid(int width, int height, bool image_is_16bit) {
102*77c1e3ccSAndroid Build Coastguard Worker   // Allocate the maximum possible number of layers for this width and height
103*77c1e3ccSAndroid Build Coastguard Worker   const int msb = get_msb(AOMMIN(width, height));
104*77c1e3ccSAndroid Build Coastguard Worker   const int n_levels = AOMMAX(msb - MIN_PYRAMID_SIZE_LOG2, 1);
105*77c1e3ccSAndroid Build Coastguard Worker 
106*77c1e3ccSAndroid Build Coastguard Worker   ImagePyramid *pyr = aom_calloc(1, sizeof(*pyr));
107*77c1e3ccSAndroid Build Coastguard Worker   if (!pyr) {
108*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
109*77c1e3ccSAndroid Build Coastguard Worker   }
110*77c1e3ccSAndroid Build Coastguard Worker 
111*77c1e3ccSAndroid Build Coastguard Worker   pyr->layers = aom_calloc(n_levels, sizeof(*pyr->layers));
112*77c1e3ccSAndroid Build Coastguard Worker   if (!pyr->layers) {
113*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr);
114*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
115*77c1e3ccSAndroid Build Coastguard Worker   }
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker   pyr->max_levels = n_levels;
118*77c1e3ccSAndroid Build Coastguard Worker   pyr->filled_levels = 0;
119*77c1e3ccSAndroid Build Coastguard Worker 
120*77c1e3ccSAndroid Build Coastguard Worker   // Compute sizes and offsets for each pyramid level
121*77c1e3ccSAndroid Build Coastguard Worker   // These are gathered up first, so that we can allocate all pyramid levels
122*77c1e3ccSAndroid Build Coastguard Worker   // in a single buffer
123*77c1e3ccSAndroid Build Coastguard Worker   size_t buffer_size = 0;
124*77c1e3ccSAndroid Build Coastguard Worker   size_t *layer_offsets = aom_calloc(n_levels, sizeof(*layer_offsets));
125*77c1e3ccSAndroid Build Coastguard Worker   if (!layer_offsets) {
126*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr->layers);
127*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr);
128*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
129*77c1e3ccSAndroid Build Coastguard Worker   }
130*77c1e3ccSAndroid Build Coastguard Worker 
131*77c1e3ccSAndroid Build Coastguard Worker   // Work out if we need to allocate a few extra bytes for alignment.
132*77c1e3ccSAndroid Build Coastguard Worker   // aom_memalign() will ensure that the start of the allocation is aligned
133*77c1e3ccSAndroid Build Coastguard Worker   // to a multiple of PYRAMID_ALIGNMENT. But we want the first image pixel
134*77c1e3ccSAndroid Build Coastguard Worker   // to be aligned, not the first byte of the allocation.
135*77c1e3ccSAndroid Build Coastguard Worker   //
136*77c1e3ccSAndroid Build Coastguard Worker   // In the loop below, we ensure that the stride of every image is a multiple
137*77c1e3ccSAndroid Build Coastguard Worker   // of PYRAMID_ALIGNMENT. Thus the allocated size of each pyramid level will
138*77c1e3ccSAndroid Build Coastguard Worker   // also be a multiple of PYRAMID_ALIGNMENT. Thus, as long as we can get the
139*77c1e3ccSAndroid Build Coastguard Worker   // first pixel in the first pyramid layer aligned properly, that will
140*77c1e3ccSAndroid Build Coastguard Worker   // automatically mean that the first pixel of every row of every layer is
141*77c1e3ccSAndroid Build Coastguard Worker   // properly aligned too.
142*77c1e3ccSAndroid Build Coastguard Worker   //
143*77c1e3ccSAndroid Build Coastguard Worker   // Thus all we need to consider is the first pixel in the first layer.
144*77c1e3ccSAndroid Build Coastguard Worker   // This is located at offset
145*77c1e3ccSAndroid Build Coastguard Worker   //   extra_bytes + level_stride * PYRAMID_PADDING + PYRAMID_PADDING
146*77c1e3ccSAndroid Build Coastguard Worker   // bytes into the buffer. Since level_stride is a multiple of
147*77c1e3ccSAndroid Build Coastguard Worker   // PYRAMID_ALIGNMENT, we can ignore that. So we need
148*77c1e3ccSAndroid Build Coastguard Worker   //   extra_bytes + PYRAMID_PADDING = multiple of PYRAMID_ALIGNMENT
149*77c1e3ccSAndroid Build Coastguard Worker   //
150*77c1e3ccSAndroid Build Coastguard Worker   // To solve this, we can round PYRAMID_PADDING up to the next multiple
151*77c1e3ccSAndroid Build Coastguard Worker   // of PYRAMID_ALIGNMENT, then subtract the orginal value to calculate
152*77c1e3ccSAndroid Build Coastguard Worker   // how many extra bytes are needed.
153*77c1e3ccSAndroid Build Coastguard Worker   size_t first_px_offset =
154*77c1e3ccSAndroid Build Coastguard Worker       (PYRAMID_PADDING + PYRAMID_ALIGNMENT - 1) & ~(PYRAMID_ALIGNMENT - 1);
155*77c1e3ccSAndroid Build Coastguard Worker   size_t extra_bytes = first_px_offset - PYRAMID_PADDING;
156*77c1e3ccSAndroid Build Coastguard Worker   buffer_size += extra_bytes;
157*77c1e3ccSAndroid Build Coastguard Worker 
158*77c1e3ccSAndroid Build Coastguard Worker   // If the original image is stored in an 8-bit buffer, then we can point the
159*77c1e3ccSAndroid Build Coastguard Worker   // lowest pyramid level at that buffer rather than allocating a new one.
160*77c1e3ccSAndroid Build Coastguard Worker   int first_allocated_level = image_is_16bit ? 0 : 1;
161*77c1e3ccSAndroid Build Coastguard Worker 
162*77c1e3ccSAndroid Build Coastguard Worker   for (int level = first_allocated_level; level < n_levels; level++) {
163*77c1e3ccSAndroid Build Coastguard Worker     PyramidLayer *layer = &pyr->layers[level];
164*77c1e3ccSAndroid Build Coastguard Worker 
165*77c1e3ccSAndroid Build Coastguard Worker     int level_width = width >> level;
166*77c1e3ccSAndroid Build Coastguard Worker     int level_height = height >> level;
167*77c1e3ccSAndroid Build Coastguard Worker 
168*77c1e3ccSAndroid Build Coastguard Worker     // Allocate padding for each layer
169*77c1e3ccSAndroid Build Coastguard Worker     int padded_width = level_width + 2 * PYRAMID_PADDING;
170*77c1e3ccSAndroid Build Coastguard Worker     int padded_height = level_height + 2 * PYRAMID_PADDING;
171*77c1e3ccSAndroid Build Coastguard Worker 
172*77c1e3ccSAndroid Build Coastguard Worker     // Align the layer stride to be a multiple of PYRAMID_ALIGNMENT
173*77c1e3ccSAndroid Build Coastguard Worker     // This ensures that, as long as the top-left pixel in this pyramid level is
174*77c1e3ccSAndroid Build Coastguard Worker     // properly aligned, then so will the leftmost pixel in every row of the
175*77c1e3ccSAndroid Build Coastguard Worker     // pyramid level.
176*77c1e3ccSAndroid Build Coastguard Worker     int level_stride =
177*77c1e3ccSAndroid Build Coastguard Worker         (padded_width + PYRAMID_ALIGNMENT - 1) & ~(PYRAMID_ALIGNMENT - 1);
178*77c1e3ccSAndroid Build Coastguard Worker 
179*77c1e3ccSAndroid Build Coastguard Worker     size_t level_alloc_start = buffer_size;
180*77c1e3ccSAndroid Build Coastguard Worker     size_t level_start =
181*77c1e3ccSAndroid Build Coastguard Worker         level_alloc_start + PYRAMID_PADDING * level_stride + PYRAMID_PADDING;
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker     buffer_size += level_stride * padded_height;
184*77c1e3ccSAndroid Build Coastguard Worker 
185*77c1e3ccSAndroid Build Coastguard Worker     layer_offsets[level] = level_start;
186*77c1e3ccSAndroid Build Coastguard Worker     layer->width = level_width;
187*77c1e3ccSAndroid Build Coastguard Worker     layer->height = level_height;
188*77c1e3ccSAndroid Build Coastguard Worker     layer->stride = level_stride;
189*77c1e3ccSAndroid Build Coastguard Worker   }
190*77c1e3ccSAndroid Build Coastguard Worker 
191*77c1e3ccSAndroid Build Coastguard Worker   pyr->buffer_alloc =
192*77c1e3ccSAndroid Build Coastguard Worker       aom_memalign(PYRAMID_ALIGNMENT, buffer_size * sizeof(*pyr->buffer_alloc));
193*77c1e3ccSAndroid Build Coastguard Worker   if (!pyr->buffer_alloc) {
194*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr->layers);
195*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr);
196*77c1e3ccSAndroid Build Coastguard Worker     aom_free(layer_offsets);
197*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
198*77c1e3ccSAndroid Build Coastguard Worker   }
199*77c1e3ccSAndroid Build Coastguard Worker 
200*77c1e3ccSAndroid Build Coastguard Worker   // Fill in pointers for each level
201*77c1e3ccSAndroid Build Coastguard Worker   // If image is 8-bit, then the lowest level is left unconfigured for now,
202*77c1e3ccSAndroid Build Coastguard Worker   // and will be set up properly when the pyramid is filled in
203*77c1e3ccSAndroid Build Coastguard Worker   for (int level = first_allocated_level; level < n_levels; level++) {
204*77c1e3ccSAndroid Build Coastguard Worker     PyramidLayer *layer = &pyr->layers[level];
205*77c1e3ccSAndroid Build Coastguard Worker     layer->buffer = pyr->buffer_alloc + layer_offsets[level];
206*77c1e3ccSAndroid Build Coastguard Worker   }
207*77c1e3ccSAndroid Build Coastguard Worker 
208*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
209*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_init(&pyr->mutex, NULL);
210*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
211*77c1e3ccSAndroid Build Coastguard Worker 
212*77c1e3ccSAndroid Build Coastguard Worker   aom_free(layer_offsets);
213*77c1e3ccSAndroid Build Coastguard Worker   return pyr;
214*77c1e3ccSAndroid Build Coastguard Worker }
215*77c1e3ccSAndroid Build Coastguard Worker 
216*77c1e3ccSAndroid Build Coastguard Worker // Fill the border region of a pyramid frame.
217*77c1e3ccSAndroid Build Coastguard Worker // This must be called after the main image area is filled out.
218*77c1e3ccSAndroid Build Coastguard Worker // `img_buf` should point to the first pixel in the image area,
219*77c1e3ccSAndroid Build Coastguard Worker // ie. it should be pyr->level_buffer + pyr->level_loc[level].
fill_border(uint8_t * img_buf,const int width,const int height,const int stride)220*77c1e3ccSAndroid Build Coastguard Worker static inline void fill_border(uint8_t *img_buf, const int width,
221*77c1e3ccSAndroid Build Coastguard Worker                                const int height, const int stride) {
222*77c1e3ccSAndroid Build Coastguard Worker   // Fill left and right areas
223*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < height; row++) {
224*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *row_start = &img_buf[row * stride];
225*77c1e3ccSAndroid Build Coastguard Worker     uint8_t left_pixel = row_start[0];
226*77c1e3ccSAndroid Build Coastguard Worker     memset(row_start - PYRAMID_PADDING, left_pixel, PYRAMID_PADDING);
227*77c1e3ccSAndroid Build Coastguard Worker     uint8_t right_pixel = row_start[width - 1];
228*77c1e3ccSAndroid Build Coastguard Worker     memset(row_start + width, right_pixel, PYRAMID_PADDING);
229*77c1e3ccSAndroid Build Coastguard Worker   }
230*77c1e3ccSAndroid Build Coastguard Worker 
231*77c1e3ccSAndroid Build Coastguard Worker   // Fill top area
232*77c1e3ccSAndroid Build Coastguard Worker   for (int row = -PYRAMID_PADDING; row < 0; row++) {
233*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *row_start = &img_buf[row * stride];
234*77c1e3ccSAndroid Build Coastguard Worker     memcpy(row_start - PYRAMID_PADDING, img_buf - PYRAMID_PADDING,
235*77c1e3ccSAndroid Build Coastguard Worker            width + 2 * PYRAMID_PADDING);
236*77c1e3ccSAndroid Build Coastguard Worker   }
237*77c1e3ccSAndroid Build Coastguard Worker 
238*77c1e3ccSAndroid Build Coastguard Worker   // Fill bottom area
239*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *last_row_start = &img_buf[(height - 1) * stride];
240*77c1e3ccSAndroid Build Coastguard Worker   for (int row = height; row < height + PYRAMID_PADDING; row++) {
241*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *row_start = &img_buf[row * stride];
242*77c1e3ccSAndroid Build Coastguard Worker     memcpy(row_start - PYRAMID_PADDING, last_row_start - PYRAMID_PADDING,
243*77c1e3ccSAndroid Build Coastguard Worker            width + 2 * PYRAMID_PADDING);
244*77c1e3ccSAndroid Build Coastguard Worker   }
245*77c1e3ccSAndroid Build Coastguard Worker }
246*77c1e3ccSAndroid Build Coastguard Worker 
247*77c1e3ccSAndroid Build Coastguard Worker // Compute downsampling pyramid for a frame
248*77c1e3ccSAndroid Build Coastguard Worker //
249*77c1e3ccSAndroid Build Coastguard Worker // This function will ensure that the first `n_levels` levels of the pyramid
250*77c1e3ccSAndroid Build Coastguard Worker // are filled, unless the frame is too small to have this many levels.
251*77c1e3ccSAndroid Build Coastguard Worker // In that case, we will fill all available levels and then stop.
252*77c1e3ccSAndroid Build Coastguard Worker //
253*77c1e3ccSAndroid Build Coastguard Worker // Returns the actual number of levels filled, capped at n_levels,
254*77c1e3ccSAndroid Build Coastguard Worker // or -1 on error.
255*77c1e3ccSAndroid Build Coastguard Worker //
256*77c1e3ccSAndroid Build Coastguard Worker // This must only be called while holding frame_pyr->mutex
fill_pyramid(const YV12_BUFFER_CONFIG * frame,int bit_depth,int n_levels,ImagePyramid * frame_pyr)257*77c1e3ccSAndroid Build Coastguard Worker static inline int fill_pyramid(const YV12_BUFFER_CONFIG *frame, int bit_depth,
258*77c1e3ccSAndroid Build Coastguard Worker                                int n_levels, ImagePyramid *frame_pyr) {
259*77c1e3ccSAndroid Build Coastguard Worker   int already_filled_levels = frame_pyr->filled_levels;
260*77c1e3ccSAndroid Build Coastguard Worker 
261*77c1e3ccSAndroid Build Coastguard Worker   // This condition should already be enforced by aom_compute_pyramid
262*77c1e3ccSAndroid Build Coastguard Worker   assert(n_levels <= frame_pyr->max_levels);
263*77c1e3ccSAndroid Build Coastguard Worker 
264*77c1e3ccSAndroid Build Coastguard Worker   if (already_filled_levels >= n_levels) {
265*77c1e3ccSAndroid Build Coastguard Worker     return n_levels;
266*77c1e3ccSAndroid Build Coastguard Worker   }
267*77c1e3ccSAndroid Build Coastguard Worker 
268*77c1e3ccSAndroid Build Coastguard Worker   const int frame_width = frame->y_crop_width;
269*77c1e3ccSAndroid Build Coastguard Worker   const int frame_height = frame->y_crop_height;
270*77c1e3ccSAndroid Build Coastguard Worker   const int frame_stride = frame->y_stride;
271*77c1e3ccSAndroid Build Coastguard Worker   assert((frame_width >> n_levels) >= 0);
272*77c1e3ccSAndroid Build Coastguard Worker   assert((frame_height >> n_levels) >= 0);
273*77c1e3ccSAndroid Build Coastguard Worker 
274*77c1e3ccSAndroid Build Coastguard Worker   if (already_filled_levels == 0) {
275*77c1e3ccSAndroid Build Coastguard Worker     // Fill in largest level from the original image
276*77c1e3ccSAndroid Build Coastguard Worker     PyramidLayer *first_layer = &frame_pyr->layers[0];
277*77c1e3ccSAndroid Build Coastguard Worker     if (frame->flags & YV12_FLAG_HIGHBITDEPTH) {
278*77c1e3ccSAndroid Build Coastguard Worker       // For frames stored in a 16-bit buffer, we need to downconvert to 8 bits
279*77c1e3ccSAndroid Build Coastguard Worker       assert(first_layer->width == frame_width);
280*77c1e3ccSAndroid Build Coastguard Worker       assert(first_layer->height == frame_height);
281*77c1e3ccSAndroid Build Coastguard Worker 
282*77c1e3ccSAndroid Build Coastguard Worker       uint16_t *frame_buffer = CONVERT_TO_SHORTPTR(frame->y_buffer);
283*77c1e3ccSAndroid Build Coastguard Worker       uint8_t *pyr_buffer = first_layer->buffer;
284*77c1e3ccSAndroid Build Coastguard Worker       int pyr_stride = first_layer->stride;
285*77c1e3ccSAndroid Build Coastguard Worker       for (int y = 0; y < frame_height; y++) {
286*77c1e3ccSAndroid Build Coastguard Worker         uint16_t *frame_row = frame_buffer + y * frame_stride;
287*77c1e3ccSAndroid Build Coastguard Worker         uint8_t *pyr_row = pyr_buffer + y * pyr_stride;
288*77c1e3ccSAndroid Build Coastguard Worker         for (int x = 0; x < frame_width; x++) {
289*77c1e3ccSAndroid Build Coastguard Worker           pyr_row[x] = frame_row[x] >> (bit_depth - 8);
290*77c1e3ccSAndroid Build Coastguard Worker         }
291*77c1e3ccSAndroid Build Coastguard Worker       }
292*77c1e3ccSAndroid Build Coastguard Worker 
293*77c1e3ccSAndroid Build Coastguard Worker       fill_border(pyr_buffer, frame_width, frame_height, pyr_stride);
294*77c1e3ccSAndroid Build Coastguard Worker     } else {
295*77c1e3ccSAndroid Build Coastguard Worker       // For frames stored in an 8-bit buffer, we don't need to copy anything -
296*77c1e3ccSAndroid Build Coastguard Worker       // we can just reference the original image buffer
297*77c1e3ccSAndroid Build Coastguard Worker       first_layer->buffer = frame->y_buffer;
298*77c1e3ccSAndroid Build Coastguard Worker       first_layer->width = frame_width;
299*77c1e3ccSAndroid Build Coastguard Worker       first_layer->height = frame_height;
300*77c1e3ccSAndroid Build Coastguard Worker       first_layer->stride = frame_stride;
301*77c1e3ccSAndroid Build Coastguard Worker     }
302*77c1e3ccSAndroid Build Coastguard Worker 
303*77c1e3ccSAndroid Build Coastguard Worker     already_filled_levels = 1;
304*77c1e3ccSAndroid Build Coastguard Worker   }
305*77c1e3ccSAndroid Build Coastguard Worker 
306*77c1e3ccSAndroid Build Coastguard Worker   // Fill in the remaining levels through progressive downsampling
307*77c1e3ccSAndroid Build Coastguard Worker   for (int level = already_filled_levels; level < n_levels; ++level) {
308*77c1e3ccSAndroid Build Coastguard Worker     bool mem_status = false;
309*77c1e3ccSAndroid Build Coastguard Worker     PyramidLayer *prev_layer = &frame_pyr->layers[level - 1];
310*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *prev_buffer = prev_layer->buffer;
311*77c1e3ccSAndroid Build Coastguard Worker     int prev_stride = prev_layer->stride;
312*77c1e3ccSAndroid Build Coastguard Worker 
313*77c1e3ccSAndroid Build Coastguard Worker     PyramidLayer *this_layer = &frame_pyr->layers[level];
314*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *this_buffer = this_layer->buffer;
315*77c1e3ccSAndroid Build Coastguard Worker     int this_width = this_layer->width;
316*77c1e3ccSAndroid Build Coastguard Worker     int this_height = this_layer->height;
317*77c1e3ccSAndroid Build Coastguard Worker     int this_stride = this_layer->stride;
318*77c1e3ccSAndroid Build Coastguard Worker 
319*77c1e3ccSAndroid Build Coastguard Worker     // The width and height of the previous layer that needs to be considered to
320*77c1e3ccSAndroid Build Coastguard Worker     // derive the current layer frame.
321*77c1e3ccSAndroid Build Coastguard Worker     const int input_layer_width = this_width << 1;
322*77c1e3ccSAndroid Build Coastguard Worker     const int input_layer_height = this_height << 1;
323*77c1e3ccSAndroid Build Coastguard Worker 
324*77c1e3ccSAndroid Build Coastguard Worker     // Compute the this pyramid level by downsampling the current level.
325*77c1e3ccSAndroid Build Coastguard Worker     //
326*77c1e3ccSAndroid Build Coastguard Worker     // We downsample by a factor of exactly 2, clipping the rightmost and
327*77c1e3ccSAndroid Build Coastguard Worker     // bottommost pixel off of the current level if needed. We do this for
328*77c1e3ccSAndroid Build Coastguard Worker     // two main reasons:
329*77c1e3ccSAndroid Build Coastguard Worker     //
330*77c1e3ccSAndroid Build Coastguard Worker     // 1) In the disflow code, when stepping from a higher pyramid level to a
331*77c1e3ccSAndroid Build Coastguard Worker     //    lower pyramid level, we need to not just interpolate the flow field
332*77c1e3ccSAndroid Build Coastguard Worker     //    but also to scale each flow vector by the upsampling ratio.
333*77c1e3ccSAndroid Build Coastguard Worker     //    So it is much more convenient if this ratio is simply 2.
334*77c1e3ccSAndroid Build Coastguard Worker     //
335*77c1e3ccSAndroid Build Coastguard Worker     // 2) Up/downsampling by a factor of 2 can be implemented much more
336*77c1e3ccSAndroid Build Coastguard Worker     //    efficiently than up/downsampling by a generic ratio.
337*77c1e3ccSAndroid Build Coastguard Worker     //    TODO(rachelbarker): Use optimized downsample-by-2 function
338*77c1e3ccSAndroid Build Coastguard Worker 
339*77c1e3ccSAndroid Build Coastguard Worker     // SIMD support has been added specifically for cases where the downsample
340*77c1e3ccSAndroid Build Coastguard Worker     // factor is exactly 2. In such instances, horizontal and vertical resizing
341*77c1e3ccSAndroid Build Coastguard Worker     // is performed utilizing the down2_symeven() function, which considers the
342*77c1e3ccSAndroid Build Coastguard Worker     // even dimensions of the input layer.
343*77c1e3ccSAndroid Build Coastguard Worker     if (should_resize_by_half(input_layer_height, input_layer_width,
344*77c1e3ccSAndroid Build Coastguard Worker                               this_height, this_width)) {
345*77c1e3ccSAndroid Build Coastguard Worker       assert(input_layer_height % 2 == 0 && input_layer_width % 2 == 0 &&
346*77c1e3ccSAndroid Build Coastguard Worker              "Input width or height cannot be odd.");
347*77c1e3ccSAndroid Build Coastguard Worker       mem_status = av1_resize_plane_to_half(
348*77c1e3ccSAndroid Build Coastguard Worker           prev_buffer, input_layer_height, input_layer_width, prev_stride,
349*77c1e3ccSAndroid Build Coastguard Worker           this_buffer, this_height, this_width, this_stride);
350*77c1e3ccSAndroid Build Coastguard Worker     } else {
351*77c1e3ccSAndroid Build Coastguard Worker       mem_status = av1_resize_plane(prev_buffer, input_layer_height,
352*77c1e3ccSAndroid Build Coastguard Worker                                     input_layer_width, prev_stride, this_buffer,
353*77c1e3ccSAndroid Build Coastguard Worker                                     this_height, this_width, this_stride);
354*77c1e3ccSAndroid Build Coastguard Worker     }
355*77c1e3ccSAndroid Build Coastguard Worker 
356*77c1e3ccSAndroid Build Coastguard Worker     // Terminate early in cases of memory allocation failure.
357*77c1e3ccSAndroid Build Coastguard Worker     if (!mem_status) {
358*77c1e3ccSAndroid Build Coastguard Worker       frame_pyr->filled_levels = n_levels;
359*77c1e3ccSAndroid Build Coastguard Worker       return -1;
360*77c1e3ccSAndroid Build Coastguard Worker     }
361*77c1e3ccSAndroid Build Coastguard Worker 
362*77c1e3ccSAndroid Build Coastguard Worker     fill_border(this_buffer, this_width, this_height, this_stride);
363*77c1e3ccSAndroid Build Coastguard Worker   }
364*77c1e3ccSAndroid Build Coastguard Worker 
365*77c1e3ccSAndroid Build Coastguard Worker   frame_pyr->filled_levels = n_levels;
366*77c1e3ccSAndroid Build Coastguard Worker   return n_levels;
367*77c1e3ccSAndroid Build Coastguard Worker }
368*77c1e3ccSAndroid Build Coastguard Worker 
369*77c1e3ccSAndroid Build Coastguard Worker // Fill out a downsampling pyramid for a given frame.
370*77c1e3ccSAndroid Build Coastguard Worker //
371*77c1e3ccSAndroid Build Coastguard Worker // The top level (index 0) will always be an 8-bit copy of the input frame,
372*77c1e3ccSAndroid Build Coastguard Worker // regardless of the input bit depth. Additional levels are then downscaled
373*77c1e3ccSAndroid Build Coastguard Worker // by powers of 2.
374*77c1e3ccSAndroid Build Coastguard Worker //
375*77c1e3ccSAndroid Build Coastguard Worker // This function will ensure that the first `n_levels` levels of the pyramid
376*77c1e3ccSAndroid Build Coastguard Worker // are filled, unless the frame is too small to have this many levels.
377*77c1e3ccSAndroid Build Coastguard Worker // In that case, we will fill all available levels and then stop.
378*77c1e3ccSAndroid Build Coastguard Worker // No matter how small the frame is, at least one level is guaranteed
379*77c1e3ccSAndroid Build Coastguard Worker // to be filled.
380*77c1e3ccSAndroid Build Coastguard Worker //
381*77c1e3ccSAndroid Build Coastguard Worker // Returns the actual number of levels filled, capped at n_levels,
382*77c1e3ccSAndroid Build Coastguard Worker // or -1 on error.
aom_compute_pyramid(const YV12_BUFFER_CONFIG * frame,int bit_depth,int n_levels,ImagePyramid * pyr)383*77c1e3ccSAndroid Build Coastguard Worker int aom_compute_pyramid(const YV12_BUFFER_CONFIG *frame, int bit_depth,
384*77c1e3ccSAndroid Build Coastguard Worker                         int n_levels, ImagePyramid *pyr) {
385*77c1e3ccSAndroid Build Coastguard Worker   assert(pyr);
386*77c1e3ccSAndroid Build Coastguard Worker 
387*77c1e3ccSAndroid Build Coastguard Worker   // Per the comments in the ImagePyramid struct, we must take this mutex
388*77c1e3ccSAndroid Build Coastguard Worker   // before reading or writing the filled_levels field, and hold it while
389*77c1e3ccSAndroid Build Coastguard Worker   // computing any additional pyramid levels, to ensure proper behaviour
390*77c1e3ccSAndroid Build Coastguard Worker   // when multithreading is used
391*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
392*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_lock(&pyr->mutex);
393*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
394*77c1e3ccSAndroid Build Coastguard Worker 
395*77c1e3ccSAndroid Build Coastguard Worker   n_levels = AOMMIN(n_levels, pyr->max_levels);
396*77c1e3ccSAndroid Build Coastguard Worker   int result = n_levels;
397*77c1e3ccSAndroid Build Coastguard Worker   if (pyr->filled_levels < n_levels) {
398*77c1e3ccSAndroid Build Coastguard Worker     // Compute any missing levels that we need
399*77c1e3ccSAndroid Build Coastguard Worker     result = fill_pyramid(frame, bit_depth, n_levels, pyr);
400*77c1e3ccSAndroid Build Coastguard Worker   }
401*77c1e3ccSAndroid Build Coastguard Worker 
402*77c1e3ccSAndroid Build Coastguard Worker   // At this point, as long as result >= 0, the requested number of pyramid
403*77c1e3ccSAndroid Build Coastguard Worker   // levels are guaranteed to be valid, and can be safely read from without
404*77c1e3ccSAndroid Build Coastguard Worker   // holding the mutex any further
405*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(result >= 0, pyr->filled_levels >= n_levels));
406*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
407*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_unlock(&pyr->mutex);
408*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
409*77c1e3ccSAndroid Build Coastguard Worker   return result;
410*77c1e3ccSAndroid Build Coastguard Worker }
411*77c1e3ccSAndroid Build Coastguard Worker 
412*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
413*77c1e3ccSAndroid Build Coastguard Worker // Check if a pyramid has already been computed to at least n levels
414*77c1e3ccSAndroid Build Coastguard Worker // This is mostly a debug helper - as it is necessary to hold pyr->mutex
415*77c1e3ccSAndroid Build Coastguard Worker // while reading the number of already-computed levels, we cannot just write:
416*77c1e3ccSAndroid Build Coastguard Worker //   assert(pyr->filled_levels >= n_levels);
417*77c1e3ccSAndroid Build Coastguard Worker // This function allows the check to be correctly written as:
418*77c1e3ccSAndroid Build Coastguard Worker //   assert(aom_is_pyramid_valid(pyr, n_levels));
419*77c1e3ccSAndroid Build Coastguard Worker //
420*77c1e3ccSAndroid Build Coastguard Worker // Note: This deliberately does not restrict n_levels based on the maximum
421*77c1e3ccSAndroid Build Coastguard Worker // number of permitted levels for the frame size. This allows the check to
422*77c1e3ccSAndroid Build Coastguard Worker // catch cases where the caller forgets to handle the case where
423*77c1e3ccSAndroid Build Coastguard Worker // max_levels is less than the requested number of levels
aom_is_pyramid_valid(ImagePyramid * pyr,int n_levels)424*77c1e3ccSAndroid Build Coastguard Worker bool aom_is_pyramid_valid(ImagePyramid *pyr, int n_levels) {
425*77c1e3ccSAndroid Build Coastguard Worker   assert(pyr);
426*77c1e3ccSAndroid Build Coastguard Worker 
427*77c1e3ccSAndroid Build Coastguard Worker   // Per the comments in the ImagePyramid struct, we must take this mutex
428*77c1e3ccSAndroid Build Coastguard Worker   // before reading or writing the filled_levels field, to ensure proper
429*77c1e3ccSAndroid Build Coastguard Worker   // behaviour when multithreading is used
430*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
431*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_lock(&pyr->mutex);
432*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
433*77c1e3ccSAndroid Build Coastguard Worker 
434*77c1e3ccSAndroid Build Coastguard Worker   bool result = (pyr->filled_levels >= n_levels);
435*77c1e3ccSAndroid Build Coastguard Worker 
436*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
437*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_unlock(&pyr->mutex);
438*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
439*77c1e3ccSAndroid Build Coastguard Worker 
440*77c1e3ccSAndroid Build Coastguard Worker   return result;
441*77c1e3ccSAndroid Build Coastguard Worker }
442*77c1e3ccSAndroid Build Coastguard Worker #endif
443*77c1e3ccSAndroid Build Coastguard Worker 
444*77c1e3ccSAndroid Build Coastguard Worker // Mark a pyramid as no longer containing valid data.
445*77c1e3ccSAndroid Build Coastguard Worker // This must be done whenever the corresponding frame buffer is reused
aom_invalidate_pyramid(ImagePyramid * pyr)446*77c1e3ccSAndroid Build Coastguard Worker void aom_invalidate_pyramid(ImagePyramid *pyr) {
447*77c1e3ccSAndroid Build Coastguard Worker   if (pyr) {
448*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
449*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(&pyr->mutex);
450*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
451*77c1e3ccSAndroid Build Coastguard Worker     pyr->filled_levels = 0;
452*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
453*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(&pyr->mutex);
454*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
455*77c1e3ccSAndroid Build Coastguard Worker   }
456*77c1e3ccSAndroid Build Coastguard Worker }
457*77c1e3ccSAndroid Build Coastguard Worker 
458*77c1e3ccSAndroid Build Coastguard Worker // Release the memory associated with a pyramid
aom_free_pyramid(ImagePyramid * pyr)459*77c1e3ccSAndroid Build Coastguard Worker void aom_free_pyramid(ImagePyramid *pyr) {
460*77c1e3ccSAndroid Build Coastguard Worker   if (pyr) {
461*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
462*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(&pyr->mutex);
463*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
464*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr->buffer_alloc);
465*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr->layers);
466*77c1e3ccSAndroid Build Coastguard Worker     aom_free(pyr);
467*77c1e3ccSAndroid Build Coastguard Worker   }
468*77c1e3ccSAndroid Build Coastguard Worker }
469