Coverage Report

Created: 2018-09-25 14:53

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