blob: 78f55e358d39dd5c58458bc9e1d64647db9e6c93 [file] [log] [blame]
Luc Trudeauf8164152017-04-11 16:20:51 -04001/*
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 Trudeaubaeb3752017-04-24 11:19:25 -040014#include "av1/common/onyxc_int.h"
15
Luc Trudeaudac5e392017-06-05 15:52:02 -040016void cfl_init(CFL_CTX *cfl, AV1_COMMON *cm) {
Luc Trudeau06b47082017-10-31 10:42:36 -040017 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 Trudeaubaeb3752017-04-24 11:19:25 -040024 }
Luc Trudeau4e26d662017-09-11 13:08:40 -040025 memset(&cfl->pred_buf_q3, 0, sizeof(cfl->pred_buf_q3));
Luc Trudeaudac5e392017-06-05 15:52:02 -040026 cfl->subsampling_x = cm->subsampling_x;
27 cfl->subsampling_y = cm->subsampling_y;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040028 cfl->are_parameters_computed = 0;
Luc Trudeaufcca37a2017-08-14 15:05:07 -040029 cfl->store_y = 0;
Hui Su9fa96232017-10-23 15:46:04 -070030#if CONFIG_DEBUG
Luc Trudeauc84c21c2017-07-25 19:40:34 -040031 cfl_clear_sub8x8_val(cfl);
Luc Trudeauc7af36d2017-10-11 21:01:00 -040032 cfl->store_counter = 0;
33 cfl->last_compute_counter = 0;
Hui Su9fa96232017-10-23 15:46:04 -070034#endif // CONFIG_DEBUG
Luc Trudeau3dc55e02017-06-22 14:03:47 -040035}
36
Luc Trudeau4e26d662017-09-11 13:08:40 -040037// 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.
40static 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 Trudeau3dc55e02017-06-22 14:03:47 -040043
44 if (diff_width > 0) {
Luc Trudeau4e26d662017-09-11 13:08:40 -040045 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 Trudeaue67377b2017-10-31 16:08:05 -040048 const int16_t last_pixel = pred_buf_q3[-1];
Luc Trudeau3dc55e02017-06-22 14:03:47 -040049 for (int i = 0; i < diff_width; i++) {
Luc Trudeau4e26d662017-09-11 13:08:40 -040050 pred_buf_q3[i] = last_pixel;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040051 }
Luc Trudeau4e26d662017-09-11 13:08:40 -040052 pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040053 }
Luc Trudeau4e26d662017-09-11 13:08:40 -040054 cfl->buf_width = width;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040055 }
Luc Trudeau3dc55e02017-06-22 14:03:47 -040056 if (diff_height > 0) {
Luc Trudeau4e26d662017-09-11 13:08:40 -040057 int16_t *pred_buf_q3 =
58 cfl->pred_buf_q3 + ((height - diff_height) * MAX_SB_SIZE);
Luc Trudeau3dc55e02017-06-22 14:03:47 -040059 for (int j = 0; j < diff_height; j++) {
Luc Trudeau4e26d662017-09-11 13:08:40 -040060 const int16_t *last_row_q3 = pred_buf_q3 - MAX_SB_SIZE;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040061 for (int i = 0; i < width; i++) {
Luc Trudeau4e26d662017-09-11 13:08:40 -040062 pred_buf_q3[i] = last_row_q3[i];
Luc Trudeau3dc55e02017-06-22 14:03:47 -040063 }
Luc Trudeau4e26d662017-09-11 13:08:40 -040064 pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040065 }
Luc Trudeau4e26d662017-09-11 13:08:40 -040066 cfl->buf_height = height;
Luc Trudeau3dc55e02017-06-22 14:03:47 -040067 }
Luc Trudeaubaeb3752017-04-24 11:19:25 -040068}
Luc Trudeauf8164152017-04-11 16:20:51 -040069
Luc Trudeau056d1f42017-09-15 17:38:14 -040070static 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 Trudeau13281d42017-10-03 10:46:49 -040072 int sum_u = 0;
73 int sum_v = 0;
74 for (int i = 0; i < width; i++) {
Luc Trudeau056d1f42017-09-15 17:38:14 -040075 sum_u += above_u[i];
76 sum_v += above_v[i];
Luc Trudeau13281d42017-10-03 10:46:49 -040077 }
78 *out_sum_u += sum_u;
79 *out_sum_v += sum_v;
80}
Luc Trudeau056d1f42017-09-15 17:38:14 -040081#if CONFIG_HIGHBITDEPTH
82static 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 Trudeau13281d42017-10-03 10:46:49 -040094
Luc Trudeau056d1f42017-09-15 17:38:14 -040095static 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
114static 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
127static 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 Trudeau13281d42017-10-03 10:46:49 -0400140static 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 Trudeau056d1f42017-09-15 17:38:14 -0400145#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 Trudeau13281d42017-10-03 10:46:49 -0400152 }
Luc Trudeau056d1f42017-09-15 17:38:14 -0400153#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 Trudeau13281d42017-10-03 10:46:49 -0400158}
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 Trudeauace7ffb2017-09-29 16:54:05 -0400162static void cfl_dc_pred(MACROBLOCKD *xd, BLOCK_SIZE plane_bsize,
163 TX_SIZE tx_size) {
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400164 CFL_CTX *const cfl = xd->cfl;
Luc Trudeaue7f9e162017-06-23 21:20:20 -0400165
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 Trudeau23a3b212017-04-28 11:36:59 -0400172 // Number of pixel on the top and left borders.
Luc Trudeau2e6cb7e2017-07-04 12:41:53 -0400173 const int num_pel = width + height;
Luc Trudeauf8164152017-04-11 16:20:51 -0400174
Luc Trudeau23a3b212017-04-28 11:36:59 -0400175 int sum_u = 0;
176 int sum_v = 0;
Luc Trudeauf8164152017-04-11 16:20:51 -0400177
Hui Su9fa96232017-10-23 15:46:04 -0700178 // 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 Trudeauf8164152017-04-11 16:20:51 -0400186
Luc Trudeau7f0f6c42017-05-16 10:35:49 -0400187 if (xd->chroma_up_available && xd->mb_to_right_edge >= 0) {
Luc Trudeau13281d42017-10-03 10:46:49 -0400188 sum_above_row(xd, width, &sum_u, &sum_v);
Luc Trudeauf8164152017-04-11 16:20:51 -0400189 } else {
Luc Trudeau13281d42017-10-03 10:46:49 -0400190 const int base = 128 << (xd->bd - 8);
Luc Trudeau71ad78c2017-09-28 16:08:14 -0400191 sum_u = width * (base - 1);
192 sum_v = width * (base - 1);
Luc Trudeauf8164152017-04-11 16:20:51 -0400193 }
194
Luc Trudeau7f0f6c42017-05-16 10:35:49 -0400195 if (xd->chroma_left_available && xd->mb_to_bottom_edge >= 0) {
Luc Trudeau13281d42017-10-03 10:46:49 -0400196 sum_left_col(xd, height, &sum_u, &sum_v);
Luc Trudeauf8164152017-04-11 16:20:51 -0400197 } else {
Luc Trudeau13281d42017-10-03 10:46:49 -0400198 const int base = 128 << (xd->bd - 8);
Luc Trudeau71ad78c2017-09-28 16:08:14 -0400199 sum_u += height * (base + 1);
200 sum_v += height * (base + 1);
Luc Trudeauf8164152017-04-11 16:20:51 -0400201 }
202
Luc Trudeau780d2492017-06-15 22:26:41 -0400203 // 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 Trudeauace7ffb2017-09-29 16:54:05 -0400205 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 Trudeauf8164152017-04-11 16:20:51 -0400221}
222
Luc Trudeau593d02c2017-09-08 11:29:37 -0400223static void cfl_subtract_averages(CFL_CTX *cfl, TX_SIZE tx_size) {
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400224 const int width = cfl->uv_width;
225 const int height = cfl->uv_height;
Luc Trudeau03678942017-06-12 17:33:19 -0400226 const int tx_height = tx_size_high[tx_size];
227 const int tx_width = tx_size_wide[tx_size];
Luc Trudeau03678942017-06-12 17:33:19 -0400228 const int block_row_stride = MAX_SB_SIZE << tx_size_high_log2[tx_size];
Luc Trudeaubfe28272017-07-01 12:57:17 -0400229 const int num_pel_log2 =
230 (tx_size_high_log2[tx_size] + tx_size_wide_log2[tx_size]);
231
Luc Trudeau4e26d662017-09-11 13:08:40 -0400232 int16_t *pred_buf_q3 = cfl->pred_buf_q3;
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400233
Luc Trudeau4e26d662017-09-11 13:08:40 -0400234 cfl_pad(cfl, width, height);
235
Luc Trudeau03678942017-06-12 17:33:19 -0400236 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 Trudeaua0af3b52017-09-06 13:37:33 -0400238 int sum_q3 = 0;
Luc Trudeau4e26d662017-09-11 13:08:40 -0400239 int16_t *tx_pred_buf_q3 = pred_buf_q3;
Luc Trudeau03678942017-06-12 17:33:19 -0400240 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 Trudeau4e26d662017-09-11 13:08:40 -0400242 sum_q3 += tx_pred_buf_q3[t_i];
Luc Trudeau03678942017-06-12 17:33:19 -0400243 }
Luc Trudeau4e26d662017-09-11 13:08:40 -0400244 tx_pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau03678942017-06-12 17:33:19 -0400245 }
Luc Trudeau593d02c2017-09-08 11:29:37 -0400246 int avg_q3 = (sum_q3 + (1 << (num_pel_log2 - 1))) >> num_pel_log2;
Luc Trudeau475fc9d2017-07-04 16:51:14 -0400247 // Loss is never more than 1/2 (in Q3)
Yaowu Xu2a91ab72017-11-06 15:12:17 -0800248 assert(abs((avg_q3 * (1 << num_pel_log2)) - sum_q3) <=
249 1 << num_pel_log2 >> 1);
Luc Trudeau593d02c2017-09-08 11:29:37 -0400250
Luc Trudeau4e26d662017-09-11 13:08:40 -0400251 tx_pred_buf_q3 = pred_buf_q3;
Luc Trudeau593d02c2017-09-08 11:29:37 -0400252 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 Trudeau4e26d662017-09-11 13:08:40 -0400254 tx_pred_buf_q3[t_i] -= avg_q3;
Luc Trudeau593d02c2017-09-08 11:29:37 -0400255 }
256
Luc Trudeau4e26d662017-09-11 13:08:40 -0400257 tx_pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau593d02c2017-09-08 11:29:37 -0400258 }
Luc Trudeau3e18e4a2017-06-13 13:54:14 -0400259 }
Luc Trudeau4e26d662017-09-11 13:08:40 -0400260 pred_buf_q3 += block_row_stride;
Luc Trudeau3e18e4a2017-06-13 13:54:14 -0400261 }
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400262}
263
David Michael Barrf6eaa152017-07-19 19:42:28 +0900264static INLINE int cfl_idx_to_alpha(int alpha_idx, int joint_sign,
Luc Trudeau4e81d922017-07-05 17:17:06 -0400265 CFL_PRED_TYPE pred_type) {
David Michael Barrf6eaa152017-07-19 19:42:28 +0900266 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 Trudeau3e18e4a2017-06-13 13:54:14 -0400272}
273
Luc Trudeau056d1f42017-09-15 17:38:14 -0400274static void cfl_build_prediction_lbd(const int16_t *pred_buf_q3, uint8_t *dst,
275 int dst_stride, int width, int height,
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400276 int alpha_q3, const int16_t *dc_pred) {
Luc Trudeau67914b52017-09-14 17:13:28 -0400277 for (int j = 0; j < height; j++) {
278 for (int i = 0; i < width; i++) {
279 dst[i] =
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400280 clip_pixel(get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]) + dc_pred[i]);
Luc Trudeau67914b52017-09-14 17:13:28 -0400281 }
282 dst += dst_stride;
283 pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400284 dc_pred += MAX_SB_SIZE;
Luc Trudeau67914b52017-09-14 17:13:28 -0400285 }
286}
287
Luc Trudeau056d1f42017-09-15 17:38:14 -0400288#if CONFIG_HIGHBITDEPTH
289static void cfl_build_prediction_hbd(const int16_t *pred_buf_q3, uint16_t *dst,
290 int dst_stride, int width, int height,
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400291 int alpha_q3, const int16_t *dc_pred,
Luc Trudeaud3487722017-09-29 15:55:48 -0400292 int bit_depth) {
Luc Trudeau056d1f42017-09-15 17:38:14 -0400293 for (int j = 0; j < height; j++) {
294 for (int i = 0; i < width; i++) {
295 dst[i] = clip_pixel_highbd(
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400296 get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]) + dc_pred[i], bit_depth);
Luc Trudeau056d1f42017-09-15 17:38:14 -0400297 }
298 dst += dst_stride;
299 pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400300 dc_pred += MAX_SB_SIZE;
Luc Trudeau056d1f42017-09-15 17:38:14 -0400301 }
302}
303#endif // CONFIG_HIGHBITDEPTH
304
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400305void 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 Trudeau4e26d662017-09-11 13:08:40 -0400313 const int16_t *pred_buf_q3 =
314 cfl->pred_buf_q3 + ((row * MAX_SB_SIZE + col) << tx_size_wide_log2[0]);
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400315 const int16_t *dc_pred = cfl->dc_pred[plane - 1] +
316 ((row * MAX_SB_SIZE + col) << tx_size_wide_log2[0]);
David Michael Barrf6eaa152017-07-19 19:42:28 +0900317 const int alpha_q3 =
318 cfl_idx_to_alpha(mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, plane - 1);
Luc Trudeaubaeb3752017-04-24 11:19:25 -0400319
Luc Trudeau6d3befb2017-10-02 13:52:22 -0400320#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 Trudeauace7ffb2017-09-29 16:54:05 -0400325 alpha_q3, dc_pred, xd->bd);
Luc Trudeau6d3befb2017-10-02 13:52:22 -0400326 return;
327 }
328#endif // CONFIG_HIGHBITDEPTH
329 cfl_build_prediction_lbd(pred_buf_q3, dst, dst_stride, tx_size_wide[tx_size],
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400330 tx_size_high[tx_size], alpha_q3, dc_pred);
Luc Trudeau4e26d662017-09-11 13:08:40 -0400331}
332
Luc Trudeau056d1f42017-09-15 17:38:14 -0400333static void cfl_luma_subsampling_420_lbd(const uint8_t *input, int input_stride,
334 int16_t *output_q3, int width,
335 int height) {
Luc Trudeau4e26d662017-09-11 13:08:40 -0400336 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 Trudeauc8323c02017-10-11 21:05:54 -0400348static 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 Trudeau06b47082017-10-31 10:42:36 -0400361static 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 Trudeau69d9e872017-09-15 20:40:47 -0400373static 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 Trudeau43ed5712017-10-31 12:29:28 -0400385typedef void (*cfl_subsample_lbd_fn)(const uint8_t *input, int input_stride,
386 int16_t *output_q3, int width, int height);
387
388static 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 Trudeau056d1f42017-09-15 17:38:14 -0400395#if CONFIG_HIGHBITDEPTH
396static 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 Trudeau69d9e872017-09-15 20:40:47 -0400410
Luc Trudeauc8323c02017-10-11 21:05:54 -0400411static 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 Trudeau06b47082017-10-31 10:42:36 -0400424static 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 Trudeau6acb3002017-11-02 14:09:28 -0400429 output_q3[i] = (input[i] + input[i + input_stride]) << 2;
Luc Trudeau06b47082017-10-31 10:42:36 -0400430 }
431 input += input_stride << 1;
432 output_q3 += MAX_SB_SIZE;
433 }
434}
435
Luc Trudeau69d9e872017-09-15 20:40:47 -0400436static 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 Trudeau43ed5712017-10-31 12:29:28 -0400447
448typedef void (*cfl_subsample_hbd_fn)(const uint16_t *input, int input_stride,
449 int16_t *output_q3, int width, int height);
450
451static 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 Trudeau056d1f42017-09-15 17:38:14 -0400457#endif // CONFIG_HIGHBITDEPTH
458
Luc Trudeau43ed5712017-10-31 12:29:28 -0400459static 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 Trudeaue3980282017-04-25 23:17:21 -0400461 const int tx_off_log2 = tx_size_wide_log2[0];
Luc Trudeau4e26d662017-09-11 13:08:40 -0400462 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 Trudeaue3980282017-04-25 23:17:21 -0400468
Luc Trudeau780d2492017-06-15 22:26:41 -0400469 // Invalidate current parameters
470 cfl->are_parameters_computed = 0;
Luc Trudeaue3980282017-04-25 23:17:21 -0400471
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 Trudeau4e26d662017-09-11 13:08:40 -0400476 cfl->buf_width = store_width;
477 cfl->buf_height = store_height;
Luc Trudeaue3980282017-04-25 23:17:21 -0400478 } else {
Luc Trudeau4e26d662017-09-11 13:08:40 -0400479 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 Trudeaue3980282017-04-25 23:17:21 -0400481 }
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400482
Luc Trudeau780d2492017-06-15 22:26:41 -0400483 // Check that we will remain inside the pixel buffer.
Luc Trudeau4e26d662017-09-11 13:08:40 -0400484 assert(store_row + store_height <= MAX_SB_SIZE);
485 assert(store_col + store_width <= MAX_SB_SIZE);
Luc Trudeau780d2492017-06-15 22:26:41 -0400486
487 // Store the input into the CfL pixel buffer
Luc Trudeau4e26d662017-09-11 13:08:40 -0400488 int16_t *pred_buf_q3 =
489 cfl->pred_buf_q3 + (store_row * MAX_SB_SIZE + store_col);
Luc Trudeau780d2492017-06-15 22:26:41 -0400490
Luc Trudeau43ed5712017-10-31 12:29:28 -0400491#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 Trudeau780d2492017-06-15 22:26:41 -0400499 }
Luc Trudeau43ed5712017-10-31 12:29:28 -0400500#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 Trudeaue3980282017-04-25 23:17:21 -0400506}
Luc Trudeau4e26d662017-09-11 13:08:40 -0400507
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400508// 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.
510static 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 Trudeauc7af36d2017-10-11 21:01:00 -0400525// 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.
543static 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 Trudeaub05eeae2017-08-18 15:14:30 -0400575 }
Luc Trudeauc7af36d2017-10-11 21:01:00 -0400576 // Something is wrong if (0,0) is missing
577 assert(found);
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400578 }
579}
580#endif // CONFIG_DEBUG
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400581
582void 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 Trudeaub05eeae2017-08-18 15:14:30 -0400588 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 Trudeauc7af36d2017-10-11 21:01:00 -0400594 sub8x8_set_val(cfl, row, col, tx_size);
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400595#endif // CONFIG_DEBUG
596 }
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400597 cfl_store(cfl, dst, pd->dst.stride, row, col, tx_size_wide[tx_size],
Luc Trudeau056d1f42017-09-15 17:38:14 -0400598 tx_size_high[tx_size], get_bitdepth_data_path_index(xd));
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400599}
600
601void 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 Trudeaub05eeae2017-08-18 15:14:30 -0400606 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 Trudeauc7af36d2017-10-11 21:01:00 -0400610 // 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 Trudeaub05eeae2017-08-18 15:14:30 -0400616#endif // CONFIG_DEBUG
617 }
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400618 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 Trudeau056d1f42017-09-15 17:38:14 -0400620 cfl_store(cfl, pd->dst.buf, pd->dst.stride, row, col, width, height,
621 get_bitdepth_data_path_index(xd));
Luc Trudeaub05eeae2017-08-18 15:14:30 -0400622}
Luc Trudeaubaeb3752017-04-24 11:19:25 -0400623
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400624void 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 Trudeaubaeb3752017-04-24 11:19:25 -0400627
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400628 // Do not call cfl_compute_parameters multiple time on the same values.
629 assert(cfl->are_parameters_computed == 0);
Luc Trudeaubaeb3752017-04-24 11:19:25 -0400630
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400631 const BLOCK_SIZE plane_bsize = AOMMAX(
632 BLOCK_4X4, get_plane_block_size(mbmi->sb_type, &xd->plane[AOM_PLANE_U]));
Luc Trudeauc84c21c2017-07-25 19:40:34 -0400633#if CONFIG_DEBUG
Luc Trudeauc7af36d2017-10-11 21:01:00 -0400634 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 Trudeauc84c21c2017-07-25 19:40:34 -0400649 }
650 }
Luc Trudeauc7af36d2017-10-11 21:01:00 -0400651 cfl->last_compute_counter = compute_counter;
Luc Trudeauc84c21c2017-07-25 19:40:34 -0400652 }
653#endif // CONFIG_DEBUG
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400654 // 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 Trudeaubaeb3752017-04-24 11:19:25 -0400658
Luc Trudeauace7ffb2017-09-29 16:54:05 -0400659 cfl_dc_pred(xd, plane_bsize, tx_size);
Luc Trudeau593d02c2017-09-08 11:29:37 -0400660 cfl_subtract_averages(cfl, tx_size);
Luc Trudeau3dc55e02017-06-22 14:03:47 -0400661 cfl->are_parameters_computed = 1;
Luc Trudeaubaeb3752017-04-24 11:19:25 -0400662}