Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 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" |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 14 | #include "av1/common/onyxc_int.h" |
| 15 | |
Luc Trudeau | dac5e39 | 2017-06-05 15:52:02 -0400 | [diff] [blame] | 16 | void cfl_init(CFL_CTX *cfl, AV1_COMMON *cm) { |
Luc Trudeau | 06b4708 | 2017-10-31 10:42:36 -0400 | [diff] [blame] | 17 | if ((cm->subsampling_x != 0 && cm->subsampling_x != 1) || |
| 18 | (cm->subsampling_y != 0 && cm->subsampling_y != 1)) { |
| 19 | aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
| 20 | "Only 4:4:4, 4:4:0, 4:2:2 and 4:2:0 are currently " |
| 21 | "supported by CfL, %d %d " |
| 22 | "subsampling is not supported.\n", |
| 23 | cm->subsampling_x, cm->subsampling_y); |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 24 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 25 | memset(&cfl->pred_buf_q3, 0, sizeof(cfl->pred_buf_q3)); |
Luc Trudeau | dac5e39 | 2017-06-05 15:52:02 -0400 | [diff] [blame] | 26 | cfl->subsampling_x = cm->subsampling_x; |
| 27 | cfl->subsampling_y = cm->subsampling_y; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 28 | cfl->are_parameters_computed = 0; |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 29 | cfl->store_y = 0; |
Hui Su | 9fa9623 | 2017-10-23 15:46:04 -0700 | [diff] [blame] | 30 | #if CONFIG_DEBUG |
Luc Trudeau | c84c21c | 2017-07-25 19:40:34 -0400 | [diff] [blame] | 31 | cfl_clear_sub8x8_val(cfl); |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 32 | cfl->store_counter = 0; |
| 33 | cfl->last_compute_counter = 0; |
Hui Su | 9fa9623 | 2017-10-23 15:46:04 -0700 | [diff] [blame] | 34 | #endif // CONFIG_DEBUG |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 37 | // Due to frame boundary issues, it is possible that the total area covered by |
| 38 | // chroma exceeds that of luma. When this happens, we fill the missing pixels by |
| 39 | // repeating the last columns and/or rows. |
| 40 | static INLINE void cfl_pad(CFL_CTX *cfl, int width, int height) { |
| 41 | const int diff_width = width - cfl->buf_width; |
| 42 | const int diff_height = height - cfl->buf_height; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 43 | |
| 44 | if (diff_width > 0) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 45 | const int min_height = height - diff_height; |
| 46 | int16_t *pred_buf_q3 = cfl->pred_buf_q3 + (width - diff_width); |
| 47 | for (int j = 0; j < min_height; j++) { |
Luc Trudeau | e67377b | 2017-10-31 16:08:05 -0400 | [diff] [blame] | 48 | const int16_t last_pixel = pred_buf_q3[-1]; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 49 | for (int i = 0; i < diff_width; i++) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 50 | pred_buf_q3[i] = last_pixel; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 51 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 52 | pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 53 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 54 | cfl->buf_width = width; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 55 | } |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 56 | if (diff_height > 0) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 57 | int16_t *pred_buf_q3 = |
| 58 | cfl->pred_buf_q3 + ((height - diff_height) * MAX_SB_SIZE); |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 59 | for (int j = 0; j < diff_height; j++) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 60 | const int16_t *last_row_q3 = pred_buf_q3 - MAX_SB_SIZE; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 61 | for (int i = 0; i < width; i++) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 62 | pred_buf_q3[i] = last_row_q3[i]; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 63 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 64 | pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 65 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 66 | cfl->buf_height = height; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 67 | } |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 68 | } |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 69 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 70 | static void sum_above_row_lbd(const uint8_t *above_u, const uint8_t *above_v, |
| 71 | int width, int *out_sum_u, int *out_sum_v) { |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 72 | int sum_u = 0; |
| 73 | int sum_v = 0; |
| 74 | for (int i = 0; i < width; i++) { |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 75 | sum_u += above_u[i]; |
| 76 | sum_v += above_v[i]; |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 77 | } |
| 78 | *out_sum_u += sum_u; |
| 79 | *out_sum_v += sum_v; |
| 80 | } |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 81 | #if CONFIG_HIGHBITDEPTH |
| 82 | static void sum_above_row_hbd(const uint16_t *above_u, const uint16_t *above_v, |
| 83 | int width, int *out_sum_u, int *out_sum_v) { |
| 84 | int sum_u = 0; |
| 85 | int sum_v = 0; |
| 86 | for (int i = 0; i < width; i++) { |
| 87 | sum_u += above_u[i]; |
| 88 | sum_v += above_v[i]; |
| 89 | } |
| 90 | *out_sum_u += sum_u; |
| 91 | *out_sum_v += sum_v; |
| 92 | } |
| 93 | #endif // CONFIG_HIGHBITDEPTH |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 94 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 95 | static void sum_above_row(const MACROBLOCKD *xd, int width, int *out_sum_u, |
| 96 | int *out_sum_v) { |
| 97 | const struct macroblockd_plane *const pd_u = &xd->plane[AOM_PLANE_U]; |
| 98 | const struct macroblockd_plane *const pd_v = &xd->plane[AOM_PLANE_V]; |
| 99 | #if CONFIG_HIGHBITDEPTH |
| 100 | if (get_bitdepth_data_path_index(xd)) { |
| 101 | const uint16_t *above_u_16 = |
| 102 | CONVERT_TO_SHORTPTR(pd_u->dst.buf) - pd_u->dst.stride; |
| 103 | const uint16_t *above_v_16 = |
| 104 | CONVERT_TO_SHORTPTR(pd_v->dst.buf) - pd_v->dst.stride; |
| 105 | sum_above_row_hbd(above_u_16, above_v_16, width, out_sum_u, out_sum_v); |
| 106 | return; |
| 107 | } |
| 108 | #endif // CONFIG_HIGHBITDEPTH |
| 109 | const uint8_t *above_u = pd_u->dst.buf - pd_u->dst.stride; |
| 110 | const uint8_t *above_v = pd_v->dst.buf - pd_v->dst.stride; |
| 111 | sum_above_row_lbd(above_u, above_v, width, out_sum_u, out_sum_v); |
| 112 | } |
| 113 | |
| 114 | static void sum_left_col_lbd(const uint8_t *left_u, int u_stride, |
| 115 | const uint8_t *left_v, int v_stride, int height, |
| 116 | int *out_sum_u, int *out_sum_v) { |
| 117 | int sum_u = 0; |
| 118 | int sum_v = 0; |
| 119 | for (int i = 0; i < height; i++) { |
| 120 | sum_u += left_u[i * u_stride]; |
| 121 | sum_v += left_v[i * v_stride]; |
| 122 | } |
| 123 | *out_sum_u += sum_u; |
| 124 | *out_sum_v += sum_v; |
| 125 | } |
| 126 | #if CONFIG_HIGHBITDEPTH |
| 127 | static void sum_left_col_hbd(const uint16_t *left_u, int u_stride, |
| 128 | const uint16_t *left_v, int v_stride, int height, |
| 129 | int *out_sum_u, int *out_sum_v) { |
| 130 | int sum_u = 0; |
| 131 | int sum_v = 0; |
| 132 | for (int i = 0; i < height; i++) { |
| 133 | sum_u += left_u[i * u_stride]; |
| 134 | sum_v += left_v[i * v_stride]; |
| 135 | } |
| 136 | *out_sum_u += sum_u; |
| 137 | *out_sum_v += sum_v; |
| 138 | } |
| 139 | #endif // CONFIG_HIGHBITDEPTH |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 140 | static void sum_left_col(const MACROBLOCKD *xd, int height, int *out_sum_u, |
| 141 | int *out_sum_v) { |
| 142 | const struct macroblockd_plane *const pd_u = &xd->plane[AOM_PLANE_U]; |
| 143 | const struct macroblockd_plane *const pd_v = &xd->plane[AOM_PLANE_V]; |
| 144 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 145 | #if CONFIG_HIGHBITDEPTH |
| 146 | if (get_bitdepth_data_path_index(xd)) { |
| 147 | const uint16_t *left_u_16 = CONVERT_TO_SHORTPTR(pd_u->dst.buf) - 1; |
| 148 | const uint16_t *left_v_16 = CONVERT_TO_SHORTPTR(pd_v->dst.buf) - 1; |
| 149 | sum_left_col_hbd(left_u_16, pd_u->dst.stride, left_v_16, pd_v->dst.stride, |
| 150 | height, out_sum_u, out_sum_v); |
| 151 | return; |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 152 | } |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 153 | #endif // CONFIG_HIGHBITDEPTH |
| 154 | const uint8_t *left_u = pd_u->dst.buf - 1; |
| 155 | const uint8_t *left_v = pd_v->dst.buf - 1; |
| 156 | sum_left_col_lbd(left_u, pd_u->dst.stride, left_v, pd_v->dst.stride, height, |
| 157 | out_sum_u, out_sum_v); |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // CfL computes its own block-level DC_PRED. This is required to compute both |
| 161 | // alpha_cb and alpha_cr before the prediction are computed. |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 162 | static void cfl_dc_pred(MACROBLOCKD *xd, BLOCK_SIZE plane_bsize, |
| 163 | TX_SIZE tx_size) { |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 164 | CFL_CTX *const cfl = xd->cfl; |
Luc Trudeau | e7f9e16 | 2017-06-23 21:20:20 -0400 | [diff] [blame] | 165 | |
| 166 | // Compute DC_PRED until block boundary. We can't assume the neighbor will use |
| 167 | // the same transform size. |
| 168 | const int width = max_block_wide(xd, plane_bsize, AOM_PLANE_U) |
| 169 | << tx_size_wide_log2[0]; |
| 170 | const int height = max_block_high(xd, plane_bsize, AOM_PLANE_U) |
| 171 | << tx_size_high_log2[0]; |
Luc Trudeau | 23a3b21 | 2017-04-28 11:36:59 -0400 | [diff] [blame] | 172 | // Number of pixel on the top and left borders. |
Luc Trudeau | 2e6cb7e | 2017-07-04 12:41:53 -0400 | [diff] [blame] | 173 | const int num_pel = width + height; |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 174 | |
Luc Trudeau | 23a3b21 | 2017-04-28 11:36:59 -0400 | [diff] [blame] | 175 | int sum_u = 0; |
| 176 | int sum_v = 0; |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 177 | |
Hui Su | 9fa9623 | 2017-10-23 15:46:04 -0700 | [diff] [blame] | 178 | // Match behavior of build_intra_predictors_high (reconintra.c) at superblock |
| 179 | // boundaries: |
| 180 | // base-1 base-1 base-1 .. base-1 base-1 base-1 base-1 base-1 base-1 |
| 181 | // base+1 A B .. Y Z |
| 182 | // base+1 C D .. W X |
| 183 | // base+1 E F .. U V |
| 184 | // base+1 G H .. S T T T T T |
| 185 | // .. |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 186 | |
Luc Trudeau | 7f0f6c4 | 2017-05-16 10:35:49 -0400 | [diff] [blame] | 187 | if (xd->chroma_up_available && xd->mb_to_right_edge >= 0) { |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 188 | sum_above_row(xd, width, &sum_u, &sum_v); |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 189 | } else { |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 190 | const int base = 128 << (xd->bd - 8); |
Luc Trudeau | 71ad78c | 2017-09-28 16:08:14 -0400 | [diff] [blame] | 191 | sum_u = width * (base - 1); |
| 192 | sum_v = width * (base - 1); |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Luc Trudeau | 7f0f6c4 | 2017-05-16 10:35:49 -0400 | [diff] [blame] | 195 | if (xd->chroma_left_available && xd->mb_to_bottom_edge >= 0) { |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 196 | sum_left_col(xd, height, &sum_u, &sum_v); |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 197 | } else { |
Luc Trudeau | 13281d4 | 2017-10-03 10:46:49 -0400 | [diff] [blame] | 198 | const int base = 128 << (xd->bd - 8); |
Luc Trudeau | 71ad78c | 2017-09-28 16:08:14 -0400 | [diff] [blame] | 199 | sum_u += height * (base + 1); |
| 200 | sum_v += height * (base + 1); |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 203 | // TODO(ltrudeau) Because of max_block_wide and max_block_high, num_pel will |
| 204 | // not be a power of two. So these divisions will have to use a lookup table. |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 205 | const int16_t dc_pred_u = (sum_u + (num_pel >> 1)) / num_pel; |
| 206 | const int16_t dc_pred_v = (sum_v + (num_pel >> 1)) / num_pel; |
| 207 | const int blk_width = |
| 208 | max_intra_block_width(xd, plane_bsize, AOM_PLANE_U, tx_size); |
| 209 | const int blk_height = |
| 210 | max_intra_block_height(xd, plane_bsize, AOM_PLANE_U, tx_size); |
| 211 | int16_t *p_dc_pred_u = cfl->dc_pred[CFL_PRED_U]; |
| 212 | int16_t *p_dc_pred_v = cfl->dc_pred[CFL_PRED_V]; |
| 213 | for (int j = 0; j < blk_height; j++) { |
| 214 | for (int i = 0; i < blk_width; i++) { |
| 215 | p_dc_pred_u[i] = dc_pred_u; |
| 216 | p_dc_pred_v[i] = dc_pred_v; |
| 217 | } |
| 218 | p_dc_pred_u += MAX_SB_SIZE; |
| 219 | p_dc_pred_v += MAX_SB_SIZE; |
| 220 | } |
Luc Trudeau | f816415 | 2017-04-11 16:20:51 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 223 | static void cfl_subtract_averages(CFL_CTX *cfl, TX_SIZE tx_size) { |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 224 | const int width = cfl->uv_width; |
| 225 | const int height = cfl->uv_height; |
Luc Trudeau | 0367894 | 2017-06-12 17:33:19 -0400 | [diff] [blame] | 226 | const int tx_height = tx_size_high[tx_size]; |
| 227 | const int tx_width = tx_size_wide[tx_size]; |
Luc Trudeau | 0367894 | 2017-06-12 17:33:19 -0400 | [diff] [blame] | 228 | const int block_row_stride = MAX_SB_SIZE << tx_size_high_log2[tx_size]; |
Luc Trudeau | bfe2827 | 2017-07-01 12:57:17 -0400 | [diff] [blame] | 229 | const int num_pel_log2 = |
| 230 | (tx_size_high_log2[tx_size] + tx_size_wide_log2[tx_size]); |
| 231 | |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 232 | int16_t *pred_buf_q3 = cfl->pred_buf_q3; |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 233 | |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 234 | cfl_pad(cfl, width, height); |
| 235 | |
Luc Trudeau | 0367894 | 2017-06-12 17:33:19 -0400 | [diff] [blame] | 236 | for (int b_j = 0; b_j < height; b_j += tx_height) { |
| 237 | for (int b_i = 0; b_i < width; b_i += tx_width) { |
Luc Trudeau | a0af3b5 | 2017-09-06 13:37:33 -0400 | [diff] [blame] | 238 | int sum_q3 = 0; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 239 | int16_t *tx_pred_buf_q3 = pred_buf_q3; |
Luc Trudeau | 0367894 | 2017-06-12 17:33:19 -0400 | [diff] [blame] | 240 | for (int t_j = 0; t_j < tx_height; t_j++) { |
| 241 | for (int t_i = b_i; t_i < b_i + tx_width; t_i++) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 242 | sum_q3 += tx_pred_buf_q3[t_i]; |
Luc Trudeau | 0367894 | 2017-06-12 17:33:19 -0400 | [diff] [blame] | 243 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 244 | tx_pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | 0367894 | 2017-06-12 17:33:19 -0400 | [diff] [blame] | 245 | } |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 246 | int avg_q3 = (sum_q3 + (1 << (num_pel_log2 - 1))) >> num_pel_log2; |
Luc Trudeau | 475fc9d | 2017-07-04 16:51:14 -0400 | [diff] [blame] | 247 | // Loss is never more than 1/2 (in Q3) |
Yaowu Xu | 2a91ab7 | 2017-11-06 15:12:17 -0800 | [diff] [blame^] | 248 | assert(abs((avg_q3 * (1 << num_pel_log2)) - sum_q3) <= |
| 249 | 1 << num_pel_log2 >> 1); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 250 | |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 251 | tx_pred_buf_q3 = pred_buf_q3; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 252 | for (int t_j = 0; t_j < tx_height; t_j++) { |
| 253 | for (int t_i = b_i; t_i < b_i + tx_width; t_i++) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 254 | tx_pred_buf_q3[t_i] -= avg_q3; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 255 | } |
| 256 | |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 257 | tx_pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 258 | } |
Luc Trudeau | 3e18e4a | 2017-06-13 13:54:14 -0400 | [diff] [blame] | 259 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 260 | pred_buf_q3 += block_row_stride; |
Luc Trudeau | 3e18e4a | 2017-06-13 13:54:14 -0400 | [diff] [blame] | 261 | } |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 262 | } |
| 263 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 264 | static INLINE int cfl_idx_to_alpha(int alpha_idx, int joint_sign, |
Luc Trudeau | 4e81d92 | 2017-07-05 17:17:06 -0400 | [diff] [blame] | 265 | CFL_PRED_TYPE pred_type) { |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 266 | const int alpha_sign = (pred_type == CFL_PRED_U) ? CFL_SIGN_U(joint_sign) |
| 267 | : CFL_SIGN_V(joint_sign); |
| 268 | if (alpha_sign == CFL_SIGN_ZERO) return 0; |
| 269 | const int abs_alpha_q3 = |
| 270 | (pred_type == CFL_PRED_U) ? CFL_IDX_U(alpha_idx) : CFL_IDX_V(alpha_idx); |
| 271 | return (alpha_sign == CFL_SIGN_POS) ? abs_alpha_q3 + 1 : -abs_alpha_q3 - 1; |
Luc Trudeau | 3e18e4a | 2017-06-13 13:54:14 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 274 | static void cfl_build_prediction_lbd(const int16_t *pred_buf_q3, uint8_t *dst, |
| 275 | int dst_stride, int width, int height, |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 276 | int alpha_q3, const int16_t *dc_pred) { |
Luc Trudeau | 67914b5 | 2017-09-14 17:13:28 -0400 | [diff] [blame] | 277 | for (int j = 0; j < height; j++) { |
| 278 | for (int i = 0; i < width; i++) { |
| 279 | dst[i] = |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 280 | clip_pixel(get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]) + dc_pred[i]); |
Luc Trudeau | 67914b5 | 2017-09-14 17:13:28 -0400 | [diff] [blame] | 281 | } |
| 282 | dst += dst_stride; |
| 283 | pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 284 | dc_pred += MAX_SB_SIZE; |
Luc Trudeau | 67914b5 | 2017-09-14 17:13:28 -0400 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 288 | #if CONFIG_HIGHBITDEPTH |
| 289 | static void cfl_build_prediction_hbd(const int16_t *pred_buf_q3, uint16_t *dst, |
| 290 | int dst_stride, int width, int height, |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 291 | int alpha_q3, const int16_t *dc_pred, |
Luc Trudeau | d348772 | 2017-09-29 15:55:48 -0400 | [diff] [blame] | 292 | int bit_depth) { |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 293 | for (int j = 0; j < height; j++) { |
| 294 | for (int i = 0; i < width; i++) { |
| 295 | dst[i] = clip_pixel_highbd( |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 296 | get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]) + dc_pred[i], bit_depth); |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 297 | } |
| 298 | dst += dst_stride; |
| 299 | pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 300 | dc_pred += MAX_SB_SIZE; |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | #endif // CONFIG_HIGHBITDEPTH |
| 304 | |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 305 | void cfl_predict_block(MACROBLOCKD *const xd, uint8_t *dst, int dst_stride, |
| 306 | int row, int col, TX_SIZE tx_size, int plane) { |
| 307 | CFL_CTX *const cfl = xd->cfl; |
| 308 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 309 | |
| 310 | // CfL parameters must be computed before prediction can be done. |
| 311 | assert(cfl->are_parameters_computed == 1); |
| 312 | |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 313 | const int16_t *pred_buf_q3 = |
| 314 | cfl->pred_buf_q3 + ((row * MAX_SB_SIZE + col) << tx_size_wide_log2[0]); |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 315 | const int16_t *dc_pred = cfl->dc_pred[plane - 1] + |
| 316 | ((row * MAX_SB_SIZE + col) << tx_size_wide_log2[0]); |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 317 | const int alpha_q3 = |
| 318 | cfl_idx_to_alpha(mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, plane - 1); |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 319 | |
Luc Trudeau | 6d3befb | 2017-10-02 13:52:22 -0400 | [diff] [blame] | 320 | #if CONFIG_HIGHBITDEPTH |
| 321 | if (get_bitdepth_data_path_index(xd)) { |
| 322 | uint16_t *dst_16 = CONVERT_TO_SHORTPTR(dst); |
| 323 | cfl_build_prediction_hbd(pred_buf_q3, dst_16, dst_stride, |
| 324 | tx_size_wide[tx_size], tx_size_high[tx_size], |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 325 | alpha_q3, dc_pred, xd->bd); |
Luc Trudeau | 6d3befb | 2017-10-02 13:52:22 -0400 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | #endif // CONFIG_HIGHBITDEPTH |
| 329 | cfl_build_prediction_lbd(pred_buf_q3, dst, dst_stride, tx_size_wide[tx_size], |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 330 | tx_size_high[tx_size], alpha_q3, dc_pred); |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 333 | static void cfl_luma_subsampling_420_lbd(const uint8_t *input, int input_stride, |
| 334 | int16_t *output_q3, int width, |
| 335 | int height) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 336 | for (int j = 0; j < height; j++) { |
| 337 | for (int i = 0; i < width; i++) { |
| 338 | int top = i << 1; |
| 339 | int bot = top + input_stride; |
| 340 | output_q3[i] = (input[top] + input[top + 1] + input[bot] + input[bot + 1]) |
| 341 | << 1; |
| 342 | } |
| 343 | input += input_stride << 1; |
| 344 | output_q3 += MAX_SB_SIZE; |
| 345 | } |
| 346 | } |
| 347 | |
Luc Trudeau | c8323c0 | 2017-10-11 21:05:54 -0400 | [diff] [blame] | 348 | static void cfl_luma_subsampling_422_lbd(const uint8_t *input, int input_stride, |
| 349 | int16_t *output_q3, int width, |
| 350 | int height) { |
| 351 | for (int j = 0; j < height; j++) { |
| 352 | for (int i = 0; i < width; i++) { |
| 353 | int left = i << 1; |
| 354 | output_q3[i] = (input[left] + input[left + 1]) << 2; |
| 355 | } |
| 356 | input += input_stride; |
| 357 | output_q3 += MAX_SB_SIZE; |
| 358 | } |
| 359 | } |
| 360 | |
Luc Trudeau | 06b4708 | 2017-10-31 10:42:36 -0400 | [diff] [blame] | 361 | static void cfl_luma_subsampling_440_lbd(const uint8_t *input, int input_stride, |
| 362 | int16_t *output_q3, int width, |
| 363 | int height) { |
| 364 | for (int j = 0; j < height; j++) { |
| 365 | for (int i = 0; i < width; i++) { |
| 366 | output_q3[i] = (input[i] + input[i + input_stride]) << 2; |
| 367 | } |
| 368 | input += input_stride << 1; |
| 369 | output_q3 += MAX_SB_SIZE; |
| 370 | } |
| 371 | } |
| 372 | |
Luc Trudeau | 69d9e87 | 2017-09-15 20:40:47 -0400 | [diff] [blame] | 373 | static void cfl_luma_subsampling_444_lbd(const uint8_t *input, int input_stride, |
| 374 | int16_t *output_q3, int width, |
| 375 | int height) { |
| 376 | for (int j = 0; j < height; j++) { |
| 377 | for (int i = 0; i < width; i++) { |
| 378 | output_q3[i] = input[i] << 3; |
| 379 | } |
| 380 | input += input_stride; |
| 381 | output_q3 += MAX_SB_SIZE; |
| 382 | } |
| 383 | } |
| 384 | |
Luc Trudeau | 43ed571 | 2017-10-31 12:29:28 -0400 | [diff] [blame] | 385 | typedef void (*cfl_subsample_lbd_fn)(const uint8_t *input, int input_stride, |
| 386 | int16_t *output_q3, int width, int height); |
| 387 | |
| 388 | static const cfl_subsample_lbd_fn subsample_lbd[2][2] = { |
| 389 | // (sub_y == 0, sub_x == 0) (sub_y == 0, sub_x == 1) |
| 390 | // (sub_y == 1, sub_x == 0) (sub_y == 1, sub_x == 1) |
| 391 | { cfl_luma_subsampling_444_lbd, cfl_luma_subsampling_422_lbd }, |
| 392 | { cfl_luma_subsampling_440_lbd, cfl_luma_subsampling_420_lbd }, |
| 393 | }; |
| 394 | |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 395 | #if CONFIG_HIGHBITDEPTH |
| 396 | static void cfl_luma_subsampling_420_hbd(const uint16_t *input, |
| 397 | int input_stride, int16_t *output_q3, |
| 398 | int width, int height) { |
| 399 | for (int j = 0; j < height; j++) { |
| 400 | for (int i = 0; i < width; i++) { |
| 401 | int top = i << 1; |
| 402 | int bot = top + input_stride; |
| 403 | output_q3[i] = (input[top] + input[top + 1] + input[bot] + input[bot + 1]) |
| 404 | << 1; |
| 405 | } |
| 406 | input += input_stride << 1; |
| 407 | output_q3 += MAX_SB_SIZE; |
| 408 | } |
| 409 | } |
Luc Trudeau | 69d9e87 | 2017-09-15 20:40:47 -0400 | [diff] [blame] | 410 | |
Luc Trudeau | c8323c0 | 2017-10-11 21:05:54 -0400 | [diff] [blame] | 411 | static void cfl_luma_subsampling_422_hbd(const uint16_t *input, |
| 412 | int input_stride, int16_t *output_q3, |
| 413 | int width, int height) { |
| 414 | for (int j = 0; j < height; j++) { |
| 415 | for (int i = 0; i < width; i++) { |
| 416 | int left = i << 1; |
| 417 | output_q3[i] = (input[left] + input[left + 1]) << 2; |
| 418 | } |
| 419 | input += input_stride; |
| 420 | output_q3 += MAX_SB_SIZE; |
| 421 | } |
| 422 | } |
| 423 | |
Luc Trudeau | 06b4708 | 2017-10-31 10:42:36 -0400 | [diff] [blame] | 424 | static void cfl_luma_subsampling_440_hbd(const uint16_t *input, |
| 425 | int input_stride, int16_t *output_q3, |
| 426 | int width, int height) { |
| 427 | for (int j = 0; j < height; j++) { |
| 428 | for (int i = 0; i < width; i++) { |
Luc Trudeau | 6acb300 | 2017-11-02 14:09:28 -0400 | [diff] [blame] | 429 | output_q3[i] = (input[i] + input[i + input_stride]) << 2; |
Luc Trudeau | 06b4708 | 2017-10-31 10:42:36 -0400 | [diff] [blame] | 430 | } |
| 431 | input += input_stride << 1; |
| 432 | output_q3 += MAX_SB_SIZE; |
| 433 | } |
| 434 | } |
| 435 | |
Luc Trudeau | 69d9e87 | 2017-09-15 20:40:47 -0400 | [diff] [blame] | 436 | static void cfl_luma_subsampling_444_hbd(const uint16_t *input, |
| 437 | int input_stride, int16_t *output_q3, |
| 438 | int width, int height) { |
| 439 | for (int j = 0; j < height; j++) { |
| 440 | for (int i = 0; i < width; i++) { |
| 441 | output_q3[i] = input[i] << 3; |
| 442 | } |
| 443 | input += input_stride; |
| 444 | output_q3 += MAX_SB_SIZE; |
| 445 | } |
| 446 | } |
Luc Trudeau | 43ed571 | 2017-10-31 12:29:28 -0400 | [diff] [blame] | 447 | |
| 448 | typedef void (*cfl_subsample_hbd_fn)(const uint16_t *input, int input_stride, |
| 449 | int16_t *output_q3, int width, int height); |
| 450 | |
| 451 | static const cfl_subsample_hbd_fn subsample_hbd[2][2] = { |
| 452 | // (sub_y == 0, sub_x == 0) (sub_y == 0, sub_x == 1) |
| 453 | // (sub_y == 1, sub_x == 0) (sub_y == 1, sub_x == 1) |
| 454 | { cfl_luma_subsampling_444_hbd, cfl_luma_subsampling_422_hbd }, |
| 455 | { cfl_luma_subsampling_440_hbd, cfl_luma_subsampling_420_hbd }, |
| 456 | }; |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 457 | #endif // CONFIG_HIGHBITDEPTH |
| 458 | |
Luc Trudeau | 43ed571 | 2017-10-31 12:29:28 -0400 | [diff] [blame] | 459 | static void cfl_store(CFL_CTX *cfl, const uint8_t *input, int input_stride, |
| 460 | int row, int col, int width, int height, int use_hbd) { |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 461 | const int tx_off_log2 = tx_size_wide_log2[0]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 462 | const int sub_x = cfl->subsampling_x; |
| 463 | const int sub_y = cfl->subsampling_y; |
| 464 | const int store_row = row << (tx_off_log2 - sub_y); |
| 465 | const int store_col = col << (tx_off_log2 - sub_x); |
| 466 | const int store_height = height >> sub_y; |
| 467 | const int store_width = width >> sub_x; |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 468 | |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 469 | // Invalidate current parameters |
| 470 | cfl->are_parameters_computed = 0; |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 471 | |
| 472 | // Store the surface of the pixel buffer that was written to, this way we |
| 473 | // can manage chroma overrun (e.g. when the chroma surfaces goes beyond the |
| 474 | // frame boundary) |
| 475 | if (col == 0 && row == 0) { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 476 | cfl->buf_width = store_width; |
| 477 | cfl->buf_height = store_height; |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 478 | } else { |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 479 | cfl->buf_width = OD_MAXI(store_col + store_width, cfl->buf_width); |
| 480 | cfl->buf_height = OD_MAXI(store_row + store_height, cfl->buf_height); |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 481 | } |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 482 | |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 483 | // Check that we will remain inside the pixel buffer. |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 484 | assert(store_row + store_height <= MAX_SB_SIZE); |
| 485 | assert(store_col + store_width <= MAX_SB_SIZE); |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 486 | |
| 487 | // Store the input into the CfL pixel buffer |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 488 | int16_t *pred_buf_q3 = |
| 489 | cfl->pred_buf_q3 + (store_row * MAX_SB_SIZE + store_col); |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 490 | |
Luc Trudeau | 43ed571 | 2017-10-31 12:29:28 -0400 | [diff] [blame] | 491 | #if CONFIG_HIGHBITDEPTH |
| 492 | if (use_hbd) { |
| 493 | const uint16_t *input_16 = CONVERT_TO_SHORTPTR(input); |
| 494 | // AND sub_x and sub_y with 1 to ensures that an attacker won't be able to |
| 495 | // index the function pointer array out of bounds. |
| 496 | subsample_hbd[sub_y & 1][sub_x & 1](input_16, input_stride, pred_buf_q3, |
| 497 | store_width, store_height); |
| 498 | return; |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 499 | } |
Luc Trudeau | 43ed571 | 2017-10-31 12:29:28 -0400 | [diff] [blame] | 500 | #endif // CONFIG_HIGHBITDEPTH |
| 501 | (void)use_hbd; |
| 502 | // AND sub_x and sub_y with 1 to ensures that an attacker won't be able to |
| 503 | // index the function pointer array out of bounds. |
| 504 | subsample_lbd[sub_y & 1][sub_x & 1](input, input_stride, pred_buf_q3, |
| 505 | store_width, store_height); |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 506 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 507 | |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 508 | // Adjust the row and column of blocks smaller than 8X8, as chroma-referenced |
| 509 | // and non-chroma-referenced blocks are stored together in the CfL buffer. |
| 510 | static INLINE void sub8x8_adjust_offset(const CFL_CTX *cfl, int *row_out, |
| 511 | int *col_out) { |
| 512 | // Increment row index for bottom: 8x4, 16x4 or both bottom 4x4s. |
| 513 | if ((cfl->mi_row & 0x01) && cfl->subsampling_y) { |
| 514 | assert(*row_out == 0); |
| 515 | (*row_out)++; |
| 516 | } |
| 517 | |
| 518 | // Increment col index for right: 4x8, 4x16 or both right 4x4s. |
| 519 | if ((cfl->mi_col & 0x01) && cfl->subsampling_x) { |
| 520 | assert(*col_out == 0); |
| 521 | (*col_out)++; |
| 522 | } |
| 523 | } |
| 524 | #if CONFIG_DEBUG |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 525 | // Since the chroma surface of sub8x8 block span across multiple luma blocks, |
| 526 | // this function validates that the reconstructed luma area required to predict |
| 527 | // the chroma block using CfL has been stored during the previous luma encode. |
| 528 | // |
| 529 | // Issue 1: Chroma intra prediction is not always performed after luma. One |
| 530 | // such example is when luma RD cost is really high and the mode decision |
| 531 | // algorithm decides to terminate instead of evaluating chroma. |
| 532 | // |
| 533 | // Issue 2: When multiple CfL predictions are computed for a given sub8x8 |
| 534 | // block. The reconstructed luma that belongs to the non-reference sub8x8 |
| 535 | // blocks must remain in the buffer (we cannot clear the buffer when we |
| 536 | // compute the CfL prediction |
| 537 | // |
| 538 | // To resolve these issues, we increment the store_counter on each store. if |
| 539 | // other sub8x8 blocks have already been coded and the counter corresponds to |
| 540 | // the previous value they are also set to the current value. If a sub8x8 block |
| 541 | // is not stored the store_counter won't match which will be detected when the |
| 542 | // CfL parements are computed. |
| 543 | static void sub8x8_set_val(CFL_CTX *cfl, int row, int col, TX_SIZE y_tx_size) { |
| 544 | const int y_tx_wide_unit = tx_size_wide_unit[y_tx_size]; |
| 545 | const int y_tx_high_unit = tx_size_high_unit[y_tx_size]; |
| 546 | |
| 547 | // How many 4x4 are in tx_size |
| 548 | const int y_tx_unit_len = y_tx_wide_unit * y_tx_high_unit; |
| 549 | assert(y_tx_unit_len == 1 || y_tx_unit_len == 2 || y_tx_unit_len == 4); |
| 550 | |
| 551 | // Invalidate other counters if (0,0) |
| 552 | const int is_first = row + col == 0; |
| 553 | cfl->store_counter += is_first ? 2 : 1; |
| 554 | |
| 555 | const int inc = |
| 556 | (y_tx_wide_unit >= y_tx_high_unit) ? 1 : CFL_SUB8X8_VAL_MI_SIZE; |
| 557 | uint16_t *sub8x8_val = cfl->sub8x8_val + (row * CFL_SUB8X8_VAL_MI_SIZE + col); |
| 558 | for (int i = 0; i < y_tx_unit_len; i++) { |
| 559 | *sub8x8_val = cfl->store_counter; |
| 560 | sub8x8_val += inc; |
| 561 | } |
| 562 | |
| 563 | if (!is_first) { |
| 564 | const uint16_t prev_store_counter = cfl->store_counter - 1; |
| 565 | int found = 0; |
| 566 | sub8x8_val = cfl->sub8x8_val; |
| 567 | for (int y = 0; y < CFL_SUB8X8_VAL_MI_SIZE; y++) { |
| 568 | for (int x = 0; x < CFL_SUB8X8_VAL_MI_SIZE; x++) { |
| 569 | if (sub8x8_val[x] == prev_store_counter) { |
| 570 | sub8x8_val[x] = cfl->store_counter; |
| 571 | found = 1; |
| 572 | } |
| 573 | } |
| 574 | sub8x8_val += CFL_SUB8X8_VAL_MI_SIZE; |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 575 | } |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 576 | // Something is wrong if (0,0) is missing |
| 577 | assert(found); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | #endif // CONFIG_DEBUG |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 581 | |
| 582 | void cfl_store_tx(MACROBLOCKD *const xd, int row, int col, TX_SIZE tx_size, |
| 583 | BLOCK_SIZE bsize) { |
| 584 | CFL_CTX *const cfl = xd->cfl; |
| 585 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
| 586 | uint8_t *dst = |
| 587 | &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]]; |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 588 | if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) { |
| 589 | // Only dimensions of size 4 can have an odd offset. |
| 590 | assert(!((col & 1) && tx_size_wide[tx_size] != 4)); |
| 591 | assert(!((row & 1) && tx_size_high[tx_size] != 4)); |
| 592 | sub8x8_adjust_offset(cfl, &row, &col); |
| 593 | #if CONFIG_DEBUG |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 594 | sub8x8_set_val(cfl, row, col, tx_size); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 595 | #endif // CONFIG_DEBUG |
| 596 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 597 | cfl_store(cfl, dst, pd->dst.stride, row, col, tx_size_wide[tx_size], |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 598 | tx_size_high[tx_size], get_bitdepth_data_path_index(xd)); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void cfl_store_block(MACROBLOCKD *const xd, BLOCK_SIZE bsize, TX_SIZE tx_size) { |
| 602 | CFL_CTX *const cfl = xd->cfl; |
| 603 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
| 604 | int row = 0; |
| 605 | int col = 0; |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 606 | bsize = AOMMAX(BLOCK_4X4, bsize); |
| 607 | if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) { |
| 608 | sub8x8_adjust_offset(cfl, &row, &col); |
| 609 | #if CONFIG_DEBUG |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 610 | // Point to the last transform block inside the partition. |
| 611 | const int off_row = |
| 612 | row + (mi_size_high[bsize] - tx_size_high_unit[tx_size]); |
| 613 | const int off_col = |
| 614 | col + (mi_size_wide[bsize] - tx_size_wide_unit[tx_size]); |
| 615 | sub8x8_set_val(cfl, off_row, off_col, tx_size); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 616 | #endif // CONFIG_DEBUG |
| 617 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 618 | const int width = max_intra_block_width(xd, bsize, AOM_PLANE_Y, tx_size); |
| 619 | const int height = max_intra_block_height(xd, bsize, AOM_PLANE_Y, tx_size); |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 620 | cfl_store(cfl, pd->dst.buf, pd->dst.stride, row, col, width, height, |
| 621 | get_bitdepth_data_path_index(xd)); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 622 | } |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 623 | |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 624 | void cfl_compute_parameters(MACROBLOCKD *const xd, TX_SIZE tx_size) { |
| 625 | CFL_CTX *const cfl = xd->cfl; |
| 626 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 627 | |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 628 | // Do not call cfl_compute_parameters multiple time on the same values. |
| 629 | assert(cfl->are_parameters_computed == 0); |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 630 | |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 631 | const BLOCK_SIZE plane_bsize = AOMMAX( |
| 632 | BLOCK_4X4, get_plane_block_size(mbmi->sb_type, &xd->plane[AOM_PLANE_U])); |
Luc Trudeau | c84c21c | 2017-07-25 19:40:34 -0400 | [diff] [blame] | 633 | #if CONFIG_DEBUG |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 634 | BLOCK_SIZE bsize = mbmi->sb_type; |
| 635 | if (block_size_high[bsize] == 4 || block_size_wide[bsize] == 4) { |
| 636 | const uint16_t compute_counter = cfl->sub8x8_val[0]; |
| 637 | assert(compute_counter != cfl->last_compute_counter); |
| 638 | bsize = scale_chroma_bsize(bsize, cfl->subsampling_x, cfl->subsampling_y); |
| 639 | const int val_wide = mi_size_wide[bsize]; |
| 640 | const int val_high = mi_size_high[bsize]; |
| 641 | assert(val_wide <= CFL_SUB8X8_VAL_MI_SIZE); |
| 642 | assert(val_high <= CFL_SUB8X8_VAL_MI_SIZE); |
| 643 | for (int val_r = 0; val_r < val_high; val_r++) { |
| 644 | for (int val_c = 0; val_c < val_wide; val_c++) { |
| 645 | // If all counters in the validation buffer are equal then they are all |
| 646 | // related to the same chroma reference block. |
| 647 | assert(cfl->sub8x8_val[val_r * CFL_SUB8X8_VAL_MI_SIZE + val_c] == |
| 648 | compute_counter); |
Luc Trudeau | c84c21c | 2017-07-25 19:40:34 -0400 | [diff] [blame] | 649 | } |
| 650 | } |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 651 | cfl->last_compute_counter = compute_counter; |
Luc Trudeau | c84c21c | 2017-07-25 19:40:34 -0400 | [diff] [blame] | 652 | } |
| 653 | #endif // CONFIG_DEBUG |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 654 | // AOM_PLANE_U is used, but both planes will have the same sizes. |
| 655 | cfl->uv_width = max_intra_block_width(xd, plane_bsize, AOM_PLANE_U, tx_size); |
| 656 | cfl->uv_height = |
| 657 | max_intra_block_height(xd, plane_bsize, AOM_PLANE_U, tx_size); |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 658 | |
Luc Trudeau | ace7ffb | 2017-09-29 16:54:05 -0400 | [diff] [blame] | 659 | cfl_dc_pred(xd, plane_bsize, tx_size); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 660 | cfl_subtract_averages(cfl, tx_size); |
Luc Trudeau | 3dc55e0 | 2017-06-22 14:03:47 -0400 | [diff] [blame] | 661 | cfl->are_parameters_computed = 1; |
Luc Trudeau | baeb375 | 2017-04-24 11:19:25 -0400 | [diff] [blame] | 662 | } |