xref: /aosp_15_r20/external/libaom/aom_scale/yv12config.h (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 #ifndef AOM_AOM_SCALE_YV12CONFIG_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_SCALE_YV12CONFIG_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
16*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
17*77c1e3ccSAndroid Build Coastguard Worker #endif
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h>
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
22*77c1e3ccSAndroid Build Coastguard Worker 
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_codec.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_frame_buffer.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "aom/internal/aom_image_internal.h"
27*77c1e3ccSAndroid Build Coastguard Worker 
28*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
29*77c1e3ccSAndroid Build Coastguard Worker 
30*77c1e3ccSAndroid Build Coastguard Worker #define AOMINNERBORDERINPIXELS 160
31*77c1e3ccSAndroid Build Coastguard Worker #define AOM_INTERP_EXTEND 4
32*77c1e3ccSAndroid Build Coastguard Worker #define AOM_BORDER_IN_PIXELS 288
33*77c1e3ccSAndroid Build Coastguard Worker #define AOM_ENC_NO_SCALE_BORDER 160
34*77c1e3ccSAndroid Build Coastguard Worker #define AOM_ENC_ALLINTRA_BORDER 64
35*77c1e3ccSAndroid Build Coastguard Worker #define AOM_DEC_BORDER_IN_PIXELS 64
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY
38*77c1e3ccSAndroid Build Coastguard Worker struct image_pyramid;
39*77c1e3ccSAndroid Build Coastguard Worker struct corner_list;
40*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
43*77c1e3ccSAndroid Build Coastguard Worker /*!
44*77c1e3ccSAndroid Build Coastguard Worker  * \brief YV12 frame buffer data structure
45*77c1e3ccSAndroid Build Coastguard Worker  */
46*77c1e3ccSAndroid Build Coastguard Worker typedef struct yv12_buffer_config {
47*77c1e3ccSAndroid Build Coastguard Worker   /*!\cond */
48*77c1e3ccSAndroid Build Coastguard Worker   union {
49*77c1e3ccSAndroid Build Coastguard Worker     struct {
50*77c1e3ccSAndroid Build Coastguard Worker       // The aligned frame width of luma.
51*77c1e3ccSAndroid Build Coastguard Worker       // It is aligned to a multiple of 8:
52*77c1e3ccSAndroid Build Coastguard Worker       // y_width = (y_crop_width + 7) & ~7
53*77c1e3ccSAndroid Build Coastguard Worker       int y_width;
54*77c1e3ccSAndroid Build Coastguard Worker       // The aligned frame width of chroma.
55*77c1e3ccSAndroid Build Coastguard Worker       // uv_width = y_width >> subsampling_x
56*77c1e3ccSAndroid Build Coastguard Worker       int uv_width;
57*77c1e3ccSAndroid Build Coastguard Worker     };
58*77c1e3ccSAndroid Build Coastguard Worker     int widths[2];
59*77c1e3ccSAndroid Build Coastguard Worker   };
60*77c1e3ccSAndroid Build Coastguard Worker   union {
61*77c1e3ccSAndroid Build Coastguard Worker     struct {
62*77c1e3ccSAndroid Build Coastguard Worker       // The aligned frame height of luma.
63*77c1e3ccSAndroid Build Coastguard Worker       // It is aligned to a multiple of 8:
64*77c1e3ccSAndroid Build Coastguard Worker       // y_height = (y_crop_height + 7) & ~7
65*77c1e3ccSAndroid Build Coastguard Worker       int y_height;
66*77c1e3ccSAndroid Build Coastguard Worker       // The aligned frame height of chroma.
67*77c1e3ccSAndroid Build Coastguard Worker       // uv_height = y_height >> subsampling_y
68*77c1e3ccSAndroid Build Coastguard Worker       int uv_height;
69*77c1e3ccSAndroid Build Coastguard Worker     };
70*77c1e3ccSAndroid Build Coastguard Worker     int heights[2];
71*77c1e3ccSAndroid Build Coastguard Worker   };
72*77c1e3ccSAndroid Build Coastguard Worker   // The frame size en/decoded by AV1
73*77c1e3ccSAndroid Build Coastguard Worker   union {
74*77c1e3ccSAndroid Build Coastguard Worker     struct {
75*77c1e3ccSAndroid Build Coastguard Worker       int y_crop_width;
76*77c1e3ccSAndroid Build Coastguard Worker       int uv_crop_width;
77*77c1e3ccSAndroid Build Coastguard Worker     };
78*77c1e3ccSAndroid Build Coastguard Worker     int crop_widths[2];
79*77c1e3ccSAndroid Build Coastguard Worker   };
80*77c1e3ccSAndroid Build Coastguard Worker   union {
81*77c1e3ccSAndroid Build Coastguard Worker     struct {
82*77c1e3ccSAndroid Build Coastguard Worker       int y_crop_height;
83*77c1e3ccSAndroid Build Coastguard Worker       int uv_crop_height;
84*77c1e3ccSAndroid Build Coastguard Worker     };
85*77c1e3ccSAndroid Build Coastguard Worker     int crop_heights[2];
86*77c1e3ccSAndroid Build Coastguard Worker   };
87*77c1e3ccSAndroid Build Coastguard Worker   union {
88*77c1e3ccSAndroid Build Coastguard Worker     struct {
89*77c1e3ccSAndroid Build Coastguard Worker       int y_stride;
90*77c1e3ccSAndroid Build Coastguard Worker       int uv_stride;
91*77c1e3ccSAndroid Build Coastguard Worker     };
92*77c1e3ccSAndroid Build Coastguard Worker     int strides[2];
93*77c1e3ccSAndroid Build Coastguard Worker   };
94*77c1e3ccSAndroid Build Coastguard Worker   union {
95*77c1e3ccSAndroid Build Coastguard Worker     struct {
96*77c1e3ccSAndroid Build Coastguard Worker       uint8_t *y_buffer;
97*77c1e3ccSAndroid Build Coastguard Worker       uint8_t *u_buffer;
98*77c1e3ccSAndroid Build Coastguard Worker       uint8_t *v_buffer;
99*77c1e3ccSAndroid Build Coastguard Worker     };
100*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *buffers[3];
101*77c1e3ccSAndroid Build Coastguard Worker   };
102*77c1e3ccSAndroid Build Coastguard Worker 
103*77c1e3ccSAndroid Build Coastguard Worker   // Indicate whether y_buffer, u_buffer, and v_buffer points to the internally
104*77c1e3ccSAndroid Build Coastguard Worker   // allocated memory or external buffers.
105*77c1e3ccSAndroid Build Coastguard Worker   int use_external_reference_buffers;
106*77c1e3ccSAndroid Build Coastguard Worker   // This is needed to store y_buffer, u_buffer, and v_buffer when set reference
107*77c1e3ccSAndroid Build Coastguard Worker   // uses an external refernece, and restore those buffer pointers after the
108*77c1e3ccSAndroid Build Coastguard Worker   // external reference frame is no longer used.
109*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *store_buf_adr[3];
110*77c1e3ccSAndroid Build Coastguard Worker 
111*77c1e3ccSAndroid Build Coastguard Worker   // Global motion search data
112*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY
113*77c1e3ccSAndroid Build Coastguard Worker   // 8-bit downsampling pyramid for the Y plane
114*77c1e3ccSAndroid Build Coastguard Worker   struct image_pyramid *y_pyramid;
115*77c1e3ccSAndroid Build Coastguard Worker   struct corner_list *corners;
116*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *buffer_alloc;
119*77c1e3ccSAndroid Build Coastguard Worker   size_t buffer_alloc_sz;
120*77c1e3ccSAndroid Build Coastguard Worker   int border;
121*77c1e3ccSAndroid Build Coastguard Worker   size_t frame_size;
122*77c1e3ccSAndroid Build Coastguard Worker   int subsampling_x;
123*77c1e3ccSAndroid Build Coastguard Worker   int subsampling_y;
124*77c1e3ccSAndroid Build Coastguard Worker   unsigned int bit_depth;
125*77c1e3ccSAndroid Build Coastguard Worker   aom_color_primaries_t color_primaries;
126*77c1e3ccSAndroid Build Coastguard Worker   aom_transfer_characteristics_t transfer_characteristics;
127*77c1e3ccSAndroid Build Coastguard Worker   aom_matrix_coefficients_t matrix_coefficients;
128*77c1e3ccSAndroid Build Coastguard Worker   uint8_t monochrome;
129*77c1e3ccSAndroid Build Coastguard Worker   aom_chroma_sample_position_t chroma_sample_position;
130*77c1e3ccSAndroid Build Coastguard Worker   aom_color_range_t color_range;
131*77c1e3ccSAndroid Build Coastguard Worker   int render_width;
132*77c1e3ccSAndroid Build Coastguard Worker   int render_height;
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker   int corrupted;
135*77c1e3ccSAndroid Build Coastguard Worker   int flags;
136*77c1e3ccSAndroid Build Coastguard Worker   aom_metadata_array_t *metadata;
137*77c1e3ccSAndroid Build Coastguard Worker   /*!\endcond */
138*77c1e3ccSAndroid Build Coastguard Worker } YV12_BUFFER_CONFIG;
139*77c1e3ccSAndroid Build Coastguard Worker 
140*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
141*77c1e3ccSAndroid Build Coastguard Worker 
142*77c1e3ccSAndroid Build Coastguard Worker #define YV12_FLAG_HIGHBITDEPTH 8
143*77c1e3ccSAndroid Build Coastguard Worker 
144*77c1e3ccSAndroid Build Coastguard Worker // Allocate a frame buffer
145*77c1e3ccSAndroid Build Coastguard Worker //
146*77c1e3ccSAndroid Build Coastguard Worker // If ybf currently contains an image, all associated memory will be freed and
147*77c1e3ccSAndroid Build Coastguard Worker // then reallocated. In contrast, aom_realloc_frame_buffer() will reuse any
148*77c1e3ccSAndroid Build Coastguard Worker // existing allocations where possible. So, if ybf is likely to already be
149*77c1e3ccSAndroid Build Coastguard Worker // set up, please consider aom_realloc_frame_buffer() instead.
150*77c1e3ccSAndroid Build Coastguard Worker //
151*77c1e3ccSAndroid Build Coastguard Worker // See aom_realloc_frame_buffer() for the meanings of the arguments, and
152*77c1e3ccSAndroid Build Coastguard Worker // available return values.
153*77c1e3ccSAndroid Build Coastguard Worker int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
154*77c1e3ccSAndroid Build Coastguard Worker                            int ss_x, int ss_y, int use_highbitdepth, int border,
155*77c1e3ccSAndroid Build Coastguard Worker                            int byte_alignment, bool alloc_pyramid,
156*77c1e3ccSAndroid Build Coastguard Worker                            int alloc_y_plane_only);
157*77c1e3ccSAndroid Build Coastguard Worker 
158*77c1e3ccSAndroid Build Coastguard Worker // Updates the yv12 buffer config with the frame buffer. |byte_alignment| must
159*77c1e3ccSAndroid Build Coastguard Worker // be a power of 2, from 32 to 1024. 0 sets legacy alignment. If cb is not
160*77c1e3ccSAndroid Build Coastguard Worker // NULL, then libaom is using the frame buffer callbacks to handle memory.
161*77c1e3ccSAndroid Build Coastguard Worker // If cb is not NULL, libaom will call cb with minimum size in bytes needed
162*77c1e3ccSAndroid Build Coastguard Worker // to decode the current frame. If cb is NULL, libaom will allocate memory
163*77c1e3ccSAndroid Build Coastguard Worker // internally to decode the current frame.
164*77c1e3ccSAndroid Build Coastguard Worker //
165*77c1e3ccSAndroid Build Coastguard Worker // If alloc_pyramid is true, then an image pyramid will be allocated
166*77c1e3ccSAndroid Build Coastguard Worker // for use in global motion estimation. This is only needed if this frame
167*77c1e3ccSAndroid Build Coastguard Worker // buffer will be used to store a source frame or a reference frame in
168*77c1e3ccSAndroid Build Coastguard Worker // the encoder. Any other framebuffers (eg, intermediates for filtering,
169*77c1e3ccSAndroid Build Coastguard Worker // or any buffer in the decoder) can set alloc_pyramid = false.
170*77c1e3ccSAndroid Build Coastguard Worker //
171*77c1e3ccSAndroid Build Coastguard Worker // Returns 0 on success. Returns < 0  on failure.
172*77c1e3ccSAndroid Build Coastguard Worker int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
173*77c1e3ccSAndroid Build Coastguard Worker                              int ss_x, int ss_y, int use_highbitdepth,
174*77c1e3ccSAndroid Build Coastguard Worker                              int border, int byte_alignment,
175*77c1e3ccSAndroid Build Coastguard Worker                              aom_codec_frame_buffer_t *fb,
176*77c1e3ccSAndroid Build Coastguard Worker                              aom_get_frame_buffer_cb_fn_t cb, void *cb_priv,
177*77c1e3ccSAndroid Build Coastguard Worker                              bool alloc_pyramid, int alloc_y_plane_only);
178*77c1e3ccSAndroid Build Coastguard Worker 
179*77c1e3ccSAndroid Build Coastguard Worker int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf);
180*77c1e3ccSAndroid Build Coastguard Worker 
181*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
182*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Removes metadata from YUV_BUFFER_CONFIG struct.
183*77c1e3ccSAndroid Build Coastguard Worker  *
184*77c1e3ccSAndroid Build Coastguard Worker  * Frees metadata in frame buffer.
185*77c1e3ccSAndroid Build Coastguard Worker  * Frame buffer metadata pointer will be set to NULL.
186*77c1e3ccSAndroid Build Coastguard Worker  *
187*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ybf       Frame buffer struct pointer
188*77c1e3ccSAndroid Build Coastguard Worker  */
189*77c1e3ccSAndroid Build Coastguard Worker void aom_remove_metadata_from_frame_buffer(YV12_BUFFER_CONFIG *ybf);
190*77c1e3ccSAndroid Build Coastguard Worker 
191*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Copy metadata to YUV_BUFFER_CONFIG struct.
192*77c1e3ccSAndroid Build Coastguard Worker  *
193*77c1e3ccSAndroid Build Coastguard Worker  * Copies metadata to frame buffer.
194*77c1e3ccSAndroid Build Coastguard Worker  * Frame buffer will clear any previous metadata and will reallocate the
195*77c1e3ccSAndroid Build Coastguard Worker  * metadata array to the new metadata size. Then, it will copy the new metadata
196*77c1e3ccSAndroid Build Coastguard Worker  * array into it.
197*77c1e3ccSAndroid Build Coastguard Worker  * If arr metadata pointer points to the same address as current metadata in the
198*77c1e3ccSAndroid Build Coastguard Worker  * frame buffer, function will do nothing and return 0.
199*77c1e3ccSAndroid Build Coastguard Worker  * Returns 0 on success or -1 on failure.
200*77c1e3ccSAndroid Build Coastguard Worker  *
201*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ybf       Frame buffer struct pointer
202*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    arr       Metadata array struct pointer
203*77c1e3ccSAndroid Build Coastguard Worker  */
204*77c1e3ccSAndroid Build Coastguard Worker int aom_copy_metadata_to_frame_buffer(YV12_BUFFER_CONFIG *ybf,
205*77c1e3ccSAndroid Build Coastguard Worker                                       const aom_metadata_array_t *arr);
206*77c1e3ccSAndroid Build Coastguard Worker 
207*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Calculate the stride required for the image.
208*77c1e3ccSAndroid Build Coastguard Worker  *
209*77c1e3ccSAndroid Build Coastguard Worker  * Calculates the stride value for an image from aligned width and border.
210*77c1e3ccSAndroid Build Coastguard Worker  * Returns the y stride value.
211*77c1e3ccSAndroid Build Coastguard Worker  *
212*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    aligned_width       Aligned width of the image
213*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    border              Border in pixels
214*77c1e3ccSAndroid Build Coastguard Worker  */
aom_calc_y_stride(int aligned_width,int border)215*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_calc_y_stride(int aligned_width, int border) {
216*77c1e3ccSAndroid Build Coastguard Worker   return ((aligned_width + 2 * border) + 31) & ~31;
217*77c1e3ccSAndroid Build Coastguard Worker }
218*77c1e3ccSAndroid Build Coastguard Worker 
219*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
220*77c1e3ccSAndroid Build Coastguard Worker }
221*77c1e3ccSAndroid Build Coastguard Worker #endif
222*77c1e3ccSAndroid Build Coastguard Worker 
223*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_SCALE_YV12CONFIG_H_
224