dering.c 5.7 KB
Newer Older
Yaowu Xu's avatar
Yaowu Xu committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xu's avatar
Yaowu Xu committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xu's avatar
Yaowu Xu committed
10
11
12
13
14
 */

#include <string.h>
#include <math.h>

Yaowu Xu's avatar
Yaowu Xu committed
15
16
#include "./aom_scale_rtcd.h"
#include "aom/aom_integer.h"
17
18
19
20
#include "av1/common/dering.h"
#include "av1/common/onyxc_int.h"
#include "av1/common/reconinter.h"
#include "av1/common/od_dering.h"
Yaowu Xu's avatar
Yaowu Xu committed
21
22

int compute_level_from_index(int global_level, int gi) {
clang-format's avatar
clang-format committed
23
  static const int dering_gains[DERING_REFINEMENT_LEVELS] = { 0, 11, 16, 22 };
Yaowu Xu's avatar
Yaowu Xu committed
24
25
  int level;
  if (global_level == 0) return 0;
clang-format's avatar
clang-format committed
26
27
  level = (global_level * dering_gains[gi] + 8) >> 4;
  return clamp(level, gi, MAX_DERING_LEVEL - 1);
Yaowu Xu's avatar
Yaowu Xu committed
28
29
}

Yaowu Xu's avatar
Yaowu Xu committed
30
int sb_all_skip(const AV1_COMMON *const cm, int mi_row, int mi_col) {
Yaowu Xu's avatar
Yaowu Xu committed
31
32
33
34
35
  int r, c;
  int maxc, maxr;
  int skip = 1;
  maxc = cm->mi_cols - mi_col;
  maxr = cm->mi_rows - mi_row;
36
37
  if (maxr > MAX_MIB_SIZE) maxr = MAX_MIB_SIZE;
  if (maxc > MAX_MIB_SIZE) maxc = MAX_MIB_SIZE;
Yaowu Xu's avatar
Yaowu Xu committed
38
39
40
  for (r = 0; r < maxr; r++) {
    for (c = 0; c < maxc; c++) {
      skip = skip &&
clang-format's avatar
clang-format committed
41
42
             cm->mi_grid_visible[(mi_row + r) * cm->mi_stride + mi_col + c]
                 ->mbmi.skip;
Yaowu Xu's avatar
Yaowu Xu committed
43
44
45
46
47
    }
  }
  return skip;
}

Yaowu Xu's avatar
Yaowu Xu committed
48
49
void av1_dering_frame(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
                      MACROBLOCKD *xd, int global_level) {
Yaowu Xu's avatar
Yaowu Xu committed
50
51
52
53
54
  int r, c;
  int sbr, sbc;
  int nhsb, nvsb;
  od_dering_in *src[3];
  unsigned char *bskip;
clang-format's avatar
clang-format committed
55
  int dir[OD_DERING_NBLOCKS][OD_DERING_NBLOCKS] = { { 0 } };
Yaowu Xu's avatar
Yaowu Xu committed
56
  int stride;
57
58
59
60
  int bsize_x[3];
  int bsize_y[3];
  int dec_x[3];
  int dec_y[3];
Yaowu Xu's avatar
Yaowu Xu committed
61
  int pli;
Yaowu Xu's avatar
Yaowu Xu committed
62
  int coeff_shift = AOMMAX(cm->bit_depth - 8, 0);
63
64
65
66
67
68
  int nplanes;
  if (xd->plane[1].subsampling_x == xd->plane[1].subsampling_y &&
      xd->plane[2].subsampling_x == xd->plane[2].subsampling_y)
    nplanes = 3;
  else
    nplanes = 1;
69
70
  nvsb = (cm->mi_rows + MAX_MIB_SIZE - 1) / MAX_MIB_SIZE;
  nhsb = (cm->mi_cols + MAX_MIB_SIZE - 1) / MAX_MIB_SIZE;
Yaowu Xu's avatar
Yaowu Xu committed
71
72
  bskip = aom_malloc(sizeof(*bskip) * cm->mi_rows * cm->mi_cols);
  av1_setup_dst_planes(xd->plane, frame, 0, 0);
73
  for (pli = 0; pli < nplanes; pli++) {
74
75
76
77
    dec_x[pli] = xd->plane[pli].subsampling_x;
    dec_y[pli] = xd->plane[pli].subsampling_y;
    bsize_x[pli] = 8 >> dec_x[pli];
    bsize_y[pli] = 8 >> dec_y[pli];
Yaowu Xu's avatar
Yaowu Xu committed
78
  }
79
  stride = bsize_x[0] * cm->mi_cols;
80
  for (pli = 0; pli < nplanes; pli++) {
Yaowu Xu's avatar
Yaowu Xu committed
81
    src[pli] = aom_malloc(sizeof(*src) * cm->mi_rows * cm->mi_cols * 64);
82
83
    for (r = 0; r < bsize_y[pli] * cm->mi_rows; ++r) {
      for (c = 0; c < bsize_x[pli] * cm->mi_cols; ++c) {
Yaowu Xu's avatar
Yaowu Xu committed
84
#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
85
        if (cm->use_highbitdepth) {
clang-format's avatar
clang-format committed
86
87
          src[pli][r * stride + c] = CONVERT_TO_SHORTPTR(
              xd->plane[pli].dst.buf)[r * xd->plane[pli].dst.stride + c];
Yaowu Xu's avatar
Yaowu Xu committed
88
89
90
91
        } else {
#endif
          src[pli][r * stride + c] =
              xd->plane[pli].dst.buf[r * xd->plane[pli].dst.stride + c];
Yaowu Xu's avatar
Yaowu Xu committed
92
#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        }
#endif
      }
    }
  }
  for (r = 0; r < cm->mi_rows; ++r) {
    for (c = 0; c < cm->mi_cols; ++c) {
      const MB_MODE_INFO *mbmi =
          &cm->mi_grid_visible[r * cm->mi_stride + c]->mbmi;
      bskip[r * cm->mi_cols + c] = mbmi->skip;
    }
  }
  for (sbr = 0; sbr < nvsb; sbr++) {
    for (sbc = 0; sbc < nhsb; sbc++) {
      int level;
      int nhb, nvb;
Yaowu Xu's avatar
Yaowu Xu committed
109
110
      nhb = AOMMIN(MAX_MIB_SIZE, cm->mi_cols - MAX_MIB_SIZE * sbc);
      nvb = AOMMIN(MAX_MIB_SIZE, cm->mi_rows - MAX_MIB_SIZE * sbr);
111
112
113
114
115
116
      level = compute_level_from_index(
          global_level, cm->mi_grid_visible[MAX_MIB_SIZE * sbr * cm->mi_stride +
                                            MAX_MIB_SIZE * sbc]
                            ->mbmi.dering_gain);
      if (level == 0 || sb_all_skip(cm, sbr * MAX_MIB_SIZE, sbc * MAX_MIB_SIZE))
        continue;
117
      for (pli = 0; pli < nplanes; pli++) {
118
        int16_t dst[MAX_MIB_SIZE * MAX_MIB_SIZE * 8 * 8];
Yaowu Xu's avatar
Yaowu Xu committed
119
120
121
        int threshold;
        /* FIXME: This is a temporary hack that uses more conservative
           deringing for chroma. */
122
123
124
125
126
        if (pli)
          threshold = (level * 5 + 4) >> 3 << coeff_shift;
        else
          threshold = level << coeff_shift;
        if (threshold == 0) continue;
127
128
129
130
131
        od_dering(dst, MAX_MIB_SIZE * bsize_x[pli],
                  &src[pli][sbr * stride * bsize_x[pli] * MAX_MIB_SIZE +
                            sbc * bsize_x[pli] * MAX_MIB_SIZE],
                  stride, nhb, nvb, sbc, sbr, nhsb, nvsb, dec_x[pli],
                  dec_y[pli], dir, pli,
132
                  &bskip[MAX_MIB_SIZE * sbr * cm->mi_cols + MAX_MIB_SIZE * sbc],
133
                  cm->mi_cols, threshold, coeff_shift);
134
135
        for (r = 0; r < bsize_y[pli] * nvb; ++r) {
          for (c = 0; c < bsize_x[pli] * nhb; ++c) {
Yaowu Xu's avatar
Yaowu Xu committed
136
#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
137
138
            if (cm->use_highbitdepth) {
              CONVERT_TO_SHORTPTR(xd->plane[pli].dst.buf)
clang-format's avatar
clang-format committed
139
              [xd->plane[pli].dst.stride *
140
141
142
                   (bsize_x[pli] * MAX_MIB_SIZE * sbr + r) +
               sbc * bsize_x[pli] * MAX_MIB_SIZE + c] =
                  dst[r * MAX_MIB_SIZE * bsize_x[pli] + c];
Yaowu Xu's avatar
Yaowu Xu committed
143
144
            } else {
#endif
145
146
147
148
149
              xd->plane[pli]
                  .dst.buf[xd->plane[pli].dst.stride *
                               (bsize_x[pli] * MAX_MIB_SIZE * sbr + r) +
                           sbc * bsize_x[pli] * MAX_MIB_SIZE + c] =
                  dst[r * MAX_MIB_SIZE * bsize_x[pli] + c];
Yaowu Xu's avatar
Yaowu Xu committed
150
#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
151
152
153
154
155
156
157
            }
#endif
          }
        }
      }
    }
  }
158
  for (pli = 0; pli < nplanes; pli++) {
Yaowu Xu's avatar
Yaowu Xu committed
159
    aom_free(src[pli]);
Yaowu Xu's avatar
Yaowu Xu committed
160
  }
Yaowu Xu's avatar
Yaowu Xu committed
161
  aom_free(bskip);
Yaowu Xu's avatar
Yaowu Xu committed
162
}