Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <string.h> |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 13 | #include <math.h> |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "./aom_scale_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | #include "av1/common/dering.h" |
| 17 | #include "av1/common/onyxc_int.h" |
| 18 | #include "av1/common/reconinter.h" |
| 19 | #include "av1/encoder/encoder.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 20 | #include "aom/aom_integer.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | |
| 22 | static double compute_dist(int16_t *x, int xstride, int16_t *y, int ystride, |
| 23 | int nhb, int nvb, int coeff_shift) { |
| 24 | int i, j; |
| 25 | double sum; |
| 26 | sum = 0; |
| 27 | for (i = 0; i < nvb << 3; i++) { |
| 28 | for (j = 0; j < nhb << 3; j++) { |
| 29 | double tmp; |
| 30 | tmp = x[i * xstride + j] - y[i * ystride + j]; |
| 31 | sum += tmp * tmp; |
| 32 | } |
| 33 | } |
| 34 | return sum / (double)(1 << 2 * coeff_shift); |
| 35 | } |
| 36 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 37 | int av1_dering_search(YV12_BUFFER_CONFIG *frame, const YV12_BUFFER_CONFIG *ref, |
| 38 | AV1_COMMON *cm, MACROBLOCKD *xd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 39 | int r, c; |
| 40 | int sbr, sbc; |
| 41 | int nhsb, nvsb; |
Jean-Marc Valin | 39ee109 | 2016-10-25 19:30:49 -0400 | [diff] [blame] | 42 | int16_t *src; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 43 | int16_t *ref_coeff; |
Jean-Marc Valin | 39d92a0 | 2016-11-02 02:33:46 -0400 | [diff] [blame] | 44 | dering_list dlist[MAX_MIB_SIZE * MAX_MIB_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 45 | int dir[OD_DERING_NBLOCKS][OD_DERING_NBLOCKS] = { { 0 } }; |
| 46 | int stride; |
| 47 | int bsize[3]; |
| 48 | int dec[3]; |
| 49 | int pli; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 50 | int level; |
| 51 | int best_level; |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 52 | int dering_count; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 53 | int coeff_shift = AOMMAX(cm->bit_depth - 8, 0); |
| 54 | src = aom_malloc(sizeof(*src) * cm->mi_rows * cm->mi_cols * 64); |
| 55 | ref_coeff = aom_malloc(sizeof(*ref_coeff) * cm->mi_rows * cm->mi_cols * 64); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 56 | av1_setup_dst_planes(xd->plane, frame, 0, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | for (pli = 0; pli < 3; pli++) { |
| 58 | dec[pli] = xd->plane[pli].subsampling_x; |
Jean-Marc Valin | e254241 | 2016-10-26 01:26:25 -0400 | [diff] [blame] | 59 | bsize[pli] = OD_DERING_SIZE_LOG2 - dec[pli]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 60 | } |
Jean-Marc Valin | 8e94178 | 2016-10-13 14:44:22 -0400 | [diff] [blame] | 61 | stride = cm->mi_cols << bsize[0]; |
| 62 | for (r = 0; r < cm->mi_rows << bsize[0]; ++r) { |
| 63 | for (c = 0; c < cm->mi_cols << bsize[0]; ++c) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 64 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 65 | if (cm->use_highbitdepth) { |
| 66 | src[r * stride + c] = CONVERT_TO_SHORTPTR( |
| 67 | xd->plane[0].dst.buf)[r * xd->plane[0].dst.stride + c]; |
| 68 | ref_coeff[r * stride + c] = |
| 69 | CONVERT_TO_SHORTPTR(ref->y_buffer)[r * ref->y_stride + c]; |
| 70 | } else { |
| 71 | #endif |
| 72 | src[r * stride + c] = |
| 73 | xd->plane[0].dst.buf[r * xd->plane[0].dst.stride + c]; |
| 74 | ref_coeff[r * stride + c] = ref->y_buffer[r * ref->y_stride + c]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 75 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | } |
| 77 | #endif |
| 78 | } |
| 79 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 80 | nvsb = (cm->mi_rows + MAX_MIB_SIZE - 1) / MAX_MIB_SIZE; |
| 81 | nhsb = (cm->mi_cols + MAX_MIB_SIZE - 1) / MAX_MIB_SIZE; |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 82 | /* Pick a base threshold based on the quantizer. The threshold will then be |
| 83 | adjusted on a 64x64 basis. We use a threshold of the form T = a*Q^b, |
| 84 | where a and b are derived empirically trying to optimize rate-distortion |
| 85 | at different quantizer settings. */ |
Jean-Marc Valin | 209f830 | 2016-10-08 14:10:39 -0400 | [diff] [blame] | 86 | best_level = AOMMIN( |
| 87 | MAX_DERING_LEVEL - 1, |
| 88 | (int)floor(.5 + |
| 89 | .45 * pow(av1_ac_quant(cm->base_qindex, 0, cm->bit_depth) >> |
| 90 | (cm->bit_depth - 8), |
| 91 | 0.6))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 92 | for (sbr = 0; sbr < nvsb; sbr++) { |
| 93 | for (sbc = 0; sbc < nhsb; sbc++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 94 | int nvb, nhb; |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 95 | int gi; |
| 96 | int best_gi; |
| 97 | int32_t best_mse = INT32_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 98 | int16_t dst[MAX_MIB_SIZE * MAX_MIB_SIZE * 8 * 8]; |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 99 | int16_t tmp_dst[MAX_MIB_SIZE * MAX_MIB_SIZE * 8 * 8]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 100 | nhb = AOMMIN(MAX_MIB_SIZE, cm->mi_cols - MAX_MIB_SIZE * sbc); |
| 101 | nvb = AOMMIN(MAX_MIB_SIZE, cm->mi_rows - MAX_MIB_SIZE * sbr); |
Jean-Marc Valin | 39d92a0 | 2016-11-02 02:33:46 -0400 | [diff] [blame] | 102 | dering_count = sb_compute_dering_list(cm, sbr * MAX_MIB_SIZE, |
| 103 | sbc * MAX_MIB_SIZE, dlist); |
| 104 | if (dering_count == 0) continue; |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 105 | best_gi = 0; |
| 106 | for (gi = 0; gi < DERING_REFINEMENT_LEVELS; gi++) { |
Yaowu Xu | 9c323bc | 2016-09-01 11:35:16 -0700 | [diff] [blame] | 107 | int cur_mse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 108 | int threshold; |
Jean-Marc Valin | ca1eb5d | 2016-10-13 15:24:39 -0400 | [diff] [blame] | 109 | int16_t inbuf[OD_DERING_INBUF_SIZE]; |
| 110 | int16_t *in; |
| 111 | int i, j; |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 112 | level = compute_level_from_index(best_level, gi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 113 | threshold = level << coeff_shift; |
Jean-Marc Valin | 8e94178 | 2016-10-13 14:44:22 -0400 | [diff] [blame] | 114 | for (r = 0; r < nvb << bsize[0]; r++) { |
| 115 | for (c = 0; c < nhb << bsize[0]; c++) { |
| 116 | dst[(r * MAX_MIB_SIZE << bsize[0]) + c] = |
| 117 | src[((sbr * MAX_MIB_SIZE << bsize[0]) + r) * stride + |
| 118 | (sbc * MAX_MIB_SIZE << bsize[0]) + c]; |
Jean-Marc Valin | bcf3580 | 2016-10-12 17:39:54 -0400 | [diff] [blame] | 119 | } |
| 120 | } |
Jean-Marc Valin | ca1eb5d | 2016-10-13 15:24:39 -0400 | [diff] [blame] | 121 | in = inbuf + OD_FILT_VBORDER * OD_FILT_BSTRIDE + OD_FILT_HBORDER; |
| 122 | /* We avoid filtering the pixels for which some of the pixels to average |
Jean-Marc Valin | 39d92a0 | 2016-11-02 02:33:46 -0400 | [diff] [blame] | 123 | are outside the frame. We could change the filter instead, but it |
| 124 | would |
Jean-Marc Valin | ca1eb5d | 2016-10-13 15:24:39 -0400 | [diff] [blame] | 125 | add special cases for any future vectorization. */ |
Jean-Marc Valin | 39d92a0 | 2016-11-02 02:33:46 -0400 | [diff] [blame] | 126 | for (i = 0; i < OD_DERING_INBUF_SIZE; i++) |
| 127 | inbuf[i] = OD_DERING_VERY_LARGE; |
Jean-Marc Valin | ca1eb5d | 2016-10-13 15:24:39 -0400 | [diff] [blame] | 128 | for (i = -OD_FILT_VBORDER * (sbr != 0); |
| 129 | i < (nvb << bsize[0]) + OD_FILT_VBORDER * (sbr != nvsb - 1); i++) { |
| 130 | for (j = -OD_FILT_HBORDER * (sbc != 0); |
Jean-Marc Valin | 39d92a0 | 2016-11-02 02:33:46 -0400 | [diff] [blame] | 131 | j < (nhb << bsize[0]) + OD_FILT_HBORDER * (sbc != nhsb - 1); |
| 132 | j++) { |
Jean-Marc Valin | ca1eb5d | 2016-10-13 15:24:39 -0400 | [diff] [blame] | 133 | int16_t *x; |
| 134 | x = &src[(sbr * stride * MAX_MIB_SIZE << bsize[0]) + |
| 135 | (sbc * MAX_MIB_SIZE << bsize[0])]; |
| 136 | in[i * OD_FILT_BSTRIDE + j] = x[i * stride + j]; |
| 137 | } |
| 138 | } |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame] | 139 | od_dering(tmp_dst, in, 0, dir, 0, dlist, dering_count, threshold, |
Jean-Marc Valin | 39d92a0 | 2016-11-02 02:33:46 -0400 | [diff] [blame] | 140 | coeff_shift); |
| 141 | copy_dering_16bit_to_16bit(dst, MAX_MIB_SIZE << bsize[0], tmp_dst, |
| 142 | dlist, dering_count, bsize[0]); |
Yaowu Xu | 9c323bc | 2016-09-01 11:35:16 -0700 | [diff] [blame] | 143 | cur_mse = (int)compute_dist( |
Jean-Marc Valin | 8e94178 | 2016-10-13 14:44:22 -0400 | [diff] [blame] | 144 | dst, MAX_MIB_SIZE << bsize[0], |
| 145 | &ref_coeff[(sbr * stride * MAX_MIB_SIZE << bsize[0]) + |
| 146 | (sbc * MAX_MIB_SIZE << bsize[0])], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 147 | stride, nhb, nvb, coeff_shift); |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 148 | if (cur_mse < best_mse) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 149 | best_gi = gi; |
Jean-Marc Valin | 6d5a7a9 | 2016-08-22 15:47:09 -0400 | [diff] [blame] | 150 | best_mse = cur_mse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | cm->mi_grid_visible[MAX_MIB_SIZE * sbr * cm->mi_stride + |
| 154 | MAX_MIB_SIZE * sbc] |
| 155 | ->mbmi.dering_gain = best_gi; |
| 156 | } |
| 157 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 158 | aom_free(src); |
| 159 | aom_free(ref_coeff); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 160 | return best_level; |
| 161 | } |