Coverage Report

Created: 2018-09-25 14:53

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