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