xref: /aosp_15_r20/external/libaom/av1/common/cfl.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 "av1/common/av1_common_int.h"
13*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/cfl.h"
14*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common_data.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
cfl_init(CFL_CTX * cfl,const SequenceHeader * seq_params)18*77c1e3ccSAndroid Build Coastguard Worker void cfl_init(CFL_CTX *cfl, const SequenceHeader *seq_params) {
19*77c1e3ccSAndroid Build Coastguard Worker   assert(block_size_wide[CFL_MAX_BLOCK_SIZE] == CFL_BUF_LINE);
20*77c1e3ccSAndroid Build Coastguard Worker   assert(block_size_high[CFL_MAX_BLOCK_SIZE] == CFL_BUF_LINE);
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker   memset(&cfl->recon_buf_q3, 0, sizeof(cfl->recon_buf_q3));
23*77c1e3ccSAndroid Build Coastguard Worker   memset(&cfl->ac_buf_q3, 0, sizeof(cfl->ac_buf_q3));
24*77c1e3ccSAndroid Build Coastguard Worker   cfl->subsampling_x = seq_params->subsampling_x;
25*77c1e3ccSAndroid Build Coastguard Worker   cfl->subsampling_y = seq_params->subsampling_y;
26*77c1e3ccSAndroid Build Coastguard Worker   cfl->are_parameters_computed = 0;
27*77c1e3ccSAndroid Build Coastguard Worker   cfl->store_y = 0;
28*77c1e3ccSAndroid Build Coastguard Worker   // The DC_PRED cache is disabled by default and is only enabled in
29*77c1e3ccSAndroid Build Coastguard Worker   // cfl_rd_pick_alpha
30*77c1e3ccSAndroid Build Coastguard Worker   clear_cfl_dc_pred_cache_flags(cfl);
31*77c1e3ccSAndroid Build Coastguard Worker }
32*77c1e3ccSAndroid Build Coastguard Worker 
cfl_store_dc_pred(MACROBLOCKD * const xd,const uint8_t * input,CFL_PRED_TYPE pred_plane,int width)33*77c1e3ccSAndroid Build Coastguard Worker void cfl_store_dc_pred(MACROBLOCKD *const xd, const uint8_t *input,
34*77c1e3ccSAndroid Build Coastguard Worker                        CFL_PRED_TYPE pred_plane, int width) {
35*77c1e3ccSAndroid Build Coastguard Worker   assert(pred_plane < CFL_PRED_PLANES);
36*77c1e3ccSAndroid Build Coastguard Worker   assert(width <= CFL_BUF_LINE);
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker   if (is_cur_buf_hbd(xd)) {
39*77c1e3ccSAndroid Build Coastguard Worker     uint16_t *const input_16 = CONVERT_TO_SHORTPTR(input);
40*77c1e3ccSAndroid Build Coastguard Worker     memcpy(xd->cfl.dc_pred_cache[pred_plane], input_16, width << 1);
41*77c1e3ccSAndroid Build Coastguard Worker     return;
42*77c1e3ccSAndroid Build Coastguard Worker   }
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker   memcpy(xd->cfl.dc_pred_cache[pred_plane], input, width);
45*77c1e3ccSAndroid Build Coastguard Worker }
46*77c1e3ccSAndroid Build Coastguard Worker 
cfl_load_dc_pred_lbd(const int16_t * dc_pred_cache,uint8_t * dst,int dst_stride,int width,int height)47*77c1e3ccSAndroid Build Coastguard Worker static void cfl_load_dc_pred_lbd(const int16_t *dc_pred_cache, uint8_t *dst,
48*77c1e3ccSAndroid Build Coastguard Worker                                  int dst_stride, int width, int height) {
49*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
50*77c1e3ccSAndroid Build Coastguard Worker     memcpy(dst, dc_pred_cache, width);
51*77c1e3ccSAndroid Build Coastguard Worker     dst += dst_stride;
52*77c1e3ccSAndroid Build Coastguard Worker   }
53*77c1e3ccSAndroid Build Coastguard Worker }
54*77c1e3ccSAndroid Build Coastguard Worker 
cfl_load_dc_pred_hbd(const int16_t * dc_pred_cache,uint16_t * dst,int dst_stride,int width,int height)55*77c1e3ccSAndroid Build Coastguard Worker static void cfl_load_dc_pred_hbd(const int16_t *dc_pred_cache, uint16_t *dst,
56*77c1e3ccSAndroid Build Coastguard Worker                                  int dst_stride, int width, int height) {
57*77c1e3ccSAndroid Build Coastguard Worker   const size_t num_bytes = width << 1;
58*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
59*77c1e3ccSAndroid Build Coastguard Worker     memcpy(dst, dc_pred_cache, num_bytes);
60*77c1e3ccSAndroid Build Coastguard Worker     dst += dst_stride;
61*77c1e3ccSAndroid Build Coastguard Worker   }
62*77c1e3ccSAndroid Build Coastguard Worker }
cfl_load_dc_pred(MACROBLOCKD * const xd,uint8_t * dst,int dst_stride,TX_SIZE tx_size,CFL_PRED_TYPE pred_plane)63*77c1e3ccSAndroid Build Coastguard Worker void cfl_load_dc_pred(MACROBLOCKD *const xd, uint8_t *dst, int dst_stride,
64*77c1e3ccSAndroid Build Coastguard Worker                       TX_SIZE tx_size, CFL_PRED_TYPE pred_plane) {
65*77c1e3ccSAndroid Build Coastguard Worker   const int width = tx_size_wide[tx_size];
66*77c1e3ccSAndroid Build Coastguard Worker   const int height = tx_size_high[tx_size];
67*77c1e3ccSAndroid Build Coastguard Worker   assert(pred_plane < CFL_PRED_PLANES);
68*77c1e3ccSAndroid Build Coastguard Worker   assert(width <= CFL_BUF_LINE);
69*77c1e3ccSAndroid Build Coastguard Worker   assert(height <= CFL_BUF_LINE);
70*77c1e3ccSAndroid Build Coastguard Worker   if (is_cur_buf_hbd(xd)) {
71*77c1e3ccSAndroid Build Coastguard Worker     uint16_t *dst_16 = CONVERT_TO_SHORTPTR(dst);
72*77c1e3ccSAndroid Build Coastguard Worker     cfl_load_dc_pred_hbd(xd->cfl.dc_pred_cache[pred_plane], dst_16, dst_stride,
73*77c1e3ccSAndroid Build Coastguard Worker                          width, height);
74*77c1e3ccSAndroid Build Coastguard Worker     return;
75*77c1e3ccSAndroid Build Coastguard Worker   }
76*77c1e3ccSAndroid Build Coastguard Worker   cfl_load_dc_pred_lbd(xd->cfl.dc_pred_cache[pred_plane], dst, dst_stride,
77*77c1e3ccSAndroid Build Coastguard Worker                        width, height);
78*77c1e3ccSAndroid Build Coastguard Worker }
79*77c1e3ccSAndroid Build Coastguard Worker 
80*77c1e3ccSAndroid Build Coastguard Worker // Due to frame boundary issues, it is possible that the total area covered by
81*77c1e3ccSAndroid Build Coastguard Worker // chroma exceeds that of luma. When this happens, we fill the missing pixels by
82*77c1e3ccSAndroid Build Coastguard Worker // repeating the last columns and/or rows.
cfl_pad(CFL_CTX * cfl,int width,int height)83*77c1e3ccSAndroid Build Coastguard Worker static inline void cfl_pad(CFL_CTX *cfl, int width, int height) {
84*77c1e3ccSAndroid Build Coastguard Worker   const int diff_width = width - cfl->buf_width;
85*77c1e3ccSAndroid Build Coastguard Worker   const int diff_height = height - cfl->buf_height;
86*77c1e3ccSAndroid Build Coastguard Worker 
87*77c1e3ccSAndroid Build Coastguard Worker   if (diff_width > 0) {
88*77c1e3ccSAndroid Build Coastguard Worker     const int min_height = height - diff_height;
89*77c1e3ccSAndroid Build Coastguard Worker     uint16_t *recon_buf_q3 = cfl->recon_buf_q3 + (width - diff_width);
90*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < min_height; j++) {
91*77c1e3ccSAndroid Build Coastguard Worker       const uint16_t last_pixel = recon_buf_q3[-1];
92*77c1e3ccSAndroid Build Coastguard Worker       assert(recon_buf_q3 + diff_width <= cfl->recon_buf_q3 + CFL_BUF_SQUARE);
93*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < diff_width; i++) {
94*77c1e3ccSAndroid Build Coastguard Worker         recon_buf_q3[i] = last_pixel;
95*77c1e3ccSAndroid Build Coastguard Worker       }
96*77c1e3ccSAndroid Build Coastguard Worker       recon_buf_q3 += CFL_BUF_LINE;
97*77c1e3ccSAndroid Build Coastguard Worker     }
98*77c1e3ccSAndroid Build Coastguard Worker     cfl->buf_width = width;
99*77c1e3ccSAndroid Build Coastguard Worker   }
100*77c1e3ccSAndroid Build Coastguard Worker   if (diff_height > 0) {
101*77c1e3ccSAndroid Build Coastguard Worker     uint16_t *recon_buf_q3 =
102*77c1e3ccSAndroid Build Coastguard Worker         cfl->recon_buf_q3 + ((height - diff_height) * CFL_BUF_LINE);
103*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < diff_height; j++) {
104*77c1e3ccSAndroid Build Coastguard Worker       const uint16_t *last_row_q3 = recon_buf_q3 - CFL_BUF_LINE;
105*77c1e3ccSAndroid Build Coastguard Worker       assert(recon_buf_q3 + width <= cfl->recon_buf_q3 + CFL_BUF_SQUARE);
106*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < width; i++) {
107*77c1e3ccSAndroid Build Coastguard Worker         recon_buf_q3[i] = last_row_q3[i];
108*77c1e3ccSAndroid Build Coastguard Worker       }
109*77c1e3ccSAndroid Build Coastguard Worker       recon_buf_q3 += CFL_BUF_LINE;
110*77c1e3ccSAndroid Build Coastguard Worker     }
111*77c1e3ccSAndroid Build Coastguard Worker     cfl->buf_height = height;
112*77c1e3ccSAndroid Build Coastguard Worker   }
113*77c1e3ccSAndroid Build Coastguard Worker }
114*77c1e3ccSAndroid Build Coastguard Worker 
subtract_average_c(const uint16_t * src,int16_t * dst,int width,int height,int round_offset,int num_pel_log2)115*77c1e3ccSAndroid Build Coastguard Worker static void subtract_average_c(const uint16_t *src, int16_t *dst, int width,
116*77c1e3ccSAndroid Build Coastguard Worker                                int height, int round_offset, int num_pel_log2) {
117*77c1e3ccSAndroid Build Coastguard Worker   int sum = round_offset;
118*77c1e3ccSAndroid Build Coastguard Worker   const uint16_t *recon = src;
119*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
120*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i++) {
121*77c1e3ccSAndroid Build Coastguard Worker       sum += recon[i];
122*77c1e3ccSAndroid Build Coastguard Worker     }
123*77c1e3ccSAndroid Build Coastguard Worker     recon += CFL_BUF_LINE;
124*77c1e3ccSAndroid Build Coastguard Worker   }
125*77c1e3ccSAndroid Build Coastguard Worker   const int avg = sum >> num_pel_log2;
126*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
127*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i++) {
128*77c1e3ccSAndroid Build Coastguard Worker       dst[i] = src[i] - avg;
129*77c1e3ccSAndroid Build Coastguard Worker     }
130*77c1e3ccSAndroid Build Coastguard Worker     src += CFL_BUF_LINE;
131*77c1e3ccSAndroid Build Coastguard Worker     dst += CFL_BUF_LINE;
132*77c1e3ccSAndroid Build Coastguard Worker   }
133*77c1e3ccSAndroid Build Coastguard Worker }
134*77c1e3ccSAndroid Build Coastguard Worker 
CFL_SUB_AVG_FN(c)135*77c1e3ccSAndroid Build Coastguard Worker CFL_SUB_AVG_FN(c)
136*77c1e3ccSAndroid Build Coastguard Worker 
137*77c1e3ccSAndroid Build Coastguard Worker static inline int cfl_idx_to_alpha(uint8_t alpha_idx, int8_t joint_sign,
138*77c1e3ccSAndroid Build Coastguard Worker                                    CFL_PRED_TYPE pred_type) {
139*77c1e3ccSAndroid Build Coastguard Worker   const int alpha_sign = (pred_type == CFL_PRED_U) ? CFL_SIGN_U(joint_sign)
140*77c1e3ccSAndroid Build Coastguard Worker                                                    : CFL_SIGN_V(joint_sign);
141*77c1e3ccSAndroid Build Coastguard Worker   if (alpha_sign == CFL_SIGN_ZERO) return 0;
142*77c1e3ccSAndroid Build Coastguard Worker   const int abs_alpha_q3 =
143*77c1e3ccSAndroid Build Coastguard Worker       (pred_type == CFL_PRED_U) ? CFL_IDX_U(alpha_idx) : CFL_IDX_V(alpha_idx);
144*77c1e3ccSAndroid Build Coastguard Worker   return (alpha_sign == CFL_SIGN_POS) ? abs_alpha_q3 + 1 : -abs_alpha_q3 - 1;
145*77c1e3ccSAndroid Build Coastguard Worker }
146*77c1e3ccSAndroid Build Coastguard Worker 
cfl_predict_lbd_c(const int16_t * ac_buf_q3,uint8_t * dst,int dst_stride,int alpha_q3,int width,int height)147*77c1e3ccSAndroid Build Coastguard Worker static inline void cfl_predict_lbd_c(const int16_t *ac_buf_q3, uint8_t *dst,
148*77c1e3ccSAndroid Build Coastguard Worker                                      int dst_stride, int alpha_q3, int width,
149*77c1e3ccSAndroid Build Coastguard Worker                                      int height) {
150*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
151*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i++) {
152*77c1e3ccSAndroid Build Coastguard Worker       dst[i] = clip_pixel(get_scaled_luma_q0(alpha_q3, ac_buf_q3[i]) + dst[i]);
153*77c1e3ccSAndroid Build Coastguard Worker     }
154*77c1e3ccSAndroid Build Coastguard Worker     dst += dst_stride;
155*77c1e3ccSAndroid Build Coastguard Worker     ac_buf_q3 += CFL_BUF_LINE;
156*77c1e3ccSAndroid Build Coastguard Worker   }
157*77c1e3ccSAndroid Build Coastguard Worker }
158*77c1e3ccSAndroid Build Coastguard Worker 
CFL_PREDICT_FN(c,lbd)159*77c1e3ccSAndroid Build Coastguard Worker CFL_PREDICT_FN(c, lbd)
160*77c1e3ccSAndroid Build Coastguard Worker 
161*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
162*77c1e3ccSAndroid Build Coastguard Worker static inline void cfl_predict_hbd_c(const int16_t *ac_buf_q3, uint16_t *dst,
163*77c1e3ccSAndroid Build Coastguard Worker                                      int dst_stride, int alpha_q3,
164*77c1e3ccSAndroid Build Coastguard Worker                                      int bit_depth, int width, int height) {
165*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
166*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i++) {
167*77c1e3ccSAndroid Build Coastguard Worker       dst[i] = clip_pixel_highbd(
168*77c1e3ccSAndroid Build Coastguard Worker           get_scaled_luma_q0(alpha_q3, ac_buf_q3[i]) + dst[i], bit_depth);
169*77c1e3ccSAndroid Build Coastguard Worker     }
170*77c1e3ccSAndroid Build Coastguard Worker     dst += dst_stride;
171*77c1e3ccSAndroid Build Coastguard Worker     ac_buf_q3 += CFL_BUF_LINE;
172*77c1e3ccSAndroid Build Coastguard Worker   }
173*77c1e3ccSAndroid Build Coastguard Worker }
174*77c1e3ccSAndroid Build Coastguard Worker 
CFL_PREDICT_FN(c,hbd)175*77c1e3ccSAndroid Build Coastguard Worker CFL_PREDICT_FN(c, hbd)
176*77c1e3ccSAndroid Build Coastguard Worker #endif
177*77c1e3ccSAndroid Build Coastguard Worker 
178*77c1e3ccSAndroid Build Coastguard Worker static void cfl_compute_parameters(MACROBLOCKD *const xd, TX_SIZE tx_size) {
179*77c1e3ccSAndroid Build Coastguard Worker   CFL_CTX *const cfl = &xd->cfl;
180*77c1e3ccSAndroid Build Coastguard Worker   // Do not call cfl_compute_parameters multiple time on the same values.
181*77c1e3ccSAndroid Build Coastguard Worker   assert(cfl->are_parameters_computed == 0);
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker   cfl_pad(cfl, tx_size_wide[tx_size], tx_size_high[tx_size]);
184*77c1e3ccSAndroid Build Coastguard Worker   cfl_get_subtract_average_fn(tx_size)(cfl->recon_buf_q3, cfl->ac_buf_q3);
185*77c1e3ccSAndroid Build Coastguard Worker   cfl->are_parameters_computed = 1;
186*77c1e3ccSAndroid Build Coastguard Worker }
187*77c1e3ccSAndroid Build Coastguard Worker 
av1_cfl_predict_block(MACROBLOCKD * const xd,uint8_t * dst,int dst_stride,TX_SIZE tx_size,int plane)188*77c1e3ccSAndroid Build Coastguard Worker void av1_cfl_predict_block(MACROBLOCKD *const xd, uint8_t *dst, int dst_stride,
189*77c1e3ccSAndroid Build Coastguard Worker                            TX_SIZE tx_size, int plane) {
190*77c1e3ccSAndroid Build Coastguard Worker   CFL_CTX *const cfl = &xd->cfl;
191*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
192*77c1e3ccSAndroid Build Coastguard Worker   assert(is_cfl_allowed(xd));
193*77c1e3ccSAndroid Build Coastguard Worker 
194*77c1e3ccSAndroid Build Coastguard Worker   if (!cfl->are_parameters_computed) cfl_compute_parameters(xd, tx_size);
195*77c1e3ccSAndroid Build Coastguard Worker 
196*77c1e3ccSAndroid Build Coastguard Worker   const int alpha_q3 =
197*77c1e3ccSAndroid Build Coastguard Worker       cfl_idx_to_alpha(mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, plane - 1);
198*77c1e3ccSAndroid Build Coastguard Worker   assert((tx_size_high[tx_size] - 1) * CFL_BUF_LINE + tx_size_wide[tx_size] <=
199*77c1e3ccSAndroid Build Coastguard Worker          CFL_BUF_SQUARE);
200*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
201*77c1e3ccSAndroid Build Coastguard Worker   if (is_cur_buf_hbd(xd)) {
202*77c1e3ccSAndroid Build Coastguard Worker     uint16_t *dst_16 = CONVERT_TO_SHORTPTR(dst);
203*77c1e3ccSAndroid Build Coastguard Worker     cfl_get_predict_hbd_fn(tx_size)(cfl->ac_buf_q3, dst_16, dst_stride,
204*77c1e3ccSAndroid Build Coastguard Worker                                     alpha_q3, xd->bd);
205*77c1e3ccSAndroid Build Coastguard Worker     return;
206*77c1e3ccSAndroid Build Coastguard Worker   }
207*77c1e3ccSAndroid Build Coastguard Worker #endif
208*77c1e3ccSAndroid Build Coastguard Worker   cfl_get_predict_lbd_fn(tx_size)(cfl->ac_buf_q3, dst, dst_stride, alpha_q3);
209*77c1e3ccSAndroid Build Coastguard Worker }
210*77c1e3ccSAndroid Build Coastguard Worker 
cfl_luma_subsampling_420_lbd_c(const uint8_t * input,int input_stride,uint16_t * output_q3,int width,int height)211*77c1e3ccSAndroid Build Coastguard Worker static void cfl_luma_subsampling_420_lbd_c(const uint8_t *input,
212*77c1e3ccSAndroid Build Coastguard Worker                                            int input_stride,
213*77c1e3ccSAndroid Build Coastguard Worker                                            uint16_t *output_q3, int width,
214*77c1e3ccSAndroid Build Coastguard Worker                                            int height) {
215*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j += 2) {
216*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i += 2) {
217*77c1e3ccSAndroid Build Coastguard Worker       const int bot = i + input_stride;
218*77c1e3ccSAndroid Build Coastguard Worker       output_q3[i >> 1] =
219*77c1e3ccSAndroid Build Coastguard Worker           (input[i] + input[i + 1] + input[bot] + input[bot + 1]) << 1;
220*77c1e3ccSAndroid Build Coastguard Worker     }
221*77c1e3ccSAndroid Build Coastguard Worker     input += input_stride << 1;
222*77c1e3ccSAndroid Build Coastguard Worker     output_q3 += CFL_BUF_LINE;
223*77c1e3ccSAndroid Build Coastguard Worker   }
224*77c1e3ccSAndroid Build Coastguard Worker }
225*77c1e3ccSAndroid Build Coastguard Worker 
cfl_luma_subsampling_422_lbd_c(const uint8_t * input,int input_stride,uint16_t * output_q3,int width,int height)226*77c1e3ccSAndroid Build Coastguard Worker static void cfl_luma_subsampling_422_lbd_c(const uint8_t *input,
227*77c1e3ccSAndroid Build Coastguard Worker                                            int input_stride,
228*77c1e3ccSAndroid Build Coastguard Worker                                            uint16_t *output_q3, int width,
229*77c1e3ccSAndroid Build Coastguard Worker                                            int height) {
230*77c1e3ccSAndroid Build Coastguard Worker   assert((height - 1) * CFL_BUF_LINE + width <= CFL_BUF_SQUARE);
231*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
232*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i += 2) {
233*77c1e3ccSAndroid Build Coastguard Worker       output_q3[i >> 1] = (input[i] + input[i + 1]) << 2;
234*77c1e3ccSAndroid Build Coastguard Worker     }
235*77c1e3ccSAndroid Build Coastguard Worker     input += input_stride;
236*77c1e3ccSAndroid Build Coastguard Worker     output_q3 += CFL_BUF_LINE;
237*77c1e3ccSAndroid Build Coastguard Worker   }
238*77c1e3ccSAndroid Build Coastguard Worker }
239*77c1e3ccSAndroid Build Coastguard Worker 
cfl_luma_subsampling_444_lbd_c(const uint8_t * input,int input_stride,uint16_t * output_q3,int width,int height)240*77c1e3ccSAndroid Build Coastguard Worker static void cfl_luma_subsampling_444_lbd_c(const uint8_t *input,
241*77c1e3ccSAndroid Build Coastguard Worker                                            int input_stride,
242*77c1e3ccSAndroid Build Coastguard Worker                                            uint16_t *output_q3, int width,
243*77c1e3ccSAndroid Build Coastguard Worker                                            int height) {
244*77c1e3ccSAndroid Build Coastguard Worker   assert((height - 1) * CFL_BUF_LINE + width <= CFL_BUF_SQUARE);
245*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
246*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i++) {
247*77c1e3ccSAndroid Build Coastguard Worker       output_q3[i] = input[i] << 3;
248*77c1e3ccSAndroid Build Coastguard Worker     }
249*77c1e3ccSAndroid Build Coastguard Worker     input += input_stride;
250*77c1e3ccSAndroid Build Coastguard Worker     output_q3 += CFL_BUF_LINE;
251*77c1e3ccSAndroid Build Coastguard Worker   }
252*77c1e3ccSAndroid Build Coastguard Worker }
253*77c1e3ccSAndroid Build Coastguard Worker 
254*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
cfl_luma_subsampling_420_hbd_c(const uint16_t * input,int input_stride,uint16_t * output_q3,int width,int height)255*77c1e3ccSAndroid Build Coastguard Worker static void cfl_luma_subsampling_420_hbd_c(const uint16_t *input,
256*77c1e3ccSAndroid Build Coastguard Worker                                            int input_stride,
257*77c1e3ccSAndroid Build Coastguard Worker                                            uint16_t *output_q3, int width,
258*77c1e3ccSAndroid Build Coastguard Worker                                            int height) {
259*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j += 2) {
260*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i += 2) {
261*77c1e3ccSAndroid Build Coastguard Worker       const int bot = i + input_stride;
262*77c1e3ccSAndroid Build Coastguard Worker       output_q3[i >> 1] =
263*77c1e3ccSAndroid Build Coastguard Worker           (input[i] + input[i + 1] + input[bot] + input[bot + 1]) << 1;
264*77c1e3ccSAndroid Build Coastguard Worker     }
265*77c1e3ccSAndroid Build Coastguard Worker     input += input_stride << 1;
266*77c1e3ccSAndroid Build Coastguard Worker     output_q3 += CFL_BUF_LINE;
267*77c1e3ccSAndroid Build Coastguard Worker   }
268*77c1e3ccSAndroid Build Coastguard Worker }
269*77c1e3ccSAndroid Build Coastguard Worker 
cfl_luma_subsampling_422_hbd_c(const uint16_t * input,int input_stride,uint16_t * output_q3,int width,int height)270*77c1e3ccSAndroid Build Coastguard Worker static void cfl_luma_subsampling_422_hbd_c(const uint16_t *input,
271*77c1e3ccSAndroid Build Coastguard Worker                                            int input_stride,
272*77c1e3ccSAndroid Build Coastguard Worker                                            uint16_t *output_q3, int width,
273*77c1e3ccSAndroid Build Coastguard Worker                                            int height) {
274*77c1e3ccSAndroid Build Coastguard Worker   assert((height - 1) * CFL_BUF_LINE + width <= CFL_BUF_SQUARE);
275*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
276*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i += 2) {
277*77c1e3ccSAndroid Build Coastguard Worker       output_q3[i >> 1] = (input[i] + input[i + 1]) << 2;
278*77c1e3ccSAndroid Build Coastguard Worker     }
279*77c1e3ccSAndroid Build Coastguard Worker     input += input_stride;
280*77c1e3ccSAndroid Build Coastguard Worker     output_q3 += CFL_BUF_LINE;
281*77c1e3ccSAndroid Build Coastguard Worker   }
282*77c1e3ccSAndroid Build Coastguard Worker }
283*77c1e3ccSAndroid Build Coastguard Worker 
cfl_luma_subsampling_444_hbd_c(const uint16_t * input,int input_stride,uint16_t * output_q3,int width,int height)284*77c1e3ccSAndroid Build Coastguard Worker static void cfl_luma_subsampling_444_hbd_c(const uint16_t *input,
285*77c1e3ccSAndroid Build Coastguard Worker                                            int input_stride,
286*77c1e3ccSAndroid Build Coastguard Worker                                            uint16_t *output_q3, int width,
287*77c1e3ccSAndroid Build Coastguard Worker                                            int height) {
288*77c1e3ccSAndroid Build Coastguard Worker   assert((height - 1) * CFL_BUF_LINE + width <= CFL_BUF_SQUARE);
289*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < height; j++) {
290*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < width; i++) {
291*77c1e3ccSAndroid Build Coastguard Worker       output_q3[i] = input[i] << 3;
292*77c1e3ccSAndroid Build Coastguard Worker     }
293*77c1e3ccSAndroid Build Coastguard Worker     input += input_stride;
294*77c1e3ccSAndroid Build Coastguard Worker     output_q3 += CFL_BUF_LINE;
295*77c1e3ccSAndroid Build Coastguard Worker   }
296*77c1e3ccSAndroid Build Coastguard Worker }
297*77c1e3ccSAndroid Build Coastguard Worker #endif
298*77c1e3ccSAndroid Build Coastguard Worker 
CFL_GET_SUBSAMPLE_FUNCTION(c)299*77c1e3ccSAndroid Build Coastguard Worker CFL_GET_SUBSAMPLE_FUNCTION(c)
300*77c1e3ccSAndroid Build Coastguard Worker 
301*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
302*77c1e3ccSAndroid Build Coastguard Worker static inline cfl_subsample_hbd_fn cfl_subsampling_hbd(TX_SIZE tx_size,
303*77c1e3ccSAndroid Build Coastguard Worker                                                        int sub_x, int sub_y) {
304*77c1e3ccSAndroid Build Coastguard Worker   if (sub_x == 1) {
305*77c1e3ccSAndroid Build Coastguard Worker     if (sub_y == 1) {
306*77c1e3ccSAndroid Build Coastguard Worker       return cfl_get_luma_subsampling_420_hbd(tx_size);
307*77c1e3ccSAndroid Build Coastguard Worker     }
308*77c1e3ccSAndroid Build Coastguard Worker     return cfl_get_luma_subsampling_422_hbd(tx_size);
309*77c1e3ccSAndroid Build Coastguard Worker   }
310*77c1e3ccSAndroid Build Coastguard Worker   return cfl_get_luma_subsampling_444_hbd(tx_size);
311*77c1e3ccSAndroid Build Coastguard Worker }
312*77c1e3ccSAndroid Build Coastguard Worker #endif
313*77c1e3ccSAndroid Build Coastguard Worker 
cfl_subsampling_lbd(TX_SIZE tx_size,int sub_x,int sub_y)314*77c1e3ccSAndroid Build Coastguard Worker static inline cfl_subsample_lbd_fn cfl_subsampling_lbd(TX_SIZE tx_size,
315*77c1e3ccSAndroid Build Coastguard Worker                                                        int sub_x, int sub_y) {
316*77c1e3ccSAndroid Build Coastguard Worker   if (sub_x == 1) {
317*77c1e3ccSAndroid Build Coastguard Worker     if (sub_y == 1) {
318*77c1e3ccSAndroid Build Coastguard Worker       return cfl_get_luma_subsampling_420_lbd(tx_size);
319*77c1e3ccSAndroid Build Coastguard Worker     }
320*77c1e3ccSAndroid Build Coastguard Worker     return cfl_get_luma_subsampling_422_lbd(tx_size);
321*77c1e3ccSAndroid Build Coastguard Worker   }
322*77c1e3ccSAndroid Build Coastguard Worker   return cfl_get_luma_subsampling_444_lbd(tx_size);
323*77c1e3ccSAndroid Build Coastguard Worker }
324*77c1e3ccSAndroid Build Coastguard Worker 
cfl_store(CFL_CTX * cfl,const uint8_t * input,int input_stride,int row,int col,TX_SIZE tx_size,int use_hbd)325*77c1e3ccSAndroid Build Coastguard Worker static void cfl_store(CFL_CTX *cfl, const uint8_t *input, int input_stride,
326*77c1e3ccSAndroid Build Coastguard Worker                       int row, int col, TX_SIZE tx_size, int use_hbd) {
327*77c1e3ccSAndroid Build Coastguard Worker   const int width = tx_size_wide[tx_size];
328*77c1e3ccSAndroid Build Coastguard Worker   const int height = tx_size_high[tx_size];
329*77c1e3ccSAndroid Build Coastguard Worker   const int tx_off_log2 = MI_SIZE_LOG2;
330*77c1e3ccSAndroid Build Coastguard Worker   const int sub_x = cfl->subsampling_x;
331*77c1e3ccSAndroid Build Coastguard Worker   const int sub_y = cfl->subsampling_y;
332*77c1e3ccSAndroid Build Coastguard Worker   const int store_row = row << (tx_off_log2 - sub_y);
333*77c1e3ccSAndroid Build Coastguard Worker   const int store_col = col << (tx_off_log2 - sub_x);
334*77c1e3ccSAndroid Build Coastguard Worker   const int store_height = height >> sub_y;
335*77c1e3ccSAndroid Build Coastguard Worker   const int store_width = width >> sub_x;
336*77c1e3ccSAndroid Build Coastguard Worker 
337*77c1e3ccSAndroid Build Coastguard Worker   // Invalidate current parameters
338*77c1e3ccSAndroid Build Coastguard Worker   cfl->are_parameters_computed = 0;
339*77c1e3ccSAndroid Build Coastguard Worker 
340*77c1e3ccSAndroid Build Coastguard Worker   // Store the surface of the pixel buffer that was written to, this way we
341*77c1e3ccSAndroid Build Coastguard Worker   // can manage chroma overrun (e.g. when the chroma surfaces goes beyond the
342*77c1e3ccSAndroid Build Coastguard Worker   // frame boundary)
343*77c1e3ccSAndroid Build Coastguard Worker   if (col == 0 && row == 0) {
344*77c1e3ccSAndroid Build Coastguard Worker     cfl->buf_width = store_width;
345*77c1e3ccSAndroid Build Coastguard Worker     cfl->buf_height = store_height;
346*77c1e3ccSAndroid Build Coastguard Worker   } else {
347*77c1e3ccSAndroid Build Coastguard Worker     cfl->buf_width = OD_MAXI(store_col + store_width, cfl->buf_width);
348*77c1e3ccSAndroid Build Coastguard Worker     cfl->buf_height = OD_MAXI(store_row + store_height, cfl->buf_height);
349*77c1e3ccSAndroid Build Coastguard Worker   }
350*77c1e3ccSAndroid Build Coastguard Worker 
351*77c1e3ccSAndroid Build Coastguard Worker   // Check that we will remain inside the pixel buffer.
352*77c1e3ccSAndroid Build Coastguard Worker   assert(store_row + store_height <= CFL_BUF_LINE);
353*77c1e3ccSAndroid Build Coastguard Worker   assert(store_col + store_width <= CFL_BUF_LINE);
354*77c1e3ccSAndroid Build Coastguard Worker 
355*77c1e3ccSAndroid Build Coastguard Worker   // Store the input into the CfL pixel buffer
356*77c1e3ccSAndroid Build Coastguard Worker   uint16_t *recon_buf_q3 =
357*77c1e3ccSAndroid Build Coastguard Worker       cfl->recon_buf_q3 + (store_row * CFL_BUF_LINE + store_col);
358*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
359*77c1e3ccSAndroid Build Coastguard Worker   if (use_hbd) {
360*77c1e3ccSAndroid Build Coastguard Worker     cfl_subsampling_hbd(tx_size, sub_x, sub_y)(CONVERT_TO_SHORTPTR(input),
361*77c1e3ccSAndroid Build Coastguard Worker                                                input_stride, recon_buf_q3);
362*77c1e3ccSAndroid Build Coastguard Worker   } else {
363*77c1e3ccSAndroid Build Coastguard Worker     cfl_subsampling_lbd(tx_size, sub_x, sub_y)(input, input_stride,
364*77c1e3ccSAndroid Build Coastguard Worker                                                recon_buf_q3);
365*77c1e3ccSAndroid Build Coastguard Worker   }
366*77c1e3ccSAndroid Build Coastguard Worker #else
367*77c1e3ccSAndroid Build Coastguard Worker   (void)use_hbd;
368*77c1e3ccSAndroid Build Coastguard Worker   cfl_subsampling_lbd(tx_size, sub_x, sub_y)(input, input_stride, recon_buf_q3);
369*77c1e3ccSAndroid Build Coastguard Worker #endif
370*77c1e3ccSAndroid Build Coastguard Worker }
371*77c1e3ccSAndroid Build Coastguard Worker 
372*77c1e3ccSAndroid Build Coastguard Worker // Adjust the row and column of blocks smaller than 8X8, as chroma-referenced
373*77c1e3ccSAndroid Build Coastguard Worker // and non-chroma-referenced blocks are stored together in the CfL buffer.
sub8x8_adjust_offset(const CFL_CTX * cfl,int mi_row,int mi_col,int * row_out,int * col_out)374*77c1e3ccSAndroid Build Coastguard Worker static inline void sub8x8_adjust_offset(const CFL_CTX *cfl, int mi_row,
375*77c1e3ccSAndroid Build Coastguard Worker                                         int mi_col, int *row_out,
376*77c1e3ccSAndroid Build Coastguard Worker                                         int *col_out) {
377*77c1e3ccSAndroid Build Coastguard Worker   // Increment row index for bottom: 8x4, 16x4 or both bottom 4x4s.
378*77c1e3ccSAndroid Build Coastguard Worker   if ((mi_row & 0x01) && cfl->subsampling_y) {
379*77c1e3ccSAndroid Build Coastguard Worker     assert(*row_out == 0);
380*77c1e3ccSAndroid Build Coastguard Worker     (*row_out)++;
381*77c1e3ccSAndroid Build Coastguard Worker   }
382*77c1e3ccSAndroid Build Coastguard Worker 
383*77c1e3ccSAndroid Build Coastguard Worker   // Increment col index for right: 4x8, 4x16 or both right 4x4s.
384*77c1e3ccSAndroid Build Coastguard Worker   if ((mi_col & 0x01) && cfl->subsampling_x) {
385*77c1e3ccSAndroid Build Coastguard Worker     assert(*col_out == 0);
386*77c1e3ccSAndroid Build Coastguard Worker     (*col_out)++;
387*77c1e3ccSAndroid Build Coastguard Worker   }
388*77c1e3ccSAndroid Build Coastguard Worker }
389*77c1e3ccSAndroid Build Coastguard Worker 
cfl_store_tx(MACROBLOCKD * const xd,int row,int col,TX_SIZE tx_size,BLOCK_SIZE bsize)390*77c1e3ccSAndroid Build Coastguard Worker void cfl_store_tx(MACROBLOCKD *const xd, int row, int col, TX_SIZE tx_size,
391*77c1e3ccSAndroid Build Coastguard Worker                   BLOCK_SIZE bsize) {
392*77c1e3ccSAndroid Build Coastguard Worker   CFL_CTX *const cfl = &xd->cfl;
393*77c1e3ccSAndroid Build Coastguard Worker   struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
394*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *dst = &pd->dst.buf[(row * pd->dst.stride + col) << MI_SIZE_LOG2];
395*77c1e3ccSAndroid Build Coastguard Worker 
396*77c1e3ccSAndroid Build Coastguard Worker   if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) {
397*77c1e3ccSAndroid Build Coastguard Worker     // Only dimensions of size 4 can have an odd offset.
398*77c1e3ccSAndroid Build Coastguard Worker     assert(!((col & 1) && tx_size_wide[tx_size] != 4));
399*77c1e3ccSAndroid Build Coastguard Worker     assert(!((row & 1) && tx_size_high[tx_size] != 4));
400*77c1e3ccSAndroid Build Coastguard Worker     sub8x8_adjust_offset(cfl, xd->mi_row, xd->mi_col, &row, &col);
401*77c1e3ccSAndroid Build Coastguard Worker   }
402*77c1e3ccSAndroid Build Coastguard Worker   cfl_store(cfl, dst, pd->dst.stride, row, col, tx_size, is_cur_buf_hbd(xd));
403*77c1e3ccSAndroid Build Coastguard Worker }
404*77c1e3ccSAndroid Build Coastguard Worker 
max_intra_block_width(const MACROBLOCKD * xd,BLOCK_SIZE plane_bsize,int plane,TX_SIZE tx_size)405*77c1e3ccSAndroid Build Coastguard Worker static inline int max_intra_block_width(const MACROBLOCKD *xd,
406*77c1e3ccSAndroid Build Coastguard Worker                                         BLOCK_SIZE plane_bsize, int plane,
407*77c1e3ccSAndroid Build Coastguard Worker                                         TX_SIZE tx_size) {
408*77c1e3ccSAndroid Build Coastguard Worker   const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane)
409*77c1e3ccSAndroid Build Coastguard Worker                               << MI_SIZE_LOG2;
410*77c1e3ccSAndroid Build Coastguard Worker   return ALIGN_POWER_OF_TWO(max_blocks_wide, tx_size_wide_log2[tx_size]);
411*77c1e3ccSAndroid Build Coastguard Worker }
412*77c1e3ccSAndroid Build Coastguard Worker 
max_intra_block_height(const MACROBLOCKD * xd,BLOCK_SIZE plane_bsize,int plane,TX_SIZE tx_size)413*77c1e3ccSAndroid Build Coastguard Worker static inline int max_intra_block_height(const MACROBLOCKD *xd,
414*77c1e3ccSAndroid Build Coastguard Worker                                          BLOCK_SIZE plane_bsize, int plane,
415*77c1e3ccSAndroid Build Coastguard Worker                                          TX_SIZE tx_size) {
416*77c1e3ccSAndroid Build Coastguard Worker   const int max_blocks_high = max_block_high(xd, plane_bsize, plane)
417*77c1e3ccSAndroid Build Coastguard Worker                               << MI_SIZE_LOG2;
418*77c1e3ccSAndroid Build Coastguard Worker   return ALIGN_POWER_OF_TWO(max_blocks_high, tx_size_high_log2[tx_size]);
419*77c1e3ccSAndroid Build Coastguard Worker }
420*77c1e3ccSAndroid Build Coastguard Worker 
cfl_store_block(MACROBLOCKD * const xd,BLOCK_SIZE bsize,TX_SIZE tx_size)421*77c1e3ccSAndroid Build Coastguard Worker void cfl_store_block(MACROBLOCKD *const xd, BLOCK_SIZE bsize, TX_SIZE tx_size) {
422*77c1e3ccSAndroid Build Coastguard Worker   CFL_CTX *const cfl = &xd->cfl;
423*77c1e3ccSAndroid Build Coastguard Worker   struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
424*77c1e3ccSAndroid Build Coastguard Worker   int row = 0;
425*77c1e3ccSAndroid Build Coastguard Worker   int col = 0;
426*77c1e3ccSAndroid Build Coastguard Worker 
427*77c1e3ccSAndroid Build Coastguard Worker   if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) {
428*77c1e3ccSAndroid Build Coastguard Worker     sub8x8_adjust_offset(cfl, xd->mi_row, xd->mi_col, &row, &col);
429*77c1e3ccSAndroid Build Coastguard Worker   }
430*77c1e3ccSAndroid Build Coastguard Worker   const int width = max_intra_block_width(xd, bsize, AOM_PLANE_Y, tx_size);
431*77c1e3ccSAndroid Build Coastguard Worker   const int height = max_intra_block_height(xd, bsize, AOM_PLANE_Y, tx_size);
432*77c1e3ccSAndroid Build Coastguard Worker   tx_size = get_tx_size(width, height);
433*77c1e3ccSAndroid Build Coastguard Worker   cfl_store(cfl, pd->dst.buf, pd->dst.stride, row, col, tx_size,
434*77c1e3ccSAndroid Build Coastguard Worker             is_cur_buf_hbd(xd));
435*77c1e3ccSAndroid Build Coastguard Worker }
436