Coverage Report

Created: 2023-06-07 06:31

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