Coverage Report

Created: 2025-10-28 07:26

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