cfl.c 24.2 KB
Newer Older
Luc Trudeau's avatar
Luc Trudeau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include "av1/common/cfl.h"
#include "av1/common/common_data.h"
14
15
#include "av1/common/onyxc_int.h"

16
void cfl_init(CFL_CTX *cfl, AV1_COMMON *cm) {
Luc Trudeau's avatar
Luc Trudeau committed
17
18
19
20
21
22
23
  if ((cm->subsampling_x != 0 && cm->subsampling_x != 1) ||
      (cm->subsampling_y != 0 && cm->subsampling_y != 1)) {
    aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
                       "Only 4:4:4, 4:4:0, 4:2:2 and 4:2:0 are currently "
                       "supported by CfL, %d %d "
                       "subsampling is not supported.\n",
                       cm->subsampling_x, cm->subsampling_y);
24
  }
Luc Trudeau's avatar
Luc Trudeau committed
25
  memset(&cfl->pred_buf_q3, 0, sizeof(cfl->pred_buf_q3));
26
27
  cfl->subsampling_x = cm->subsampling_x;
  cfl->subsampling_y = cm->subsampling_y;
Luc Trudeau's avatar
Luc Trudeau committed
28
  cfl->are_parameters_computed = 0;
Luc Trudeau's avatar
Luc Trudeau committed
29
  cfl->store_y = 0;
30
#if CONFIG_DEBUG
Luc Trudeau's avatar
Luc Trudeau committed
31
  cfl_clear_sub8x8_val(cfl);
32
33
  cfl->store_counter = 0;
  cfl->last_compute_counter = 0;
34
#endif  // CONFIG_DEBUG
Luc Trudeau's avatar
Luc Trudeau committed
35
36
}

Luc Trudeau's avatar
Luc Trudeau committed
37
38
39
40
41
42
// Due to frame boundary issues, it is possible that the total area covered by
// chroma exceeds that of luma. When this happens, we fill the missing pixels by
// repeating the last columns and/or rows.
static INLINE void cfl_pad(CFL_CTX *cfl, int width, int height) {
  const int diff_width = width - cfl->buf_width;
  const int diff_height = height - cfl->buf_height;
Luc Trudeau's avatar
Luc Trudeau committed
43
44

  if (diff_width > 0) {
Luc Trudeau's avatar
Luc Trudeau committed
45
46
47
    const int min_height = height - diff_height;
    int16_t *pred_buf_q3 = cfl->pred_buf_q3 + (width - diff_width);
    for (int j = 0; j < min_height; j++) {
48
      const int16_t last_pixel = pred_buf_q3[-1];
Luc Trudeau's avatar
Luc Trudeau committed
49
      for (int i = 0; i < diff_width; i++) {
Luc Trudeau's avatar
Luc Trudeau committed
50
        pred_buf_q3[i] = last_pixel;
Luc Trudeau's avatar
Luc Trudeau committed
51
      }
Luc Trudeau's avatar
Luc Trudeau committed
52
      pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau's avatar
Luc Trudeau committed
53
    }
Luc Trudeau's avatar
Luc Trudeau committed
54
    cfl->buf_width = width;
Luc Trudeau's avatar
Luc Trudeau committed
55
56
  }
  if (diff_height > 0) {
Luc Trudeau's avatar
Luc Trudeau committed
57
58
    int16_t *pred_buf_q3 =
        cfl->pred_buf_q3 + ((height - diff_height) * MAX_SB_SIZE);
Luc Trudeau's avatar
Luc Trudeau committed
59
    for (int j = 0; j < diff_height; j++) {
Luc Trudeau's avatar
Luc Trudeau committed
60
      const int16_t *last_row_q3 = pred_buf_q3 - MAX_SB_SIZE;
Luc Trudeau's avatar
Luc Trudeau committed
61
      for (int i = 0; i < width; i++) {
Luc Trudeau's avatar
Luc Trudeau committed
62
        pred_buf_q3[i] = last_row_q3[i];
Luc Trudeau's avatar
Luc Trudeau committed
63
      }
Luc Trudeau's avatar
Luc Trudeau committed
64
      pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau's avatar
Luc Trudeau committed
65
    }
Luc Trudeau's avatar
Luc Trudeau committed
66
    cfl->buf_height = height;
Luc Trudeau's avatar
Luc Trudeau committed
67
  }
68
}
Luc Trudeau's avatar
Luc Trudeau committed
69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
static void sum_above_row_lbd(const uint8_t *above_u, const uint8_t *above_v,
                              int width, int *out_sum_u, int *out_sum_v) {
  int sum_u = 0;
  int sum_v = 0;
  for (int i = 0; i < width; i++) {
    sum_u += above_u[i];
    sum_v += above_v[i];
  }
  *out_sum_u += sum_u;
  *out_sum_v += sum_v;
}
#if CONFIG_HIGHBITDEPTH
static void sum_above_row_hbd(const uint16_t *above_u, const uint16_t *above_v,
                              int width, int *out_sum_u, int *out_sum_v) {
84
85
86
  int sum_u = 0;
  int sum_v = 0;
  for (int i = 0; i < width; i++) {
87
88
    sum_u += above_u[i];
    sum_v += above_v[i];
89
90
91
92
  }
  *out_sum_u += sum_u;
  *out_sum_v += sum_v;
}
93
#endif  // CONFIG_HIGHBITDEPTH
94

95
96
static void sum_above_row(const MACROBLOCKD *xd, int width, int *out_sum_u,
                          int *out_sum_v) {
97
98
  const struct macroblockd_plane *const pd_u = &xd->plane[AOM_PLANE_U];
  const struct macroblockd_plane *const pd_v = &xd->plane[AOM_PLANE_V];
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#if CONFIG_HIGHBITDEPTH
  if (get_bitdepth_data_path_index(xd)) {
    const uint16_t *above_u_16 =
        CONVERT_TO_SHORTPTR(pd_u->dst.buf) - pd_u->dst.stride;
    const uint16_t *above_v_16 =
        CONVERT_TO_SHORTPTR(pd_v->dst.buf) - pd_v->dst.stride;
    sum_above_row_hbd(above_u_16, above_v_16, width, out_sum_u, out_sum_v);
    return;
  }
#endif  // CONFIG_HIGHBITDEPTH
  const uint8_t *above_u = pd_u->dst.buf - pd_u->dst.stride;
  const uint8_t *above_v = pd_v->dst.buf - pd_v->dst.stride;
  sum_above_row_lbd(above_u, above_v, width, out_sum_u, out_sum_v);
}
Luc Trudeau's avatar
Luc Trudeau committed
113

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
static void sum_left_col_lbd(const uint8_t *left_u, int u_stride,
                             const uint8_t *left_v, int v_stride, int height,
                             int *out_sum_u, int *out_sum_v) {
  int sum_u = 0;
  int sum_v = 0;
  for (int i = 0; i < height; i++) {
    sum_u += left_u[i * u_stride];
    sum_v += left_v[i * v_stride];
  }
  *out_sum_u += sum_u;
  *out_sum_v += sum_v;
}
#if CONFIG_HIGHBITDEPTH
static void sum_left_col_hbd(const uint16_t *left_u, int u_stride,
                             const uint16_t *left_v, int v_stride, int height,
                             int *out_sum_u, int *out_sum_v) {
130
131
132
  int sum_u = 0;
  int sum_v = 0;
  for (int i = 0; i < height; i++) {
133
134
    sum_u += left_u[i * u_stride];
    sum_v += left_v[i * v_stride];
135
136
137
138
  }
  *out_sum_u += sum_u;
  *out_sum_v += sum_v;
}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#endif  // CONFIG_HIGHBITDEPTH
static void sum_left_col(const MACROBLOCKD *xd, int height, int *out_sum_u,
                         int *out_sum_v) {
  const struct macroblockd_plane *const pd_u = &xd->plane[AOM_PLANE_U];
  const struct macroblockd_plane *const pd_v = &xd->plane[AOM_PLANE_V];

#if CONFIG_HIGHBITDEPTH
  if (get_bitdepth_data_path_index(xd)) {
    const uint16_t *left_u_16 = CONVERT_TO_SHORTPTR(pd_u->dst.buf) - 1;
    const uint16_t *left_v_16 = CONVERT_TO_SHORTPTR(pd_v->dst.buf) - 1;
    sum_left_col_hbd(left_u_16, pd_u->dst.stride, left_v_16, pd_v->dst.stride,
                     height, out_sum_u, out_sum_v);
    return;
  }
#endif  // CONFIG_HIGHBITDEPTH
  const uint8_t *left_u = pd_u->dst.buf - 1;
  const uint8_t *left_v = pd_v->dst.buf - 1;
  sum_left_col_lbd(left_u, pd_u->dst.stride, left_v, pd_v->dst.stride, height,
                   out_sum_u, out_sum_v);
}
159
160
161
162

// CfL computes its own block-level DC_PRED. This is required to compute both
// alpha_cb and alpha_cr before the prediction are computed.
static void cfl_dc_pred(MACROBLOCKD *xd, BLOCK_SIZE plane_bsize) {
Luc Trudeau's avatar
Luc Trudeau committed
163
  CFL_CTX *const cfl = xd->cfl;
164
165
166
167
168
169
170

  // Compute DC_PRED until block boundary. We can't assume the neighbor will use
  // the same transform size.
  const int width = max_block_wide(xd, plane_bsize, AOM_PLANE_U)
                    << tx_size_wide_log2[0];
  const int height = max_block_high(xd, plane_bsize, AOM_PLANE_U)
                     << tx_size_high_log2[0];
171
  // Number of pixel on the top and left borders.
172
  const int num_pel = width + height;
Luc Trudeau's avatar
Luc Trudeau committed
173

174
175
  int sum_u = 0;
  int sum_v = 0;
Luc Trudeau's avatar
Luc Trudeau committed
176

177
178
179
180
181
182
183
184
  // Match behavior of build_intra_predictors_high (reconintra.c) at superblock
  // boundaries:
  // base-1 base-1 base-1 .. base-1 base-1 base-1 base-1 base-1 base-1
  // base+1   A      B  ..     Y      Z
  // base+1   C      D  ..     W      X
  // base+1   E      F  ..     U      V
  // base+1   G      H  ..     S      T      T      T      T      T
  // ..
Luc Trudeau's avatar
Luc Trudeau committed
185

186
  if (xd->chroma_up_available && xd->mb_to_right_edge >= 0) {
187
    sum_above_row(xd, width, &sum_u, &sum_v);
Luc Trudeau's avatar
Luc Trudeau committed
188
  } else {
189
    const int base = 128 << (xd->bd - 8);
190
191
    sum_u = width * (base - 1);
    sum_v = width * (base - 1);
Luc Trudeau's avatar
Luc Trudeau committed
192
193
  }

194
  if (xd->chroma_left_available && xd->mb_to_bottom_edge >= 0) {
195
    sum_left_col(xd, height, &sum_u, &sum_v);
Luc Trudeau's avatar
Luc Trudeau committed
196
  } else {
197
    const int base = 128 << (xd->bd - 8);
198
199
    sum_u += height * (base + 1);
    sum_v += height * (base + 1);
Luc Trudeau's avatar
Luc Trudeau committed
200
201
  }

202
203
  // TODO(ltrudeau) Because of max_block_wide and max_block_high, num_pel will
  // not be a power of two. So these divisions will have to use a lookup table.
Luc Trudeau's avatar
Luc Trudeau committed
204
205
  cfl->dc_pred[CFL_PRED_U] = (sum_u + (num_pel >> 1)) / num_pel;
  cfl->dc_pred[CFL_PRED_V] = (sum_v + (num_pel >> 1)) / num_pel;
Luc Trudeau's avatar
Luc Trudeau committed
206
207
}

208
static void cfl_subtract_averages(CFL_CTX *cfl, TX_SIZE tx_size) {
Luc Trudeau's avatar
Luc Trudeau committed
209
210
  const int width = cfl->uv_width;
  const int height = cfl->uv_height;
211
212
213
  const int tx_height = tx_size_high[tx_size];
  const int tx_width = tx_size_wide[tx_size];
  const int block_row_stride = MAX_SB_SIZE << tx_size_high_log2[tx_size];
214
215
216
  const int num_pel_log2 =
      (tx_size_high_log2[tx_size] + tx_size_wide_log2[tx_size]);

Luc Trudeau's avatar
Luc Trudeau committed
217
218
219
  int16_t *pred_buf_q3 = cfl->pred_buf_q3;

  cfl_pad(cfl, width, height);
Luc Trudeau's avatar
Luc Trudeau committed
220

221
222
  for (int b_j = 0; b_j < height; b_j += tx_height) {
    for (int b_i = 0; b_i < width; b_i += tx_width) {
Luc Trudeau's avatar
Luc Trudeau committed
223
      int sum_q3 = 0;
Luc Trudeau's avatar
Luc Trudeau committed
224
      int16_t *tx_pred_buf_q3 = pred_buf_q3;
225
226
      for (int t_j = 0; t_j < tx_height; t_j++) {
        for (int t_i = b_i; t_i < b_i + tx_width; t_i++) {
Luc Trudeau's avatar
Luc Trudeau committed
227
          sum_q3 += tx_pred_buf_q3[t_i];
228
        }
Luc Trudeau's avatar
Luc Trudeau committed
229
        tx_pred_buf_q3 += MAX_SB_SIZE;
230
      }
231
      int avg_q3 = (sum_q3 + (1 << (num_pel_log2 - 1))) >> num_pel_log2;
Luc Trudeau's avatar
Luc Trudeau committed
232
      // Loss is never more than 1/2 (in Q3)
233
      assert(abs((avg_q3 << num_pel_log2) - sum_q3) <= 1 << num_pel_log2 >> 1);
234

Luc Trudeau's avatar
Luc Trudeau committed
235
      tx_pred_buf_q3 = pred_buf_q3;
236
237
      for (int t_j = 0; t_j < tx_height; t_j++) {
        for (int t_i = b_i; t_i < b_i + tx_width; t_i++) {
Luc Trudeau's avatar
Luc Trudeau committed
238
          tx_pred_buf_q3[t_i] -= avg_q3;
239
240
        }

Luc Trudeau's avatar
Luc Trudeau committed
241
        tx_pred_buf_q3 += MAX_SB_SIZE;
242
      }
243
    }
Luc Trudeau's avatar
Luc Trudeau committed
244
    pred_buf_q3 += block_row_stride;
Luc Trudeau's avatar
Luc Trudeau committed
245
246
247
  }
}

248
static INLINE int cfl_idx_to_alpha(int alpha_idx, int joint_sign,
Luc Trudeau's avatar
Luc Trudeau committed
249
                                   CFL_PRED_TYPE pred_type) {
250
251
252
253
254
255
  const int alpha_sign = (pred_type == CFL_PRED_U) ? CFL_SIGN_U(joint_sign)
                                                   : CFL_SIGN_V(joint_sign);
  if (alpha_sign == CFL_SIGN_ZERO) return 0;
  const int abs_alpha_q3 =
      (pred_type == CFL_PRED_U) ? CFL_IDX_U(alpha_idx) : CFL_IDX_V(alpha_idx);
  return (alpha_sign == CFL_SIGN_POS) ? abs_alpha_q3 + 1 : -abs_alpha_q3 - 1;
256
257
}

258
259
static void cfl_build_prediction_lbd(const int16_t *pred_buf_q3, uint8_t *dst,
                                     int dst_stride, int width, int height,
260
                                     int alpha_q3, int16_t dc_pred) {
261
262
263
264
265
266
267
268
269
270
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      dst[i] =
          clip_pixel(get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]) + dc_pred);
    }
    dst += dst_stride;
    pred_buf_q3 += MAX_SB_SIZE;
  }
}

271
272
273
#if CONFIG_HIGHBITDEPTH
static void cfl_build_prediction_hbd(const int16_t *pred_buf_q3, uint16_t *dst,
                                     int dst_stride, int width, int height,
274
275
                                     int alpha_q3, int16_t dc_pred,
                                     int bit_depth) {
276
277
278
279
280
281
282
283
284
285
286
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      dst[i] = clip_pixel_highbd(
          get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]) + dc_pred, bit_depth);
    }
    dst += dst_stride;
    pred_buf_q3 += MAX_SB_SIZE;
  }
}
#endif  // CONFIG_HIGHBITDEPTH

Luc Trudeau's avatar
Luc Trudeau committed
287
288
289
290
291
292
293
294
void cfl_predict_block(MACROBLOCKD *const xd, uint8_t *dst, int dst_stride,
                       int row, int col, TX_SIZE tx_size, int plane) {
  CFL_CTX *const cfl = xd->cfl;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;

  // CfL parameters must be computed before prediction can be done.
  assert(cfl->are_parameters_computed == 1);

Luc Trudeau's avatar
Luc Trudeau committed
295
296
  const int16_t *pred_buf_q3 =
      cfl->pred_buf_q3 + ((row * MAX_SB_SIZE + col) << tx_size_wide_log2[0]);
297
298
  const int alpha_q3 =
      cfl_idx_to_alpha(mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, plane - 1);
Luc Trudeau's avatar
Luc Trudeau committed
299

300
301
302
303
304
305
306
307
308
309
310
311
#if CONFIG_HIGHBITDEPTH
  if (get_bitdepth_data_path_index(xd)) {
    uint16_t *dst_16 = CONVERT_TO_SHORTPTR(dst);
    cfl_build_prediction_hbd(pred_buf_q3, dst_16, dst_stride,
                             tx_size_wide[tx_size], tx_size_high[tx_size],
                             alpha_q3, cfl->dc_pred[plane - 1], xd->bd);
    return;
  }
#endif  // CONFIG_HIGHBITDEPTH
  cfl_build_prediction_lbd(pred_buf_q3, dst, dst_stride, tx_size_wide[tx_size],
                           tx_size_high[tx_size], alpha_q3,
                           cfl->dc_pred[plane - 1]);
Luc Trudeau's avatar
Luc Trudeau committed
312
313
}

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
static void cfl_luma_subsampling_420_lbd(const uint8_t *input, int input_stride,
                                         int16_t *output_q3, int width,
                                         int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      int top = i << 1;
      int bot = top + input_stride;
      output_q3[i] = (input[top] + input[top + 1] + input[bot] + input[bot + 1])
                     << 1;
    }
    input += input_stride << 1;
    output_q3 += MAX_SB_SIZE;
  }
}

Luc Trudeau's avatar
Luc Trudeau committed
329
330
331
332
333
334
335
336
337
338
339
340
341
static void cfl_luma_subsampling_422_lbd(const uint8_t *input, int input_stride,
                                         int16_t *output_q3, int width,
                                         int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      int left = i << 1;
      output_q3[i] = (input[left] + input[left + 1]) << 2;
    }
    input += input_stride;
    output_q3 += MAX_SB_SIZE;
  }
}

Luc Trudeau's avatar
Luc Trudeau committed
342
343
344
345
346
347
348
349
350
351
352
353
static void cfl_luma_subsampling_440_lbd(const uint8_t *input, int input_stride,
                                         int16_t *output_q3, int width,
                                         int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      output_q3[i] = (input[i] + input[i + input_stride]) << 2;
    }
    input += input_stride << 1;
    output_q3 += MAX_SB_SIZE;
  }
}

354
355
356
357
358
359
360
361
362
363
364
365
static void cfl_luma_subsampling_444_lbd(const uint8_t *input, int input_stride,
                                         int16_t *output_q3, int width,
                                         int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      output_q3[i] = input[i] << 3;
    }
    input += input_stride;
    output_q3 += MAX_SB_SIZE;
  }
}

366
367
368
369
370
371
372
373
374
375
typedef void (*cfl_subsample_lbd_fn)(const uint8_t *input, int input_stride,
                                     int16_t *output_q3, int width, int height);

static const cfl_subsample_lbd_fn subsample_lbd[2][2] = {
  //  (sub_y == 0, sub_x == 0)       (sub_y == 0, sub_x == 1)
  //  (sub_y == 1, sub_x == 0)       (sub_y == 1, sub_x == 1)
  { cfl_luma_subsampling_444_lbd, cfl_luma_subsampling_422_lbd },
  { cfl_luma_subsampling_440_lbd, cfl_luma_subsampling_420_lbd },
};

376
377
378
379
#if CONFIG_HIGHBITDEPTH
static void cfl_luma_subsampling_420_hbd(const uint16_t *input,
                                         int input_stride, int16_t *output_q3,
                                         int width, int height) {
Luc Trudeau's avatar
Luc Trudeau committed
380
381
382
383
384
385
386
387
388
389
390
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      int top = i << 1;
      int bot = top + input_stride;
      output_q3[i] = (input[top] + input[top + 1] + input[bot] + input[bot + 1])
                     << 1;
    }
    input += input_stride << 1;
    output_q3 += MAX_SB_SIZE;
  }
}
391

Luc Trudeau's avatar
Luc Trudeau committed
392
393
394
395
396
397
398
399
400
401
402
403
404
static void cfl_luma_subsampling_422_hbd(const uint16_t *input,
                                         int input_stride, int16_t *output_q3,
                                         int width, int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      int left = i << 1;
      output_q3[i] = (input[left] + input[left + 1]) << 2;
    }
    input += input_stride;
    output_q3 += MAX_SB_SIZE;
  }
}

Luc Trudeau's avatar
Luc Trudeau committed
405
406
407
408
409
static void cfl_luma_subsampling_440_hbd(const uint16_t *input,
                                         int input_stride, int16_t *output_q3,
                                         int width, int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
410
      output_q3[i] = (input[i] + input[i + input_stride]) << 2;
Luc Trudeau's avatar
Luc Trudeau committed
411
412
413
414
415
416
    }
    input += input_stride << 1;
    output_q3 += MAX_SB_SIZE;
  }
}

417
418
419
420
421
422
423
424
425
426
427
static void cfl_luma_subsampling_444_hbd(const uint16_t *input,
                                         int input_stride, int16_t *output_q3,
                                         int width, int height) {
  for (int j = 0; j < height; j++) {
    for (int i = 0; i < width; i++) {
      output_q3[i] = input[i] << 3;
    }
    input += input_stride;
    output_q3 += MAX_SB_SIZE;
  }
}
Luc Trudeau's avatar
Luc Trudeau committed
428

429
430
typedef void (*cfl_subsample_hbd_fn)(const uint16_t *input, int input_stride,
                                     int16_t *output_q3, int width, int height);
Luc Trudeau's avatar
Luc Trudeau committed
431

432
433
434
435
436
437
static const cfl_subsample_hbd_fn subsample_hbd[2][2] = {
  //  (sub_y == 0, sub_x == 0)       (sub_y == 0, sub_x == 1)
  //  (sub_y == 1, sub_x == 0)       (sub_y == 1, sub_x == 1)
  { cfl_luma_subsampling_444_hbd, cfl_luma_subsampling_422_hbd },
  { cfl_luma_subsampling_440_hbd, cfl_luma_subsampling_420_hbd },
};
438
#endif  // CONFIG_HIGHBITDEPTH
439

440
441
static void cfl_store(CFL_CTX *cfl, const uint8_t *input, int input_stride,
                      int row, int col, int width, int height, int use_hbd) {
442
  const int tx_off_log2 = tx_size_wide_log2[0];
Luc Trudeau's avatar
Luc Trudeau committed
443
444
445
446
447
448
  const int sub_x = cfl->subsampling_x;
  const int sub_y = cfl->subsampling_y;
  const int store_row = row << (tx_off_log2 - sub_y);
  const int store_col = col << (tx_off_log2 - sub_x);
  const int store_height = height >> sub_y;
  const int store_width = width >> sub_x;
449

450
451
  // Invalidate current parameters
  cfl->are_parameters_computed = 0;
452
453
454
455
456

  // Store the surface of the pixel buffer that was written to, this way we
  // can manage chroma overrun (e.g. when the chroma surfaces goes beyond the
  // frame boundary)
  if (col == 0 && row == 0) {
Luc Trudeau's avatar
Luc Trudeau committed
457
458
    cfl->buf_width = store_width;
    cfl->buf_height = store_height;
459
  } else {
Luc Trudeau's avatar
Luc Trudeau committed
460
461
    cfl->buf_width = OD_MAXI(store_col + store_width, cfl->buf_width);
    cfl->buf_height = OD_MAXI(store_row + store_height, cfl->buf_height);
462
  }
463

464
  // Check that we will remain inside the pixel buffer.
Luc Trudeau's avatar
Luc Trudeau committed
465
466
  assert(store_row + store_height <= MAX_SB_SIZE);
  assert(store_col + store_width <= MAX_SB_SIZE);
467
468

  // Store the input into the CfL pixel buffer
Luc Trudeau's avatar
Luc Trudeau committed
469
470
  int16_t *pred_buf_q3 =
      cfl->pred_buf_q3 + (store_row * MAX_SB_SIZE + store_col);
471

472
473
474
475
476
477
478
479
#if CONFIG_HIGHBITDEPTH
  if (use_hbd) {
    const uint16_t *input_16 = CONVERT_TO_SHORTPTR(input);
    // AND sub_x and sub_y with 1 to ensures that an attacker won't be able to
    // index the function pointer array out of bounds.
    subsample_hbd[sub_y & 1][sub_x & 1](input_16, input_stride, pred_buf_q3,
                                        store_width, store_height);
    return;
480
  }
481
482
483
484
485
486
#endif  // CONFIG_HIGHBITDEPTH
  (void)use_hbd;
  // AND sub_x and sub_y with 1 to ensures that an attacker won't be able to
  // index the function pointer array out of bounds.
  subsample_lbd[sub_y & 1][sub_x & 1](input, input_stride, pred_buf_q3,
                                      store_width, store_height);
Luc Trudeau's avatar
Luc Trudeau committed
487
}
Luc Trudeau's avatar
Luc Trudeau committed
488

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Adjust the row and column of blocks smaller than 8X8, as chroma-referenced
// and non-chroma-referenced blocks are stored together in the CfL buffer.
static INLINE void sub8x8_adjust_offset(const CFL_CTX *cfl, int *row_out,
                                        int *col_out) {
  // Increment row index for bottom: 8x4, 16x4 or both bottom 4x4s.
  if ((cfl->mi_row & 0x01) && cfl->subsampling_y) {
    assert(*row_out == 0);
    (*row_out)++;
  }

  // Increment col index for right: 4x8, 4x16 or both right 4x4s.
  if ((cfl->mi_col & 0x01) && cfl->subsampling_x) {
    assert(*col_out == 0);
    (*col_out)++;
  }
}
#if CONFIG_DEBUG
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Since the chroma surface of sub8x8 block span across multiple luma blocks,
// this function validates that the reconstructed luma area required to predict
// the chroma block using CfL has been stored during the previous luma encode.
//
//   Issue 1: Chroma intra prediction is not always performed after luma. One
//   such example is when luma RD cost is really high and the mode decision
//   algorithm decides to terminate instead of evaluating chroma.
//
//   Issue 2: When multiple CfL predictions are computed for a given sub8x8
//   block. The reconstructed luma that belongs to the non-reference sub8x8
//   blocks must remain in the buffer (we cannot clear the buffer when we
//   compute the CfL prediction
//
// To resolve these issues, we increment the store_counter on each store. if
// other sub8x8 blocks have already been coded and the counter corresponds to
// the previous value they are also set to the current value. If a sub8x8 block
// is not stored the store_counter won't match which will be detected when the
// CfL parements are computed.
static void sub8x8_set_val(CFL_CTX *cfl, int row, int col, TX_SIZE y_tx_size) {
  const int y_tx_wide_unit = tx_size_wide_unit[y_tx_size];
  const int y_tx_high_unit = tx_size_high_unit[y_tx_size];

  // How many 4x4 are in tx_size
  const int y_tx_unit_len = y_tx_wide_unit * y_tx_high_unit;
  assert(y_tx_unit_len == 1 || y_tx_unit_len == 2 || y_tx_unit_len == 4);

  // Invalidate other counters if (0,0)
  const int is_first = row + col == 0;
  cfl->store_counter += is_first ? 2 : 1;

  const int inc =
      (y_tx_wide_unit >= y_tx_high_unit) ? 1 : CFL_SUB8X8_VAL_MI_SIZE;
  uint16_t *sub8x8_val = cfl->sub8x8_val + (row * CFL_SUB8X8_VAL_MI_SIZE + col);
  for (int i = 0; i < y_tx_unit_len; i++) {
    *sub8x8_val = cfl->store_counter;
    sub8x8_val += inc;
  }

  if (!is_first) {
    const uint16_t prev_store_counter = cfl->store_counter - 1;
    int found = 0;
    sub8x8_val = cfl->sub8x8_val;
    for (int y = 0; y < CFL_SUB8X8_VAL_MI_SIZE; y++) {
      for (int x = 0; x < CFL_SUB8X8_VAL_MI_SIZE; x++) {
        if (sub8x8_val[x] == prev_store_counter) {
          sub8x8_val[x] = cfl->store_counter;
          found = 1;
        }
      }
      sub8x8_val += CFL_SUB8X8_VAL_MI_SIZE;
556
    }
557
558
    // Something is wrong if (0,0) is missing
    assert(found);
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  }
}
#endif  // CONFIG_DEBUG

void cfl_store_tx(MACROBLOCKD *const xd, int row, int col, TX_SIZE tx_size,
                  BLOCK_SIZE bsize) {
  CFL_CTX *const cfl = xd->cfl;
  struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
  uint8_t *dst =
      &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
  if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) {
    // Only dimensions of size 4 can have an odd offset.
    assert(!((col & 1) && tx_size_wide[tx_size] != 4));
    assert(!((row & 1) && tx_size_high[tx_size] != 4));
    sub8x8_adjust_offset(cfl, &row, &col);
#if CONFIG_DEBUG
575
    sub8x8_set_val(cfl, row, col, tx_size);
576
577
578
#endif  // CONFIG_DEBUG
  }
  cfl_store(cfl, dst, pd->dst.stride, row, col, tx_size_wide[tx_size],
579
            tx_size_high[tx_size], get_bitdepth_data_path_index(xd));
580
581
582
583
584
585
586
587
588
589
590
}

void cfl_store_block(MACROBLOCKD *const xd, BLOCK_SIZE bsize, TX_SIZE tx_size) {
  CFL_CTX *const cfl = xd->cfl;
  struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
  int row = 0;
  int col = 0;
  bsize = AOMMAX(BLOCK_4X4, bsize);
  if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) {
    sub8x8_adjust_offset(cfl, &row, &col);
#if CONFIG_DEBUG
591
592
593
594
595
596
    // Point to the last transform block inside the partition.
    const int off_row =
        row + (mi_size_high[bsize] - tx_size_high_unit[tx_size]);
    const int off_col =
        col + (mi_size_wide[bsize] - tx_size_wide_unit[tx_size]);
    sub8x8_set_val(cfl, off_row, off_col, tx_size);
597
598
599
600
#endif  // CONFIG_DEBUG
  }
  const int width = max_intra_block_width(xd, bsize, AOM_PLANE_Y, tx_size);
  const int height = max_intra_block_height(xd, bsize, AOM_PLANE_Y, tx_size);
601
602
  cfl_store(cfl, pd->dst.buf, pd->dst.stride, row, col, width, height,
            get_bitdepth_data_path_index(xd));
603
}
604

Luc Trudeau's avatar
Luc Trudeau committed
605
606
607
void cfl_compute_parameters(MACROBLOCKD *const xd, TX_SIZE tx_size) {
  CFL_CTX *const cfl = xd->cfl;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
608

Luc Trudeau's avatar
Luc Trudeau committed
609
610
  // Do not call cfl_compute_parameters multiple time on the same values.
  assert(cfl->are_parameters_computed == 0);
611

Luc Trudeau's avatar
Luc Trudeau committed
612
613
  const BLOCK_SIZE plane_bsize = AOMMAX(
      BLOCK_4X4, get_plane_block_size(mbmi->sb_type, &xd->plane[AOM_PLANE_U]));
Luc Trudeau's avatar
Luc Trudeau committed
614
#if CONFIG_DEBUG
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
  BLOCK_SIZE bsize = mbmi->sb_type;
  if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) {
    const uint16_t compute_counter = cfl->sub8x8_val[0];
    assert(compute_counter != cfl->last_compute_counter);
    bsize = scale_chroma_bsize(bsize, cfl->subsampling_x, cfl->subsampling_y);
    const int val_wide = mi_size_wide[bsize];
    const int val_high = mi_size_high[bsize];
    assert(val_wide <= CFL_SUB8X8_VAL_MI_SIZE);
    assert(val_high <= CFL_SUB8X8_VAL_MI_SIZE);
    for (int val_r = 0; val_r < val_high; val_r++) {
      for (int val_c = 0; val_c < val_wide; val_c++) {
        // If all counters in the validation buffer are equal then they are all
        // related to the same chroma reference block.
        assert(cfl->sub8x8_val[val_r * CFL_SUB8X8_VAL_MI_SIZE + val_c] ==
               compute_counter);
Luc Trudeau's avatar
Luc Trudeau committed
630
631
      }
    }
632
    cfl->last_compute_counter = compute_counter;
Luc Trudeau's avatar
Luc Trudeau committed
633
634
  }
#endif  // CONFIG_DEBUG
Luc Trudeau's avatar
Luc Trudeau committed
635
636
637
638
  // AOM_PLANE_U is used, but both planes will have the same sizes.
  cfl->uv_width = max_intra_block_width(xd, plane_bsize, AOM_PLANE_U, tx_size);
  cfl->uv_height =
      max_intra_block_height(xd, plane_bsize, AOM_PLANE_U, tx_size);
639

640
  cfl_dc_pred(xd, plane_bsize);
641
  cfl_subtract_averages(cfl, tx_size);
Luc Trudeau's avatar
Luc Trudeau committed
642
  cfl->are_parameters_computed = 1;
643
}