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 <assert.h>
13 #include <math.h>
14 #include <string.h>
15
16 #include "config/aom_scale_rtcd.h"
17
18 #include "aom/aom_integer.h"
19 #include "av1/common/av1_common_int.h"
20 #include "av1/common/cdef.h"
21 #include "av1/common/cdef_block.h"
22 #include "av1/common/reconinter.h"
23
is_8x8_block_skip(MB_MODE_INFO ** grid,int mi_row,int mi_col,int mi_stride)24 static int is_8x8_block_skip(MB_MODE_INFO **grid, int mi_row, int mi_col,
25 int mi_stride) {
26 MB_MODE_INFO **mbmi = grid + mi_row * mi_stride + mi_col;
27 for (int r = 0; r < mi_size_high[BLOCK_8X8]; ++r, mbmi += mi_stride) {
28 for (int c = 0; c < mi_size_wide[BLOCK_8X8]; ++c) {
29 if (!mbmi[c]->skip) return 0;
30 }
31 }
32
33 return 1;
34 }
35
av1_cdef_compute_sb_list(const CommonModeInfoParams * const mi_params,int mi_row,int mi_col,cdef_list * dlist,BLOCK_SIZE bs)36 int av1_cdef_compute_sb_list(const CommonModeInfoParams *const mi_params,
37 int mi_row, int mi_col, cdef_list *dlist,
38 BLOCK_SIZE bs) {
39 MB_MODE_INFO **grid = mi_params->mi_grid_base;
40 int maxc = mi_params->mi_cols - mi_col;
41 int maxr = mi_params->mi_rows - mi_row;
42
43 if (bs == BLOCK_128X128 || bs == BLOCK_128X64)
44 maxc = AOMMIN(maxc, MI_SIZE_128X128);
45 else
46 maxc = AOMMIN(maxc, MI_SIZE_64X64);
47 if (bs == BLOCK_128X128 || bs == BLOCK_64X128)
48 maxr = AOMMIN(maxr, MI_SIZE_128X128);
49 else
50 maxr = AOMMIN(maxr, MI_SIZE_64X64);
51
52 const int r_step = 2; // mi_size_high[BLOCK_8X8]
53 const int c_step = 2; // mi_size_wide[BLOCK_8X8]
54 const int r_shift = 1;
55 const int c_shift = 1;
56 int count = 0;
57 for (int r = 0; r < maxr; r += r_step) {
58 for (int c = 0; c < maxc; c += c_step) {
59 if (!is_8x8_block_skip(grid, mi_row + r, mi_col + c,
60 mi_params->mi_stride)) {
61 dlist[count].by = r >> r_shift;
62 dlist[count].bx = c >> c_shift;
63 count++;
64 }
65 }
66 }
67 return count;
68 }
69
cdef_copy_rect8_8bit_to_16bit_c(uint16_t * dst,int dstride,const uint8_t * src,int sstride,int v,int h)70 void cdef_copy_rect8_8bit_to_16bit_c(uint16_t *dst, int dstride,
71 const uint8_t *src, int sstride, int v,
72 int h) {
73 for (int i = 0; i < v; i++) {
74 for (int j = 0; j < h; j++) {
75 dst[i * dstride + j] = src[i * sstride + j];
76 }
77 }
78 }
79
cdef_copy_rect8_16bit_to_16bit_c(uint16_t * dst,int dstride,const uint16_t * src,int sstride,int v,int h)80 void cdef_copy_rect8_16bit_to_16bit_c(uint16_t *dst, int dstride,
81 const uint16_t *src, int sstride, int v,
82 int h) {
83 for (int i = 0; i < v; i++) {
84 for (int j = 0; j < h; j++) {
85 dst[i * dstride + j] = src[i * sstride + j];
86 }
87 }
88 }
89
copy_sb8_16(AV1_COMMON * cm,uint16_t * dst,int dstride,const uint8_t * src,int src_voffset,int src_hoffset,int sstride,int vsize,int hsize)90 static void copy_sb8_16(AV1_COMMON *cm, uint16_t *dst, int dstride,
91 const uint8_t *src, int src_voffset, int src_hoffset,
92 int sstride, int vsize, int hsize) {
93 if (cm->seq_params.use_highbitdepth) {
94 const uint16_t *base =
95 &CONVERT_TO_SHORTPTR(src)[src_voffset * sstride + src_hoffset];
96 cdef_copy_rect8_16bit_to_16bit(dst, dstride, base, sstride, vsize, hsize);
97 } else {
98 const uint8_t *base = &src[src_voffset * sstride + src_hoffset];
99 cdef_copy_rect8_8bit_to_16bit(dst, dstride, base, sstride, vsize, hsize);
100 }
101 }
102
fill_rect(uint16_t * dst,int dstride,int v,int h,uint16_t x)103 static INLINE void fill_rect(uint16_t *dst, int dstride, int v, int h,
104 uint16_t x) {
105 for (int i = 0; i < v; i++) {
106 for (int j = 0; j < h; j++) {
107 dst[i * dstride + j] = x;
108 }
109 }
110 }
111
copy_rect(uint16_t * dst,int dstride,const uint16_t * src,int sstride,int v,int h)112 static INLINE void copy_rect(uint16_t *dst, int dstride, const uint16_t *src,
113 int sstride, int v, int h) {
114 for (int i = 0; i < v; i++) {
115 for (int j = 0; j < h; j++) {
116 dst[i * dstride + j] = src[i * sstride + j];
117 }
118 }
119 }
120
av1_cdef_frame(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,MACROBLOCKD * xd)121 void av1_cdef_frame(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
122 MACROBLOCKD *xd) {
123 const CdefInfo *const cdef_info = &cm->cdef_info;
124 const CommonModeInfoParams *const mi_params = &cm->mi_params;
125 const int num_planes = av1_num_planes(cm);
126 DECLARE_ALIGNED(16, uint16_t, src[CDEF_INBUF_SIZE]);
127 uint16_t *linebuf[3];
128 uint16_t *colbuf[3];
129 cdef_list dlist[MI_SIZE_64X64 * MI_SIZE_64X64];
130 unsigned char *row_cdef, *prev_row_cdef, *curr_row_cdef;
131 int cdef_count;
132 int dir[CDEF_NBLOCKS][CDEF_NBLOCKS] = { { 0 } };
133 int var[CDEF_NBLOCKS][CDEF_NBLOCKS] = { { 0 } };
134 int mi_wide_l2[3];
135 int mi_high_l2[3];
136 int xdec[3];
137 int ydec[3];
138 int coeff_shift = AOMMAX(cm->seq_params.bit_depth - 8, 0);
139 const int nvfb = (mi_params->mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
140 const int nhfb = (mi_params->mi_cols + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
141 av1_setup_dst_planes(xd->plane, cm->seq_params.sb_size, frame, 0, 0, 0,
142 num_planes);
143 row_cdef = aom_malloc(sizeof(*row_cdef) * (nhfb + 2) * 2);
144 memset(row_cdef, 1, sizeof(*row_cdef) * (nhfb + 2) * 2);
145 prev_row_cdef = row_cdef + 1;
146 curr_row_cdef = prev_row_cdef + nhfb + 2;
147 for (int pli = 0; pli < num_planes; pli++) {
148 xdec[pli] = xd->plane[pli].subsampling_x;
149 ydec[pli] = xd->plane[pli].subsampling_y;
150 mi_wide_l2[pli] = MI_SIZE_LOG2 - xd->plane[pli].subsampling_x;
151 mi_high_l2[pli] = MI_SIZE_LOG2 - xd->plane[pli].subsampling_y;
152 }
153 const int stride = (mi_params->mi_cols << MI_SIZE_LOG2) + 2 * CDEF_HBORDER;
154 for (int pli = 0; pli < num_planes; pli++) {
155 linebuf[pli] = aom_malloc(sizeof(*linebuf) * CDEF_VBORDER * stride);
156 colbuf[pli] =
157 aom_malloc(sizeof(*colbuf) *
158 ((CDEF_BLOCKSIZE << mi_high_l2[pli]) + 2 * CDEF_VBORDER) *
159 CDEF_HBORDER);
160 }
161 for (int fbr = 0; fbr < nvfb; fbr++) {
162 for (int pli = 0; pli < num_planes; pli++) {
163 const int block_height =
164 (MI_SIZE_64X64 << mi_high_l2[pli]) + 2 * CDEF_VBORDER;
165 fill_rect(colbuf[pli], CDEF_HBORDER, block_height, CDEF_HBORDER,
166 CDEF_VERY_LARGE);
167 }
168 int cdef_left = 1;
169 for (int fbc = 0; fbc < nhfb; fbc++) {
170 int level, sec_strength;
171 int uv_level, uv_sec_strength;
172 int nhb, nvb;
173 int cstart = 0;
174 curr_row_cdef[fbc] = 0;
175 if (mi_params->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
176 MI_SIZE_64X64 * fbc] == NULL ||
177 mi_params
178 ->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
179 MI_SIZE_64X64 * fbc]
180 ->cdef_strength == -1) {
181 cdef_left = 0;
182 continue;
183 }
184 if (!cdef_left) cstart = -CDEF_HBORDER;
185 nhb = AOMMIN(MI_SIZE_64X64, mi_params->mi_cols - MI_SIZE_64X64 * fbc);
186 nvb = AOMMIN(MI_SIZE_64X64, mi_params->mi_rows - MI_SIZE_64X64 * fbr);
187 int frame_top, frame_left, frame_bottom, frame_right;
188
189 int mi_row = MI_SIZE_64X64 * fbr;
190 int mi_col = MI_SIZE_64X64 * fbc;
191 // for the current filter block, it's top left corner mi structure (mi_tl)
192 // is first accessed to check whether the top and left boundaries are
193 // frame boundaries. Then bottom-left and top-right mi structures are
194 // accessed to check whether the bottom and right boundaries
195 // (respectively) are frame boundaries.
196 //
197 // Note that we can't just check the bottom-right mi structure - eg. if
198 // we're at the right-hand edge of the frame but not the bottom, then
199 // the bottom-right mi is NULL but the bottom-left is not.
200 frame_top = (mi_row == 0) ? 1 : 0;
201 frame_left = (mi_col == 0) ? 1 : 0;
202
203 if (fbr != nvfb - 1)
204 frame_bottom = (mi_row + MI_SIZE_64X64 == mi_params->mi_rows) ? 1 : 0;
205 else
206 frame_bottom = 1;
207
208 if (fbc != nhfb - 1)
209 frame_right = (mi_col + MI_SIZE_64X64 == mi_params->mi_cols) ? 1 : 0;
210 else
211 frame_right = 1;
212
213 const int mbmi_cdef_strength =
214 mi_params
215 ->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
216 MI_SIZE_64X64 * fbc]
217 ->cdef_strength;
218 level =
219 cdef_info->cdef_strengths[mbmi_cdef_strength] / CDEF_SEC_STRENGTHS;
220 sec_strength =
221 cdef_info->cdef_strengths[mbmi_cdef_strength] % CDEF_SEC_STRENGTHS;
222 sec_strength += sec_strength == 3;
223 uv_level =
224 cdef_info->cdef_uv_strengths[mbmi_cdef_strength] / CDEF_SEC_STRENGTHS;
225 uv_sec_strength =
226 cdef_info->cdef_uv_strengths[mbmi_cdef_strength] % CDEF_SEC_STRENGTHS;
227 uv_sec_strength += uv_sec_strength == 3;
228 if ((level == 0 && sec_strength == 0 && uv_level == 0 &&
229 uv_sec_strength == 0) ||
230 (cdef_count = av1_cdef_compute_sb_list(mi_params, fbr * MI_SIZE_64X64,
231 fbc * MI_SIZE_64X64, dlist,
232 BLOCK_64X64)) == 0) {
233 cdef_left = 0;
234 continue;
235 }
236
237 curr_row_cdef[fbc] = 1;
238 for (int pli = 0; pli < num_planes; pli++) {
239 int coffset;
240 int rend, cend;
241 int damping = cdef_info->cdef_damping;
242 int hsize = nhb << mi_wide_l2[pli];
243 int vsize = nvb << mi_high_l2[pli];
244
245 if (pli) {
246 level = uv_level;
247 sec_strength = uv_sec_strength;
248 }
249
250 if (fbc == nhfb - 1)
251 cend = hsize;
252 else
253 cend = hsize + CDEF_HBORDER;
254
255 if (fbr == nvfb - 1)
256 rend = vsize;
257 else
258 rend = vsize + CDEF_VBORDER;
259
260 coffset = fbc * MI_SIZE_64X64 << mi_wide_l2[pli];
261 if (fbc == nhfb - 1) {
262 /* On the last superblock column, fill in the right border with
263 CDEF_VERY_LARGE to avoid filtering with the outside. */
264 fill_rect(&src[cend + CDEF_HBORDER], CDEF_BSTRIDE,
265 rend + CDEF_VBORDER, hsize + CDEF_HBORDER - cend,
266 CDEF_VERY_LARGE);
267 }
268 if (fbr == nvfb - 1) {
269 /* On the last superblock row, fill in the bottom border with
270 CDEF_VERY_LARGE to avoid filtering with the outside. */
271 fill_rect(&src[(rend + CDEF_VBORDER) * CDEF_BSTRIDE], CDEF_BSTRIDE,
272 CDEF_VBORDER, hsize + 2 * CDEF_HBORDER, CDEF_VERY_LARGE);
273 }
274 /* Copy in the pixels we need from the current superblock for
275 deringing.*/
276 copy_sb8_16(cm,
277 &src[CDEF_VBORDER * CDEF_BSTRIDE + CDEF_HBORDER + cstart],
278 CDEF_BSTRIDE, xd->plane[pli].dst.buf,
279 (MI_SIZE_64X64 << mi_high_l2[pli]) * fbr, coffset + cstart,
280 xd->plane[pli].dst.stride, rend, cend - cstart);
281 if (!prev_row_cdef[fbc]) {
282 copy_sb8_16(cm, &src[CDEF_HBORDER], CDEF_BSTRIDE,
283 xd->plane[pli].dst.buf,
284 (MI_SIZE_64X64 << mi_high_l2[pli]) * fbr - CDEF_VBORDER,
285 coffset, xd->plane[pli].dst.stride, CDEF_VBORDER, hsize);
286 } else if (fbr > 0) {
287 copy_rect(&src[CDEF_HBORDER], CDEF_BSTRIDE, &linebuf[pli][coffset],
288 stride, CDEF_VBORDER, hsize);
289 } else {
290 fill_rect(&src[CDEF_HBORDER], CDEF_BSTRIDE, CDEF_VBORDER, hsize,
291 CDEF_VERY_LARGE);
292 }
293 if (!prev_row_cdef[fbc - 1]) {
294 copy_sb8_16(cm, src, CDEF_BSTRIDE, xd->plane[pli].dst.buf,
295 (MI_SIZE_64X64 << mi_high_l2[pli]) * fbr - CDEF_VBORDER,
296 coffset - CDEF_HBORDER, xd->plane[pli].dst.stride,
297 CDEF_VBORDER, CDEF_HBORDER);
298 } else if (fbr > 0 && fbc > 0) {
299 copy_rect(src, CDEF_BSTRIDE, &linebuf[pli][coffset - CDEF_HBORDER],
300 stride, CDEF_VBORDER, CDEF_HBORDER);
301 } else {
302 fill_rect(src, CDEF_BSTRIDE, CDEF_VBORDER, CDEF_HBORDER,
303 CDEF_VERY_LARGE);
304 }
305 if (!prev_row_cdef[fbc + 1]) {
306 copy_sb8_16(cm, &src[CDEF_HBORDER + (nhb << mi_wide_l2[pli])],
307 CDEF_BSTRIDE, xd->plane[pli].dst.buf,
308 (MI_SIZE_64X64 << mi_high_l2[pli]) * fbr - CDEF_VBORDER,
309 coffset + hsize, xd->plane[pli].dst.stride, CDEF_VBORDER,
310 CDEF_HBORDER);
311 } else if (fbr > 0 && fbc < nhfb - 1) {
312 copy_rect(&src[hsize + CDEF_HBORDER], CDEF_BSTRIDE,
313 &linebuf[pli][coffset + hsize], stride, CDEF_VBORDER,
314 CDEF_HBORDER);
315 } else {
316 fill_rect(&src[hsize + CDEF_HBORDER], CDEF_BSTRIDE, CDEF_VBORDER,
317 CDEF_HBORDER, CDEF_VERY_LARGE);
318 }
319 if (cdef_left) {
320 /* If we deringed the superblock on the left then we need to copy in
321 saved pixels. */
322 copy_rect(src, CDEF_BSTRIDE, colbuf[pli], CDEF_HBORDER,
323 rend + CDEF_VBORDER, CDEF_HBORDER);
324 }
325 /* Saving pixels in case we need to dering the superblock on the
326 right. */
327 copy_rect(colbuf[pli], CDEF_HBORDER, src + hsize, CDEF_BSTRIDE,
328 rend + CDEF_VBORDER, CDEF_HBORDER);
329 copy_sb8_16(
330 cm, &linebuf[pli][coffset], stride, xd->plane[pli].dst.buf,
331 (MI_SIZE_64X64 << mi_high_l2[pli]) * (fbr + 1) - CDEF_VBORDER,
332 coffset, xd->plane[pli].dst.stride, CDEF_VBORDER, hsize);
333
334 if (frame_top) {
335 fill_rect(src, CDEF_BSTRIDE, CDEF_VBORDER, hsize + 2 * CDEF_HBORDER,
336 CDEF_VERY_LARGE);
337 }
338 if (frame_left) {
339 fill_rect(src, CDEF_BSTRIDE, vsize + 2 * CDEF_VBORDER, CDEF_HBORDER,
340 CDEF_VERY_LARGE);
341 }
342 if (frame_bottom) {
343 fill_rect(&src[(vsize + CDEF_VBORDER) * CDEF_BSTRIDE], CDEF_BSTRIDE,
344 CDEF_VBORDER, hsize + 2 * CDEF_HBORDER, CDEF_VERY_LARGE);
345 }
346 if (frame_right) {
347 fill_rect(&src[hsize + CDEF_HBORDER], CDEF_BSTRIDE,
348 vsize + 2 * CDEF_VBORDER, CDEF_HBORDER, CDEF_VERY_LARGE);
349 }
350
351 if (cm->seq_params.use_highbitdepth) {
352 av1_cdef_filter_fb(
353 NULL,
354 &CONVERT_TO_SHORTPTR(
355 xd->plane[pli]
356 .dst.buf)[xd->plane[pli].dst.stride *
357 (MI_SIZE_64X64 * fbr << mi_high_l2[pli]) +
358 (fbc * MI_SIZE_64X64 << mi_wide_l2[pli])],
359 xd->plane[pli].dst.stride,
360 &src[CDEF_VBORDER * CDEF_BSTRIDE + CDEF_HBORDER], xdec[pli],
361 ydec[pli], dir, NULL, var, pli, dlist, cdef_count, level,
362 sec_strength, damping, coeff_shift);
363 } else {
364 av1_cdef_filter_fb(
365 &xd->plane[pli]
366 .dst.buf[xd->plane[pli].dst.stride *
367 (MI_SIZE_64X64 * fbr << mi_high_l2[pli]) +
368 (fbc * MI_SIZE_64X64 << mi_wide_l2[pli])],
369 NULL, xd->plane[pli].dst.stride,
370 &src[CDEF_VBORDER * CDEF_BSTRIDE + CDEF_HBORDER], xdec[pli],
371 ydec[pli], dir, NULL, var, pli, dlist, cdef_count, level,
372 sec_strength, damping, coeff_shift);
373 }
374 }
375 cdef_left = 1;
376 }
377 {
378 unsigned char *tmp = prev_row_cdef;
379 prev_row_cdef = curr_row_cdef;
380 curr_row_cdef = tmp;
381 }
382 }
383 aom_free(row_cdef);
384 for (int pli = 0; pli < num_planes; pli++) {
385 aom_free(linebuf[pli]);
386 aom_free(colbuf[pli]);
387 }
388 }
389