Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [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 | bde4ac8 | 2016-11-28 15:26:06 -0800 | [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 <assert.h> |
| 13 | #include <float.h> |
| 14 | #include <limits.h> |
| 15 | #include <math.h> |
| 16 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 17 | #include "./aom_scale_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 18 | |
| 19 | #include "aom_dsp/psnr.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 20 | #include "aom_dsp/aom_dsp_common.h" |
| 21 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | #include "aom_ports/mem.h" |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 23 | #include "aom_ports/system_state.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | |
| 25 | #include "av1/common/onyxc_int.h" |
| 26 | #include "av1/common/quant_common.h" |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 27 | #include "av1/common/restoration.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | |
Tom Finegan | 17ce8b1 | 2017-02-08 12:46:31 -0800 | [diff] [blame] | 29 | #include "av1/encoder/av1_quantize.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 30 | #include "av1/encoder/encoder.h" |
| 31 | #include "av1/encoder/picklpf.h" |
| 32 | #include "av1/encoder/pickrst.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 33 | |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 34 | // When set to RESTORE_WIENER or RESTORE_SGRPROJ only those are allowed. |
| 35 | // When set to RESTORE_NONE (0) we allow switchable. |
| 36 | const RestorationType force_restore_type = RESTORE_NONE; |
Debargha Mukherjee | 1b3dbf0 | 2017-03-13 14:47:21 -0700 | [diff] [blame] | 37 | |
| 38 | // Number of Wiener iterations |
| 39 | #define NUM_WIENER_ITERS 10 |
| 40 | |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 41 | typedef double (*search_restore_type)(const YV12_BUFFER_CONFIG *src, |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 42 | AV1_COMP *cpi, int partial_frame, |
| 43 | RestorationInfo *info, |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 44 | RestorationType *rest_level, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 45 | double *best_tile_cost, |
| 46 | YV12_BUFFER_CONFIG *dst_frame); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 47 | |
Debargha Mukherjee | b3c43bc | 2017-02-01 13:09:03 -0800 | [diff] [blame] | 48 | const int frame_level_restore_bits[RESTORE_TYPES] = { 2, 2, 2, 2 }; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 49 | |
| 50 | static int64_t sse_restoration_tile(const YV12_BUFFER_CONFIG *src, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 51 | const YV12_BUFFER_CONFIG *dst, |
| 52 | const AV1_COMMON *cm, int h_start, |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 53 | int width, int v_start, int height, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 54 | int components_pattern) { |
| 55 | int64_t filt_err = 0; |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 56 | (void)cm; |
| 57 | // Y and UV components cannot be mixed |
| 58 | assert(components_pattern == 1 || components_pattern == 2 || |
| 59 | components_pattern == 4 || components_pattern == 6); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 60 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 61 | if (cm->use_highbitdepth) { |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 62 | if ((components_pattern >> AOM_PLANE_Y) & 1) { |
| 63 | filt_err += |
| 64 | aom_highbd_get_y_sse_part(src, dst, h_start, width, v_start, height); |
| 65 | } |
| 66 | if ((components_pattern >> AOM_PLANE_U) & 1) { |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 67 | filt_err += |
| 68 | aom_highbd_get_u_sse_part(src, dst, h_start, width, v_start, height); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 69 | } |
| 70 | if ((components_pattern >> AOM_PLANE_V) & 1) { |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 71 | filt_err += |
| 72 | aom_highbd_get_v_sse_part(src, dst, h_start, width, v_start, height); |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 73 | } |
| 74 | return filt_err; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 75 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 76 | #endif // CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 77 | if ((components_pattern >> AOM_PLANE_Y) & 1) { |
| 78 | filt_err += aom_get_y_sse_part(src, dst, h_start, width, v_start, height); |
| 79 | } |
| 80 | if ((components_pattern >> AOM_PLANE_U) & 1) { |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 81 | filt_err += aom_get_u_sse_part(src, dst, h_start, width, v_start, height); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 82 | } |
| 83 | if ((components_pattern >> AOM_PLANE_V) & 1) { |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 84 | filt_err += aom_get_v_sse_part(src, dst, h_start, width, v_start, height); |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 85 | } |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 86 | return filt_err; |
| 87 | } |
| 88 | |
David Barker | 60a055b | 2017-01-18 15:10:43 +0000 | [diff] [blame] | 89 | static int64_t sse_restoration_frame(AV1_COMMON *const cm, |
| 90 | const YV12_BUFFER_CONFIG *src, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 91 | const YV12_BUFFER_CONFIG *dst, |
| 92 | int components_pattern) { |
| 93 | int64_t filt_err = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 94 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 95 | if (cm->use_highbitdepth) { |
| 96 | if ((components_pattern >> AOM_PLANE_Y) & 1) { |
| 97 | filt_err += aom_highbd_get_y_sse(src, dst); |
| 98 | } |
| 99 | if ((components_pattern >> AOM_PLANE_U) & 1) { |
| 100 | filt_err += aom_highbd_get_u_sse(src, dst); |
| 101 | } |
| 102 | if ((components_pattern >> AOM_PLANE_V) & 1) { |
| 103 | filt_err += aom_highbd_get_v_sse(src, dst); |
| 104 | } |
| 105 | return filt_err; |
| 106 | } |
David Barker | 60a055b | 2017-01-18 15:10:43 +0000 | [diff] [blame] | 107 | #else |
| 108 | (void)cm; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 109 | #endif // CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 110 | if ((components_pattern >> AOM_PLANE_Y) & 1) { |
| 111 | filt_err = aom_get_y_sse(src, dst); |
| 112 | } |
| 113 | if ((components_pattern >> AOM_PLANE_U) & 1) { |
| 114 | filt_err += aom_get_u_sse(src, dst); |
| 115 | } |
| 116 | if ((components_pattern >> AOM_PLANE_V) & 1) { |
| 117 | filt_err += aom_get_v_sse(src, dst); |
| 118 | } |
| 119 | return filt_err; |
| 120 | } |
| 121 | |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 122 | static int64_t try_restoration_tile(const YV12_BUFFER_CONFIG *src, |
| 123 | AV1_COMP *const cpi, RestorationInfo *rsi, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 124 | int components_pattern, int partial_frame, |
| 125 | int tile_idx, int subtile_idx, |
| 126 | int subtile_bits, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 127 | YV12_BUFFER_CONFIG *dst_frame) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 128 | AV1_COMMON *const cm = &cpi->common; |
| 129 | int64_t filt_err; |
| 130 | int tile_width, tile_height, nhtiles, nvtiles; |
| 131 | int h_start, h_end, v_start, v_end; |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 132 | int ntiles, width, height; |
| 133 | |
| 134 | // Y and UV components cannot be mixed |
| 135 | assert(components_pattern == 1 || components_pattern == 2 || |
| 136 | components_pattern == 4 || components_pattern == 6); |
| 137 | |
| 138 | if (components_pattern == 1) { // Y only |
| 139 | width = src->y_crop_width; |
| 140 | height = src->y_crop_height; |
| 141 | } else { // Color |
| 142 | width = src->uv_crop_width; |
| 143 | height = src->uv_crop_height; |
| 144 | } |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 145 | ntiles = av1_get_rest_ntiles( |
| 146 | width, height, cm->rst_info[components_pattern > 1].restoration_tilesize, |
| 147 | &tile_width, &tile_height, &nhtiles, &nvtiles); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 148 | (void)ntiles; |
| 149 | |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 150 | av1_loop_restoration_frame(cm->frame_to_show, cm, rsi, components_pattern, |
| 151 | partial_frame, dst_frame); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 152 | av1_get_rest_tile_limits(tile_idx, subtile_idx, subtile_bits, nhtiles, |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 153 | nvtiles, tile_width, tile_height, width, height, 0, |
| 154 | 0, &h_start, &h_end, &v_start, &v_end); |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 155 | filt_err = sse_restoration_tile(src, dst_frame, cm, h_start, h_end - h_start, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 156 | v_start, v_end - v_start, components_pattern); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 157 | |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 158 | return filt_err; |
| 159 | } |
| 160 | |
| 161 | static int64_t try_restoration_frame(const YV12_BUFFER_CONFIG *src, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 162 | AV1_COMP *const cpi, RestorationInfo *rsi, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 163 | int components_pattern, int partial_frame, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 164 | YV12_BUFFER_CONFIG *dst_frame) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 165 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 166 | int64_t filt_err; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 167 | av1_loop_restoration_frame(cm->frame_to_show, cm, rsi, components_pattern, |
| 168 | partial_frame, dst_frame); |
David Barker | 60a055b | 2017-01-18 15:10:43 +0000 | [diff] [blame] | 169 | filt_err = sse_restoration_frame(cm, src, dst_frame, components_pattern); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 170 | return filt_err; |
| 171 | } |
| 172 | |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 173 | static int64_t get_pixel_proj_error(uint8_t *src8, int width, int height, |
| 174 | int src_stride, uint8_t *dat8, |
| 175 | int dat_stride, int bit_depth, |
| 176 | int32_t *flt1, int flt1_stride, |
| 177 | int32_t *flt2, int flt2_stride, int *xqd) { |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 178 | int i, j; |
| 179 | int64_t err = 0; |
| 180 | int xq[2]; |
| 181 | decode_xq(xqd, xq); |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 182 | if (bit_depth == 8) { |
| 183 | const uint8_t *src = src8; |
| 184 | const uint8_t *dat = dat8; |
| 185 | for (i = 0; i < height; ++i) { |
| 186 | for (j = 0; j < width; ++j) { |
| 187 | const int32_t u = |
| 188 | (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS); |
| 189 | const int32_t f1 = (int32_t)flt1[i * flt1_stride + j] - u; |
| 190 | const int32_t f2 = (int32_t)flt2[i * flt2_stride + j] - u; |
David Barker | ce110cc | 2017-02-22 10:38:59 +0000 | [diff] [blame] | 191 | const int32_t v = xq[0] * f1 + xq[1] * f2 + (u << SGRPROJ_PRJ_BITS); |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 192 | const int32_t e = |
| 193 | ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - |
| 194 | src[i * src_stride + j]; |
| 195 | err += e * e; |
| 196 | } |
| 197 | } |
| 198 | } else { |
| 199 | const uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
| 200 | const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8); |
| 201 | for (i = 0; i < height; ++i) { |
| 202 | for (j = 0; j < width; ++j) { |
| 203 | const int32_t u = |
| 204 | (int32_t)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS); |
| 205 | const int32_t f1 = (int32_t)flt1[i * flt1_stride + j] - u; |
| 206 | const int32_t f2 = (int32_t)flt2[i * flt2_stride + j] - u; |
David Barker | ce110cc | 2017-02-22 10:38:59 +0000 | [diff] [blame] | 207 | const int32_t v = xq[0] * f1 + xq[1] * f2 + (u << SGRPROJ_PRJ_BITS); |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 208 | const int32_t e = |
| 209 | ROUND_POWER_OF_TWO(v, SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) - |
| 210 | src[i * src_stride + j]; |
| 211 | err += e * e; |
| 212 | } |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | return err; |
| 216 | } |
| 217 | |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 218 | static void get_proj_subspace(uint8_t *src8, int width, int height, |
| 219 | int src_stride, uint8_t *dat8, int dat_stride, |
| 220 | int bit_depth, int32_t *flt1, int flt1_stride, |
| 221 | int32_t *flt2, int flt2_stride, int *xq) { |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 222 | int i, j; |
| 223 | double H[2][2] = { { 0, 0 }, { 0, 0 } }; |
| 224 | double C[2] = { 0, 0 }; |
| 225 | double Det; |
| 226 | double x[2]; |
| 227 | const int size = width * height; |
| 228 | |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 229 | aom_clear_system_state(); |
| 230 | |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 231 | // Default |
| 232 | xq[0] = 0; |
| 233 | xq[1] = 0; |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 234 | if (bit_depth == 8) { |
| 235 | const uint8_t *src = src8; |
| 236 | const uint8_t *dat = dat8; |
| 237 | for (i = 0; i < height; ++i) { |
| 238 | for (j = 0; j < width; ++j) { |
| 239 | const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS); |
| 240 | const double s = |
| 241 | (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u; |
| 242 | const double f1 = (double)flt1[i * flt1_stride + j] - u; |
| 243 | const double f2 = (double)flt2[i * flt2_stride + j] - u; |
| 244 | H[0][0] += f1 * f1; |
| 245 | H[1][1] += f2 * f2; |
| 246 | H[0][1] += f1 * f2; |
| 247 | C[0] += f1 * s; |
| 248 | C[1] += f2 * s; |
| 249 | } |
| 250 | } |
| 251 | } else { |
| 252 | const uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
| 253 | const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8); |
| 254 | for (i = 0; i < height; ++i) { |
| 255 | for (j = 0; j < width; ++j) { |
| 256 | const double u = (double)(dat[i * dat_stride + j] << SGRPROJ_RST_BITS); |
| 257 | const double s = |
| 258 | (double)(src[i * src_stride + j] << SGRPROJ_RST_BITS) - u; |
| 259 | const double f1 = (double)flt1[i * flt1_stride + j] - u; |
| 260 | const double f2 = (double)flt2[i * flt2_stride + j] - u; |
| 261 | H[0][0] += f1 * f1; |
| 262 | H[1][1] += f2 * f2; |
| 263 | H[0][1] += f1 * f2; |
| 264 | C[0] += f1 * s; |
| 265 | C[1] += f2 * s; |
| 266 | } |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | H[0][0] /= size; |
| 270 | H[0][1] /= size; |
| 271 | H[1][1] /= size; |
| 272 | H[1][0] = H[0][1]; |
| 273 | C[0] /= size; |
| 274 | C[1] /= size; |
| 275 | Det = (H[0][0] * H[1][1] - H[0][1] * H[1][0]); |
| 276 | if (Det < 1e-8) return; // ill-posed, return default values |
| 277 | x[0] = (H[1][1] * C[0] - H[0][1] * C[1]) / Det; |
| 278 | x[1] = (H[0][0] * C[1] - H[1][0] * C[0]) / Det; |
| 279 | xq[0] = (int)rint(x[0] * (1 << SGRPROJ_PRJ_BITS)); |
| 280 | xq[1] = (int)rint(x[1] * (1 << SGRPROJ_PRJ_BITS)); |
| 281 | } |
| 282 | |
| 283 | void encode_xq(int *xq, int *xqd) { |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 284 | xqd[0] = xq[0]; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 285 | xqd[0] = clamp(xqd[0], SGRPROJ_PRJ_MIN0, SGRPROJ_PRJ_MAX0); |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 286 | xqd[1] = (1 << SGRPROJ_PRJ_BITS) - xqd[0] - xq[1]; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 287 | xqd[1] = clamp(xqd[1], SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1); |
| 288 | } |
| 289 | |
| 290 | static void search_selfguided_restoration(uint8_t *dat8, int width, int height, |
| 291 | int dat_stride, uint8_t *src8, |
| 292 | int src_stride, int bit_depth, |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 293 | int *eps, int *xqd, int32_t *rstbuf) { |
| 294 | int32_t *flt1 = rstbuf; |
Debargha Mukherjee | 519dbcf | 2016-12-16 03:13:02 -0800 | [diff] [blame] | 295 | int32_t *flt2 = flt1 + RESTORATION_TILEPELS_MAX; |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 296 | int32_t *tmpbuf2 = flt2 + RESTORATION_TILEPELS_MAX; |
David Barker | 506eb72 | 2017-03-08 13:35:49 +0000 | [diff] [blame] | 297 | int ep, bestep = 0; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 298 | int64_t err, besterr = -1; |
| 299 | int exqd[2], bestxqd[2] = { 0, 0 }; |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 300 | |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 301 | for (ep = 0; ep < SGRPROJ_PARAMS; ep++) { |
| 302 | int exq[2]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 303 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 304 | if (bit_depth > 8) { |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 305 | uint16_t *dat = CONVERT_TO_SHORTPTR(dat8); |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 306 | #if USE_HIGHPASS_IN_SGRPROJ |
| 307 | av1_highpass_filter_highbd(dat, width, height, dat_stride, flt1, width, |
David Barker | eed824e | 2017-03-10 11:35:22 +0000 | [diff] [blame] | 308 | sgr_params[ep].corner, sgr_params[ep].edge); |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 309 | #else |
David Barker | 506eb72 | 2017-03-08 13:35:49 +0000 | [diff] [blame] | 310 | av1_selfguided_restoration_highbd(dat, width, height, dat_stride, flt1, |
| 311 | width, bit_depth, sgr_params[ep].r1, |
| 312 | sgr_params[ep].e1, tmpbuf2); |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 313 | #endif // USE_HIGHPASS_IN_SGRPROJ |
David Barker | 506eb72 | 2017-03-08 13:35:49 +0000 | [diff] [blame] | 314 | av1_selfguided_restoration_highbd(dat, width, height, dat_stride, flt2, |
| 315 | width, bit_depth, sgr_params[ep].r2, |
| 316 | sgr_params[ep].e2, tmpbuf2); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 317 | } else { |
David Barker | 506eb72 | 2017-03-08 13:35:49 +0000 | [diff] [blame] | 318 | #endif |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 319 | #if USE_HIGHPASS_IN_SGRPROJ |
| 320 | av1_highpass_filter(dat8, width, height, dat_stride, flt1, width, |
David Barker | eed824e | 2017-03-10 11:35:22 +0000 | [diff] [blame] | 321 | sgr_params[ep].corner, sgr_params[ep].edge); |
Debargha Mukherjee | b7bb097 | 2017-03-09 06:47:43 -0800 | [diff] [blame] | 322 | #else |
| 323 | av1_selfguided_restoration(dat8, width, height, dat_stride, flt1, width, |
| 324 | sgr_params[ep].r1, sgr_params[ep].e1, tmpbuf2); |
| 325 | #endif // USE_HIGHPASS_IN_SGRPROJ |
David Barker | 506eb72 | 2017-03-08 13:35:49 +0000 | [diff] [blame] | 326 | av1_selfguided_restoration(dat8, width, height, dat_stride, flt2, width, |
David Barker | 4d2af5d | 2017-03-09 11:46:50 +0000 | [diff] [blame] | 327 | sgr_params[ep].r2, sgr_params[ep].e2, tmpbuf2); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 328 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 329 | } |
David Barker | 506eb72 | 2017-03-08 13:35:49 +0000 | [diff] [blame] | 330 | #endif |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 331 | get_proj_subspace(src8, width, height, src_stride, dat8, dat_stride, |
| 332 | bit_depth, flt1, width, flt2, width, exq); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 333 | encode_xq(exq, exqd); |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 334 | err = |
| 335 | get_pixel_proj_error(src8, width, height, src_stride, dat8, dat_stride, |
| 336 | bit_depth, flt1, width, flt2, width, exqd); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 337 | if (besterr == -1 || err < besterr) { |
| 338 | bestep = ep; |
| 339 | besterr = err; |
| 340 | bestxqd[0] = exqd[0]; |
| 341 | bestxqd[1] = exqd[1]; |
| 342 | } |
| 343 | } |
| 344 | *eps = bestep; |
| 345 | xqd[0] = bestxqd[0]; |
| 346 | xqd[1] = bestxqd[1]; |
| 347 | } |
| 348 | |
| 349 | static double search_sgrproj(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi, |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 350 | int partial_frame, RestorationInfo *info, |
| 351 | RestorationType *type, double *best_tile_cost, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 352 | YV12_BUFFER_CONFIG *dst_frame) { |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 353 | SgrprojInfo *sgrproj_info = info->sgrproj_info; |
| 354 | double err, cost_norestore, cost_sgrproj; |
| 355 | int bits; |
| 356 | MACROBLOCK *x = &cpi->td.mb; |
| 357 | AV1_COMMON *const cm = &cpi->common; |
| 358 | const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 359 | RestorationInfo *rsi = &cpi->rst_search[0]; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 360 | int tile_idx, tile_width, tile_height, nhtiles, nvtiles; |
| 361 | int h_start, h_end, v_start, v_end; |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 362 | // Allocate for the src buffer at high precision |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 363 | const int ntiles = av1_get_rest_ntiles( |
| 364 | cm->width, cm->height, cm->rst_info[0].restoration_tilesize, &tile_width, |
| 365 | &tile_height, &nhtiles, &nvtiles); |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 366 | rsi->frame_restoration_type = RESTORE_SGRPROJ; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 367 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 368 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 369 | rsi->restoration_type[tile_idx] = RESTORE_NONE; |
| 370 | } |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 371 | // Compute best Sgrproj filters for each tile |
| 372 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 373 | av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width, |
| 374 | tile_height, cm->width, cm->height, 0, 0, &h_start, |
| 375 | &h_end, &v_start, &v_end); |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 376 | err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start, |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 377 | h_end - h_start, v_start, v_end - v_start, 1); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 378 | // #bits when a tile is not restored |
| 379 | bits = av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 0); |
| 380 | cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
| 381 | best_tile_cost[tile_idx] = DBL_MAX; |
| 382 | search_selfguided_restoration( |
| 383 | dgd->y_buffer + v_start * dgd->y_stride + h_start, h_end - h_start, |
| 384 | v_end - v_start, dgd->y_stride, |
| 385 | src->y_buffer + v_start * src->y_stride + h_start, src->y_stride, |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 386 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 387 | cm->bit_depth, |
| 388 | #else |
| 389 | 8, |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 390 | #endif // CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 391 | &rsi->sgrproj_info[tile_idx].ep, rsi->sgrproj_info[tile_idx].xqd, |
David Barker | 3a0df18 | 2016-12-21 10:44:52 +0000 | [diff] [blame] | 392 | cm->rst_internal.tmpbuf); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 393 | rsi->restoration_type[tile_idx] = RESTORE_SGRPROJ; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 394 | err = try_restoration_tile(src, cpi, rsi, 1, partial_frame, tile_idx, 0, 0, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 395 | dst_frame); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 396 | bits = SGRPROJ_BITS << AV1_PROB_COST_SHIFT; |
| 397 | bits += av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 1); |
| 398 | cost_sgrproj = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
| 399 | if (cost_sgrproj >= cost_norestore) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 400 | type[tile_idx] = RESTORE_NONE; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 401 | } else { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 402 | type[tile_idx] = RESTORE_SGRPROJ; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 403 | memcpy(&sgrproj_info[tile_idx], &rsi->sgrproj_info[tile_idx], |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 404 | sizeof(sgrproj_info[tile_idx])); |
| 405 | bits = SGRPROJ_BITS << AV1_PROB_COST_SHIFT; |
| 406 | best_tile_cost[tile_idx] = RDCOST_DBL( |
| 407 | x->rdmult, x->rddiv, |
| 408 | (bits + cpi->switchable_restore_cost[RESTORE_SGRPROJ]) >> 4, err); |
| 409 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 410 | rsi->restoration_type[tile_idx] = RESTORE_NONE; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 411 | } |
| 412 | // Cost for Sgrproj filtering |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 413 | bits = frame_level_restore_bits[rsi->frame_restoration_type] |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 414 | << AV1_PROB_COST_SHIFT; |
| 415 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 416 | bits += |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 417 | av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, type[tile_idx] != RESTORE_NONE); |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 418 | memcpy(&rsi->sgrproj_info[tile_idx], &sgrproj_info[tile_idx], |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 419 | sizeof(sgrproj_info[tile_idx])); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 420 | if (type[tile_idx] == RESTORE_SGRPROJ) { |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 421 | bits += (SGRPROJ_BITS << AV1_PROB_COST_SHIFT); |
| 422 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 423 | rsi->restoration_type[tile_idx] = type[tile_idx]; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 424 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 425 | err = try_restoration_frame(src, cpi, rsi, 1, partial_frame, dst_frame); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 426 | cost_sgrproj = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
| 427 | |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 428 | return cost_sgrproj; |
| 429 | } |
| 430 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 431 | static double find_average(uint8_t *src, int h_start, int h_end, int v_start, |
| 432 | int v_end, int stride) { |
| 433 | uint64_t sum = 0; |
| 434 | double avg = 0; |
| 435 | int i, j; |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 436 | aom_clear_system_state(); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 437 | for (i = v_start; i < v_end; i++) |
| 438 | for (j = h_start; j < h_end; j++) sum += src[i * stride + j]; |
| 439 | avg = (double)sum / ((v_end - v_start) * (h_end - h_start)); |
| 440 | return avg; |
| 441 | } |
| 442 | |
| 443 | static void compute_stats(uint8_t *dgd, uint8_t *src, int h_start, int h_end, |
| 444 | int v_start, int v_end, int dgd_stride, |
| 445 | int src_stride, double *M, double *H) { |
| 446 | int i, j, k, l; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 447 | double Y[WIENER_WIN2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 448 | const double avg = |
| 449 | find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride); |
| 450 | |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 451 | memset(M, 0, sizeof(*M) * WIENER_WIN2); |
| 452 | memset(H, 0, sizeof(*H) * WIENER_WIN2 * WIENER_WIN2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 453 | for (i = v_start; i < v_end; i++) { |
| 454 | for (j = h_start; j < h_end; j++) { |
| 455 | const double X = (double)src[i * src_stride + j] - avg; |
| 456 | int idx = 0; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 457 | for (k = -WIENER_HALFWIN; k <= WIENER_HALFWIN; k++) { |
| 458 | for (l = -WIENER_HALFWIN; l <= WIENER_HALFWIN; l++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 459 | Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg; |
| 460 | idx++; |
| 461 | } |
| 462 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 463 | for (k = 0; k < WIENER_WIN2; ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 464 | M[k] += Y[k] * X; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 465 | H[k * WIENER_WIN2 + k] += Y[k] * Y[k]; |
| 466 | for (l = k + 1; l < WIENER_WIN2; ++l) { |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 467 | // H is a symmetric matrix, so we only need to fill out the upper |
| 468 | // triangle here. We can copy it down to the lower triangle outside |
| 469 | // the (i, j) loops. |
| 470 | H[k * WIENER_WIN2 + l] += Y[k] * Y[l]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 475 | for (k = 0; k < WIENER_WIN2; ++k) { |
| 476 | for (l = k + 1; l < WIENER_WIN2; ++l) { |
| 477 | H[l * WIENER_WIN2 + k] = H[k * WIENER_WIN2 + l]; |
| 478 | } |
| 479 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 482 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 483 | static double find_average_highbd(uint16_t *src, int h_start, int h_end, |
| 484 | int v_start, int v_end, int stride) { |
| 485 | uint64_t sum = 0; |
| 486 | double avg = 0; |
| 487 | int i, j; |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 488 | aom_clear_system_state(); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 489 | for (i = v_start; i < v_end; i++) |
| 490 | for (j = h_start; j < h_end; j++) sum += src[i * stride + j]; |
| 491 | avg = (double)sum / ((v_end - v_start) * (h_end - h_start)); |
| 492 | return avg; |
| 493 | } |
| 494 | |
| 495 | static void compute_stats_highbd(uint8_t *dgd8, uint8_t *src8, int h_start, |
| 496 | int h_end, int v_start, int v_end, |
| 497 | int dgd_stride, int src_stride, double *M, |
| 498 | double *H) { |
| 499 | int i, j, k, l; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 500 | double Y[WIENER_WIN2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 501 | uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
| 502 | uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8); |
| 503 | const double avg = |
| 504 | find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride); |
| 505 | |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 506 | memset(M, 0, sizeof(*M) * WIENER_WIN2); |
| 507 | memset(H, 0, sizeof(*H) * WIENER_WIN2 * WIENER_WIN2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 508 | for (i = v_start; i < v_end; i++) { |
| 509 | for (j = h_start; j < h_end; j++) { |
| 510 | const double X = (double)src[i * src_stride + j] - avg; |
| 511 | int idx = 0; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 512 | for (k = -WIENER_HALFWIN; k <= WIENER_HALFWIN; k++) { |
| 513 | for (l = -WIENER_HALFWIN; l <= WIENER_HALFWIN; l++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 514 | Y[idx] = (double)dgd[(i + l) * dgd_stride + (j + k)] - avg; |
| 515 | idx++; |
| 516 | } |
| 517 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 518 | for (k = 0; k < WIENER_WIN2; ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 519 | M[k] += Y[k] * X; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 520 | H[k * WIENER_WIN2 + k] += Y[k] * Y[k]; |
| 521 | for (l = k + 1; l < WIENER_WIN2; ++l) { |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 522 | // H is a symmetric matrix, so we only need to fill out the upper |
| 523 | // triangle here. We can copy it down to the lower triangle outside |
| 524 | // the (i, j) loops. |
| 525 | H[k * WIENER_WIN2 + l] += Y[k] * Y[l]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 530 | for (k = 0; k < WIENER_WIN2; ++k) { |
| 531 | for (l = k + 1; l < WIENER_WIN2; ++l) { |
| 532 | H[l * WIENER_WIN2 + k] = H[k * WIENER_WIN2 + l]; |
| 533 | } |
| 534 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 535 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 536 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 537 | |
| 538 | // Solves Ax = b, where x and b are column vectors |
| 539 | static int linsolve(int n, double *A, int stride, double *b, double *x) { |
| 540 | int i, j, k; |
| 541 | double c; |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 542 | |
| 543 | aom_clear_system_state(); |
| 544 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 545 | // Forward elimination |
| 546 | for (k = 0; k < n - 1; k++) { |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 547 | // Bring the largest magitude to the diagonal position |
| 548 | for (i = n - 1; i > k; i--) { |
| 549 | if (fabs(A[(i - 1) * stride + k]) < fabs(A[i * stride + k])) { |
| 550 | for (j = 0; j < n; j++) { |
| 551 | c = A[i * stride + j]; |
| 552 | A[i * stride + j] = A[(i - 1) * stride + j]; |
| 553 | A[(i - 1) * stride + j] = c; |
| 554 | } |
| 555 | c = b[i]; |
| 556 | b[i] = b[i - 1]; |
| 557 | b[i - 1] = c; |
| 558 | } |
| 559 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 560 | for (i = k; i < n - 1; i++) { |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 561 | if (fabs(A[k * stride + k]) < 1e-10) return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 562 | c = A[(i + 1) * stride + k] / A[k * stride + k]; |
| 563 | for (j = 0; j < n; j++) A[(i + 1) * stride + j] -= c * A[k * stride + j]; |
| 564 | b[i + 1] -= c * b[k]; |
| 565 | } |
| 566 | } |
| 567 | // Backward substitution |
| 568 | for (i = n - 1; i >= 0; i--) { |
| 569 | if (fabs(A[i * stride + i]) < 1e-10) return 0; |
| 570 | c = 0; |
| 571 | for (j = i + 1; j <= n - 1; j++) c += A[i * stride + j] * x[j]; |
| 572 | x[i] = (b[i] - c) / A[i * stride + i]; |
| 573 | } |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 574 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | static INLINE int wrap_index(int i) { |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 579 | return (i >= WIENER_HALFWIN1 ? WIENER_WIN - 1 - i : i); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // Fix vector b, update vector a |
| 583 | static void update_a_sep_sym(double **Mc, double **Hc, double *a, double *b) { |
| 584 | int i, j; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 585 | double S[WIENER_WIN]; |
Debargha Mukherjee | 6ae588f | 2017-04-14 00:40:02 -0700 | [diff] [blame] | 586 | double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 587 | int w, w2; |
| 588 | memset(A, 0, sizeof(A)); |
| 589 | memset(B, 0, sizeof(B)); |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 590 | for (i = 0; i < WIENER_WIN; i++) { |
| 591 | for (j = 0; j < WIENER_WIN; ++j) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 592 | const int jj = wrap_index(j); |
| 593 | A[jj] += Mc[i][j] * b[i]; |
| 594 | } |
| 595 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 596 | for (i = 0; i < WIENER_WIN; i++) { |
| 597 | for (j = 0; j < WIENER_WIN; j++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 598 | int k, l; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 599 | for (k = 0; k < WIENER_WIN; ++k) |
| 600 | for (l = 0; l < WIENER_WIN; ++l) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 601 | const int kk = wrap_index(k); |
| 602 | const int ll = wrap_index(l); |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 603 | B[ll * WIENER_HALFWIN1 + kk] += |
| 604 | Hc[j * WIENER_WIN + i][k * WIENER_WIN2 + l] * b[i] * b[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | // Normalization enforcement in the system of equations itself |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 609 | w = WIENER_WIN; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 610 | w2 = (w >> 1) + 1; |
| 611 | for (i = 0; i < w2 - 1; ++i) |
| 612 | A[i] -= |
| 613 | A[w2 - 1] * 2 + B[i * w2 + w2 - 1] - 2 * B[(w2 - 1) * w2 + (w2 - 1)]; |
| 614 | for (i = 0; i < w2 - 1; ++i) |
| 615 | for (j = 0; j < w2 - 1; ++j) |
| 616 | B[i * w2 + j] -= 2 * (B[i * w2 + (w2 - 1)] + B[(w2 - 1) * w2 + j] - |
| 617 | 2 * B[(w2 - 1) * w2 + (w2 - 1)]); |
| 618 | if (linsolve(w2 - 1, B, w2, A, S)) { |
| 619 | S[w2 - 1] = 1.0; |
| 620 | for (i = w2; i < w; ++i) { |
| 621 | S[i] = S[w - 1 - i]; |
| 622 | S[w2 - 1] -= 2 * S[i]; |
| 623 | } |
| 624 | memcpy(a, S, w * sizeof(*a)); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | // Fix vector a, update vector b |
| 629 | static void update_b_sep_sym(double **Mc, double **Hc, double *a, double *b) { |
| 630 | int i, j; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 631 | double S[WIENER_WIN]; |
Debargha Mukherjee | 6ae588f | 2017-04-14 00:40:02 -0700 | [diff] [blame] | 632 | double A[WIENER_HALFWIN1], B[WIENER_HALFWIN1 * WIENER_HALFWIN1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 633 | int w, w2; |
| 634 | memset(A, 0, sizeof(A)); |
| 635 | memset(B, 0, sizeof(B)); |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 636 | for (i = 0; i < WIENER_WIN; i++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 637 | const int ii = wrap_index(i); |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 638 | for (j = 0; j < WIENER_WIN; j++) A[ii] += Mc[i][j] * a[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 641 | for (i = 0; i < WIENER_WIN; i++) { |
| 642 | for (j = 0; j < WIENER_WIN; j++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 643 | const int ii = wrap_index(i); |
| 644 | const int jj = wrap_index(j); |
| 645 | int k, l; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 646 | for (k = 0; k < WIENER_WIN; ++k) |
| 647 | for (l = 0; l < WIENER_WIN; ++l) |
| 648 | B[jj * WIENER_HALFWIN1 + ii] += |
| 649 | Hc[i * WIENER_WIN + j][k * WIENER_WIN2 + l] * a[k] * a[l]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | // Normalization enforcement in the system of equations itself |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 653 | w = WIENER_WIN; |
| 654 | w2 = WIENER_HALFWIN1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 655 | for (i = 0; i < w2 - 1; ++i) |
| 656 | A[i] -= |
| 657 | A[w2 - 1] * 2 + B[i * w2 + w2 - 1] - 2 * B[(w2 - 1) * w2 + (w2 - 1)]; |
| 658 | for (i = 0; i < w2 - 1; ++i) |
| 659 | for (j = 0; j < w2 - 1; ++j) |
| 660 | B[i * w2 + j] -= 2 * (B[i * w2 + (w2 - 1)] + B[(w2 - 1) * w2 + j] - |
| 661 | 2 * B[(w2 - 1) * w2 + (w2 - 1)]); |
| 662 | if (linsolve(w2 - 1, B, w2, A, S)) { |
| 663 | S[w2 - 1] = 1.0; |
| 664 | for (i = w2; i < w; ++i) { |
| 665 | S[i] = S[w - 1 - i]; |
| 666 | S[w2 - 1] -= 2 * S[i]; |
| 667 | } |
| 668 | memcpy(b, S, w * sizeof(*b)); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | static int wiener_decompose_sep_sym(double *M, double *H, double *a, |
| 673 | double *b) { |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 674 | static const double init_filt[WIENER_WIN] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 675 | 0.035623, -0.127154, 0.211436, 0.760190, 0.211436, -0.127154, 0.035623, |
| 676 | }; |
| 677 | int i, j, iter; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 678 | double *Hc[WIENER_WIN2]; |
| 679 | double *Mc[WIENER_WIN]; |
| 680 | for (i = 0; i < WIENER_WIN; i++) { |
| 681 | Mc[i] = M + i * WIENER_WIN; |
| 682 | for (j = 0; j < WIENER_WIN; j++) { |
| 683 | Hc[i * WIENER_WIN + j] = |
| 684 | H + i * WIENER_WIN * WIENER_WIN2 + j * WIENER_WIN; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 687 | memcpy(a, init_filt, sizeof(*a) * WIENER_WIN); |
| 688 | memcpy(b, init_filt, sizeof(*b) * WIENER_WIN); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 689 | |
| 690 | iter = 1; |
Debargha Mukherjee | 1b3dbf0 | 2017-03-13 14:47:21 -0700 | [diff] [blame] | 691 | while (iter < NUM_WIENER_ITERS) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 692 | update_a_sep_sym(Mc, Hc, a, b); |
| 693 | update_b_sep_sym(Mc, Hc, a, b); |
| 694 | iter++; |
| 695 | } |
| 696 | return 1; |
| 697 | } |
| 698 | |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 699 | // Computes the function x'*H*x - x'*M for the learned 2D filter x, and compares |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 700 | // against identity filters; Final score is defined as the difference between |
| 701 | // the function values |
David Barker | 1e8e6b9 | 2017-01-13 13:45:51 +0000 | [diff] [blame] | 702 | static double compute_score(double *M, double *H, InterpKernel vfilt, |
| 703 | InterpKernel hfilt) { |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 704 | double ab[WIENER_WIN * WIENER_WIN]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 705 | int i, k, l; |
| 706 | double P = 0, Q = 0; |
| 707 | double iP = 0, iQ = 0; |
| 708 | double Score, iScore; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 709 | double a[WIENER_WIN], b[WIENER_WIN]; |
Jingning Han | 041c67b | 2017-04-14 21:39:26 -0700 | [diff] [blame^] | 710 | |
| 711 | aom_clear_system_state(); |
| 712 | |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 713 | a[WIENER_HALFWIN] = b[WIENER_HALFWIN] = 1.0; |
| 714 | for (i = 0; i < WIENER_HALFWIN; ++i) { |
| 715 | a[i] = a[WIENER_WIN - i - 1] = (double)vfilt[i] / WIENER_FILT_STEP; |
| 716 | b[i] = b[WIENER_WIN - i - 1] = (double)hfilt[i] / WIENER_FILT_STEP; |
| 717 | a[WIENER_HALFWIN] -= 2 * a[i]; |
| 718 | b[WIENER_HALFWIN] -= 2 * b[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 719 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 720 | for (k = 0; k < WIENER_WIN; ++k) { |
| 721 | for (l = 0; l < WIENER_WIN; ++l) ab[k * WIENER_WIN + l] = a[l] * b[k]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 722 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 723 | for (k = 0; k < WIENER_WIN2; ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 724 | P += ab[k] * M[k]; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 725 | for (l = 0; l < WIENER_WIN2; ++l) |
| 726 | Q += ab[k] * H[k * WIENER_WIN2 + l] * ab[l]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 727 | } |
| 728 | Score = Q - 2 * P; |
| 729 | |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 730 | iP = M[WIENER_WIN2 >> 1]; |
| 731 | iQ = H[(WIENER_WIN2 >> 1) * WIENER_WIN2 + (WIENER_WIN2 >> 1)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 732 | iScore = iQ - 2 * iP; |
| 733 | |
| 734 | return Score - iScore; |
| 735 | } |
| 736 | |
David Barker | 1e8e6b9 | 2017-01-13 13:45:51 +0000 | [diff] [blame] | 737 | static void quantize_sym_filter(double *f, InterpKernel fi) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 738 | int i; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 739 | for (i = 0; i < WIENER_HALFWIN; ++i) { |
| 740 | fi[i] = RINT(f[i] * WIENER_FILT_STEP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 741 | } |
| 742 | // Specialize for 7-tap filter |
| 743 | fi[0] = CLIP(fi[0], WIENER_FILT_TAP0_MINV, WIENER_FILT_TAP0_MAXV); |
| 744 | fi[1] = CLIP(fi[1], WIENER_FILT_TAP1_MINV, WIENER_FILT_TAP1_MAXV); |
| 745 | fi[2] = CLIP(fi[2], WIENER_FILT_TAP2_MINV, WIENER_FILT_TAP2_MAXV); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 746 | // Satisfy filter constraints |
| 747 | fi[WIENER_WIN - 1] = fi[0]; |
| 748 | fi[WIENER_WIN - 2] = fi[1]; |
| 749 | fi[WIENER_WIN - 3] = fi[2]; |
David Barker | 1e8e6b9 | 2017-01-13 13:45:51 +0000 | [diff] [blame] | 750 | // The central element has an implicit +WIENER_FILT_STEP |
| 751 | fi[3] = -2 * (fi[0] + fi[1] + fi[2]); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static double search_wiener_uv(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi, |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 755 | int partial_frame, int plane, |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 756 | RestorationInfo *info, RestorationType *type, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 757 | YV12_BUFFER_CONFIG *dst_frame) { |
| 758 | WienerInfo *wiener_info = info->wiener_info; |
| 759 | AV1_COMMON *const cm = &cpi->common; |
| 760 | RestorationInfo *rsi = cpi->rst_search; |
| 761 | int64_t err; |
| 762 | int bits; |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 763 | double cost_wiener, cost_norestore, cost_wiener_frame, cost_norestore_frame; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 764 | MACROBLOCK *x = &cpi->td.mb; |
| 765 | double M[WIENER_WIN2]; |
| 766 | double H[WIENER_WIN2 * WIENER_WIN2]; |
| 767 | double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN]; |
| 768 | const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show; |
| 769 | const int width = src->uv_crop_width; |
| 770 | const int height = src->uv_crop_height; |
| 771 | const int src_stride = src->uv_stride; |
| 772 | const int dgd_stride = dgd->uv_stride; |
| 773 | double score; |
| 774 | int tile_idx, tile_width, tile_height, nhtiles, nvtiles; |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 775 | int h_start, h_end, v_start, v_end; |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 776 | const int ntiles = |
| 777 | av1_get_rest_ntiles(width, height, cm->rst_info[1].restoration_tilesize, |
| 778 | &tile_width, &tile_height, &nhtiles, &nvtiles); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 779 | assert(width == dgd->uv_crop_width); |
| 780 | assert(height == dgd->uv_crop_height); |
| 781 | |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 782 | rsi[plane].frame_restoration_type = RESTORE_NONE; |
David Barker | 60a055b | 2017-01-18 15:10:43 +0000 | [diff] [blame] | 783 | err = sse_restoration_frame(cm, src, cm->frame_to_show, (1 << plane)); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 784 | bits = 0; |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 785 | cost_norestore_frame = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 786 | |
| 787 | rsi[plane].frame_restoration_type = RESTORE_WIENER; |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 788 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 789 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 790 | rsi[plane].restoration_type[tile_idx] = RESTORE_NONE; |
| 791 | } |
| 792 | |
| 793 | // Compute best Wiener filters for each tile |
| 794 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 795 | av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width, |
| 796 | tile_height, width, height, 0, 0, &h_start, &h_end, |
| 797 | &v_start, &v_end); |
| 798 | err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start, |
| 799 | h_end - h_start, v_start, v_end - v_start, |
| 800 | 1 << plane); |
| 801 | // #bits when a tile is not restored |
| 802 | bits = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 0); |
| 803 | cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
| 804 | // best_tile_cost[tile_idx] = DBL_MAX; |
| 805 | |
| 806 | av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width, |
| 807 | tile_height, width, height, WIENER_HALFWIN, |
| 808 | WIENER_HALFWIN, &h_start, &h_end, &v_start, |
| 809 | &v_end); |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 810 | if (plane == AOM_PLANE_U) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 811 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 812 | if (cm->use_highbitdepth) |
| 813 | compute_stats_highbd(dgd->u_buffer, src->u_buffer, h_start, h_end, |
| 814 | v_start, v_end, dgd_stride, src_stride, M, H); |
| 815 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 816 | #endif // CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 817 | compute_stats(dgd->u_buffer, src->u_buffer, h_start, h_end, v_start, |
| 818 | v_end, dgd_stride, src_stride, M, H); |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 819 | } else if (plane == AOM_PLANE_V) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 820 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 821 | if (cm->use_highbitdepth) |
| 822 | compute_stats_highbd(dgd->v_buffer, src->v_buffer, h_start, h_end, |
| 823 | v_start, v_end, dgd_stride, src_stride, M, H); |
| 824 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 825 | #endif // CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 826 | compute_stats(dgd->v_buffer, src->v_buffer, h_start, h_end, v_start, |
| 827 | v_end, dgd_stride, src_stride, M, H); |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 828 | } else { |
| 829 | assert(0); |
| 830 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 831 | |
| 832 | type[tile_idx] = RESTORE_WIENER; |
| 833 | |
| 834 | if (!wiener_decompose_sep_sym(M, H, vfilterd, hfilterd)) { |
| 835 | type[tile_idx] = RESTORE_NONE; |
| 836 | continue; |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 837 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 838 | quantize_sym_filter(vfilterd, rsi[plane].wiener_info[tile_idx].vfilter); |
| 839 | quantize_sym_filter(hfilterd, rsi[plane].wiener_info[tile_idx].hfilter); |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 840 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 841 | // Filter score computes the value of the function x'*A*x - x'*b for the |
| 842 | // learned filter and compares it against identity filer. If there is no |
| 843 | // reduction in the function, the filter is reverted back to identity |
| 844 | score = compute_score(M, H, rsi[plane].wiener_info[tile_idx].vfilter, |
| 845 | rsi[plane].wiener_info[tile_idx].hfilter); |
| 846 | if (score > 0.0) { |
| 847 | type[tile_idx] = RESTORE_NONE; |
| 848 | continue; |
| 849 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 850 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 851 | rsi[plane].restoration_type[tile_idx] = RESTORE_WIENER; |
| 852 | err = try_restoration_tile(src, cpi, rsi, 1 << plane, partial_frame, |
| 853 | tile_idx, 0, 0, dst_frame); |
| 854 | bits = WIENER_FILT_BITS << AV1_PROB_COST_SHIFT; |
| 855 | bits += av1_cost_bit(RESTORE_NONE_WIENER_PROB, 1); |
| 856 | cost_wiener = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
| 857 | if (cost_wiener >= cost_norestore) { |
| 858 | type[tile_idx] = RESTORE_NONE; |
| 859 | } else { |
| 860 | type[tile_idx] = RESTORE_WIENER; |
| 861 | memcpy(&wiener_info[tile_idx], &rsi[plane].wiener_info[tile_idx], |
| 862 | sizeof(wiener_info[tile_idx])); |
| 863 | } |
| 864 | rsi[plane].restoration_type[tile_idx] = RESTORE_NONE; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 865 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 866 | // Cost for Wiener filtering |
| 867 | bits = 0; |
| 868 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 869 | bits += |
| 870 | av1_cost_bit(RESTORE_NONE_WIENER_PROB, type[tile_idx] != RESTORE_NONE); |
| 871 | memcpy(&rsi[plane].wiener_info[tile_idx], &wiener_info[tile_idx], |
| 872 | sizeof(wiener_info[tile_idx])); |
| 873 | if (type[tile_idx] == RESTORE_WIENER) { |
| 874 | bits += (WIENER_FILT_BITS << AV1_PROB_COST_SHIFT); |
| 875 | } |
| 876 | rsi[plane].restoration_type[tile_idx] = type[tile_idx]; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 877 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 878 | err = try_restoration_frame(src, cpi, rsi, 1 << plane, partial_frame, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 879 | dst_frame); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 880 | cost_wiener_frame = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
| 881 | |
| 882 | if (cost_wiener_frame < cost_norestore_frame) { |
| 883 | info->frame_restoration_type = RESTORE_WIENER; |
| 884 | } else { |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 885 | info->frame_restoration_type = RESTORE_NONE; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 888 | return info->frame_restoration_type == RESTORE_WIENER ? cost_wiener_frame |
| 889 | : cost_norestore_frame; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 892 | static double search_wiener(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi, |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 893 | int partial_frame, RestorationInfo *info, |
| 894 | RestorationType *type, double *best_tile_cost, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 895 | YV12_BUFFER_CONFIG *dst_frame) { |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 896 | WienerInfo *wiener_info = info->wiener_info; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 897 | AV1_COMMON *const cm = &cpi->common; |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 898 | RestorationInfo *rsi = cpi->rst_search; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 899 | int64_t err; |
| 900 | int bits; |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 901 | double cost_wiener, cost_norestore; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 902 | MACROBLOCK *x = &cpi->td.mb; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 903 | double M[WIENER_WIN2]; |
| 904 | double H[WIENER_WIN2 * WIENER_WIN2]; |
| 905 | double vfilterd[WIENER_WIN], hfilterd[WIENER_WIN]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 906 | const YV12_BUFFER_CONFIG *dgd = cm->frame_to_show; |
| 907 | const int width = cm->width; |
| 908 | const int height = cm->height; |
| 909 | const int src_stride = src->y_stride; |
| 910 | const int dgd_stride = dgd->y_stride; |
| 911 | double score; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 912 | int tile_idx, tile_width, tile_height, nhtiles, nvtiles; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 913 | int h_start, h_end, v_start, v_end; |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 914 | const int ntiles = |
| 915 | av1_get_rest_ntiles(width, height, cm->rst_info[0].restoration_tilesize, |
| 916 | &tile_width, &tile_height, &nhtiles, &nvtiles); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 917 | assert(width == dgd->y_crop_width); |
| 918 | assert(height == dgd->y_crop_height); |
| 919 | assert(width == src->y_crop_width); |
| 920 | assert(height == src->y_crop_height); |
| 921 | |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 922 | rsi->frame_restoration_type = RESTORE_WIENER; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 923 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 924 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 925 | rsi->restoration_type[tile_idx] = RESTORE_NONE; |
| 926 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 927 | |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 928 | // Construct a (WIENER_HALFWIN)-pixel border around the frame |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 929 | #if CONFIG_HIGHBITDEPTH |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 930 | if (cm->use_highbitdepth) |
| 931 | extend_frame_highbd(CONVERT_TO_SHORTPTR(dgd->y_buffer), width, height, |
| 932 | dgd_stride); |
| 933 | else |
| 934 | #endif |
| 935 | extend_frame(dgd->y_buffer, width, height, dgd_stride); |
| 936 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 937 | // Compute best Wiener filters for each tile |
| 938 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 939 | av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width, |
| 940 | tile_height, width, height, 0, 0, &h_start, &h_end, |
| 941 | &v_start, &v_end); |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 942 | err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start, |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 943 | h_end - h_start, v_start, v_end - v_start, 1); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 944 | // #bits when a tile is not restored |
| 945 | bits = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 0); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 946 | cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 947 | best_tile_cost[tile_idx] = DBL_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 948 | |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 949 | av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width, |
David Barker | 33f3bfd | 2017-01-06 15:34:50 +0000 | [diff] [blame] | 950 | tile_height, width, height, 0, 0, &h_start, &h_end, |
| 951 | &v_start, &v_end); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 952 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 953 | if (cm->use_highbitdepth) |
| 954 | compute_stats_highbd(dgd->y_buffer, src->y_buffer, h_start, h_end, |
| 955 | v_start, v_end, dgd_stride, src_stride, M, H); |
| 956 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 957 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 958 | compute_stats(dgd->y_buffer, src->y_buffer, h_start, h_end, v_start, |
| 959 | v_end, dgd_stride, src_stride, M, H); |
| 960 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 961 | type[tile_idx] = RESTORE_WIENER; |
| 962 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 963 | if (!wiener_decompose_sep_sym(M, H, vfilterd, hfilterd)) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 964 | type[tile_idx] = RESTORE_NONE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 965 | continue; |
| 966 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 967 | quantize_sym_filter(vfilterd, rsi->wiener_info[tile_idx].vfilter); |
| 968 | quantize_sym_filter(hfilterd, rsi->wiener_info[tile_idx].hfilter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 969 | |
| 970 | // Filter score computes the value of the function x'*A*x - x'*b for the |
| 971 | // learned filter and compares it against identity filer. If there is no |
| 972 | // reduction in the function, the filter is reverted back to identity |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 973 | score = compute_score(M, H, rsi->wiener_info[tile_idx].vfilter, |
| 974 | rsi->wiener_info[tile_idx].hfilter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 975 | if (score > 0.0) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 976 | type[tile_idx] = RESTORE_NONE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 977 | continue; |
| 978 | } |
| 979 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 980 | rsi->restoration_type[tile_idx] = RESTORE_WIENER; |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 981 | err = try_restoration_tile(src, cpi, rsi, 1, partial_frame, tile_idx, 0, 0, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 982 | dst_frame); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 983 | bits = WIENER_FILT_BITS << AV1_PROB_COST_SHIFT; |
| 984 | bits += av1_cost_bit(RESTORE_NONE_WIENER_PROB, 1); |
| 985 | cost_wiener = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 986 | if (cost_wiener >= cost_norestore) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 987 | type[tile_idx] = RESTORE_NONE; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 988 | } else { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 989 | type[tile_idx] = RESTORE_WIENER; |
| 990 | memcpy(&wiener_info[tile_idx], &rsi->wiener_info[tile_idx], |
| 991 | sizeof(wiener_info[tile_idx])); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 992 | bits = WIENER_FILT_BITS << AV1_PROB_COST_SHIFT; |
| 993 | best_tile_cost[tile_idx] = RDCOST_DBL( |
| 994 | x->rdmult, x->rddiv, |
| 995 | (bits + cpi->switchable_restore_cost[RESTORE_WIENER]) >> 4, err); |
| 996 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 997 | rsi->restoration_type[tile_idx] = RESTORE_NONE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 998 | } |
| 999 | // Cost for Wiener filtering |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 1000 | bits = frame_level_restore_bits[rsi->frame_restoration_type] |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1001 | << AV1_PROB_COST_SHIFT; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1002 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1003 | bits += |
| 1004 | av1_cost_bit(RESTORE_NONE_WIENER_PROB, type[tile_idx] != RESTORE_NONE); |
| 1005 | memcpy(&rsi->wiener_info[tile_idx], &wiener_info[tile_idx], |
| 1006 | sizeof(wiener_info[tile_idx])); |
| 1007 | if (type[tile_idx] == RESTORE_WIENER) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1008 | bits += (WIENER_FILT_BITS << AV1_PROB_COST_SHIFT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1009 | } |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1010 | rsi->restoration_type[tile_idx] = type[tile_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1011 | } |
Debargha Mukherjee | 999d2f6 | 2016-12-15 13:23:21 -0800 | [diff] [blame] | 1012 | err = try_restoration_frame(src, cpi, rsi, 1, partial_frame, dst_frame); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1013 | cost_wiener = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 1014 | |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1015 | return cost_wiener; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1018 | static double search_norestore(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi, |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1019 | int partial_frame, RestorationInfo *info, |
| 1020 | RestorationType *type, double *best_tile_cost, |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 1021 | YV12_BUFFER_CONFIG *dst_frame) { |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1022 | double err, cost_norestore; |
| 1023 | int bits; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1024 | MACROBLOCK *x = &cpi->td.mb; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1025 | AV1_COMMON *const cm = &cpi->common; |
| 1026 | int tile_idx, tile_width, tile_height, nhtiles, nvtiles; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1027 | int h_start, h_end, v_start, v_end; |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 1028 | const int ntiles = av1_get_rest_ntiles( |
| 1029 | cm->width, cm->height, cm->rst_info[0].restoration_tilesize, &tile_width, |
| 1030 | &tile_height, &nhtiles, &nvtiles); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1031 | (void)info; |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 1032 | (void)dst_frame; |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1033 | (void)partial_frame; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1034 | |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1035 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 1036 | av1_get_rest_tile_limits(tile_idx, 0, 0, nhtiles, nvtiles, tile_width, |
| 1037 | tile_height, cm->width, cm->height, 0, 0, &h_start, |
| 1038 | &h_end, &v_start, &v_end); |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 1039 | err = sse_restoration_tile(src, cm->frame_to_show, cm, h_start, |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 1040 | h_end - h_start, v_start, v_end - v_start, 1); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1041 | best_tile_cost[tile_idx] = |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1042 | RDCOST_DBL(x->rdmult, x->rddiv, |
| 1043 | (cpi->switchable_restore_cost[RESTORE_NONE] >> 4), err); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1044 | type[tile_idx] = RESTORE_NONE; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1045 | } |
| 1046 | // RD cost associated with no restoration |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 1047 | err = sse_restoration_tile(src, cm->frame_to_show, cm, 0, cm->width, 0, |
Debargha Mukherjee | 874d36d | 2016-12-14 16:53:17 -0800 | [diff] [blame] | 1048 | cm->height, 1); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1049 | bits = frame_level_restore_bits[RESTORE_NONE] << AV1_PROB_COST_SHIFT; |
| 1050 | cost_norestore = RDCOST_DBL(x->rdmult, x->rddiv, (bits >> 4), err); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1051 | return cost_norestore; |
| 1052 | } |
| 1053 | |
| 1054 | static double search_switchable_restoration( |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1055 | AV1_COMP *cpi, int partial_frame, RestorationInfo *rsi, |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1056 | double *tile_cost[RESTORE_SWITCHABLE_TYPES]) { |
| 1057 | AV1_COMMON *const cm = &cpi->common; |
| 1058 | MACROBLOCK *x = &cpi->td.mb; |
| 1059 | double cost_switchable = 0; |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 1060 | int bits, tile_idx; |
| 1061 | RestorationType r; |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 1062 | const int ntiles = av1_get_rest_ntiles(cm->width, cm->height, |
| 1063 | cm->rst_info[0].restoration_tilesize, |
| 1064 | NULL, NULL, NULL, NULL); |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1065 | (void)partial_frame; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1066 | |
| 1067 | rsi->frame_restoration_type = RESTORE_SWITCHABLE; |
| 1068 | bits = frame_level_restore_bits[rsi->frame_restoration_type] |
| 1069 | << AV1_PROB_COST_SHIFT; |
| 1070 | cost_switchable = RDCOST_DBL(x->rdmult, x->rddiv, bits >> 4, 0); |
| 1071 | for (tile_idx = 0; tile_idx < ntiles; ++tile_idx) { |
| 1072 | double best_cost = tile_cost[RESTORE_NONE][tile_idx]; |
| 1073 | rsi->restoration_type[tile_idx] = RESTORE_NONE; |
| 1074 | for (r = 1; r < RESTORE_SWITCHABLE_TYPES; r++) { |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 1075 | if (r != force_restore_type) continue; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1076 | if (tile_cost[r][tile_idx] < best_cost) { |
| 1077 | rsi->restoration_type[tile_idx] = r; |
| 1078 | best_cost = tile_cost[r][tile_idx]; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 1081 | if (force_restore_type != 0) |
| 1082 | assert(rsi->restoration_type[tile_idx] == force_restore_type || |
| 1083 | rsi->restoration_type[tile_idx] == RESTORE_NONE); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1084 | cost_switchable += best_cost; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1085 | } |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1086 | return cost_switchable; |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1090 | LPF_PICK_METHOD method) { |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1091 | static search_restore_type search_restore_fun[RESTORE_SWITCHABLE_TYPES] = { |
Debargha Mukherjee | 4bfd72e | 2017-03-08 22:20:31 -0800 | [diff] [blame] | 1092 | search_norestore, search_wiener, search_sgrproj, |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1093 | }; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1094 | AV1_COMMON *const cm = &cpi->common; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1095 | double cost_restore[RESTORE_TYPES]; |
| 1096 | double *tile_cost[RESTORE_SWITCHABLE_TYPES]; |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1097 | RestorationType *restore_types[RESTORE_SWITCHABLE_TYPES]; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1098 | double best_cost_restore; |
| 1099 | RestorationType r, best_restore; |
| 1100 | |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 1101 | const int ntiles = av1_get_rest_ntiles(cm->width, cm->height, |
| 1102 | cm->rst_info[0].restoration_tilesize, |
| 1103 | NULL, NULL, NULL, NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1104 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1105 | for (r = 0; r < RESTORE_SWITCHABLE_TYPES; r++) { |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1106 | tile_cost[r] = (double *)aom_malloc(sizeof(*tile_cost[0]) * ntiles); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1107 | restore_types[r] = |
| 1108 | (RestorationType *)aom_malloc(sizeof(*restore_types[0]) * ntiles); |
| 1109 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1110 | |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1111 | for (r = 0; r < RESTORE_SWITCHABLE_TYPES; ++r) { |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 1112 | if (force_restore_type != 0) |
| 1113 | if (r != RESTORE_NONE && r != force_restore_type) continue; |
David Barker | 9666e75 | 2016-12-08 11:25:47 +0000 | [diff] [blame] | 1114 | cost_restore[r] = search_restore_fun[r]( |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1115 | src, cpi, method == LPF_PICK_FROM_SUBIMAGE, &cm->rst_info[0], |
| 1116 | restore_types[r], tile_cost[r], &cpi->trial_frame_rst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1117 | } |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1118 | cost_restore[RESTORE_SWITCHABLE] = search_switchable_restoration( |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1119 | cpi, method == LPF_PICK_FROM_SUBIMAGE, &cm->rst_info[0], tile_cost); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1120 | |
| 1121 | best_cost_restore = DBL_MAX; |
| 1122 | best_restore = 0; |
| 1123 | for (r = 0; r < RESTORE_TYPES; ++r) { |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 1124 | if (force_restore_type != 0) |
| 1125 | if (r != RESTORE_NONE && r != force_restore_type) continue; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1126 | if (cost_restore[r] < best_cost_restore) { |
| 1127 | best_restore = r; |
| 1128 | best_cost_restore = cost_restore[r]; |
| 1129 | } |
| 1130 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1131 | cm->rst_info[0].frame_restoration_type = best_restore; |
Debargha Mukherjee | fdbe3f7 | 2017-04-06 12:09:19 -0700 | [diff] [blame] | 1132 | if (force_restore_type != 0) |
| 1133 | assert(best_restore == force_restore_type || best_restore == RESTORE_NONE); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1134 | if (best_restore != RESTORE_SWITCHABLE) { |
| 1135 | memcpy(cm->rst_info[0].restoration_type, restore_types[best_restore], |
| 1136 | ntiles * sizeof(restore_types[best_restore][0])); |
| 1137 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1138 | |
| 1139 | // Color components |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1140 | search_wiener_uv(src, cpi, method == LPF_PICK_FROM_SUBIMAGE, AOM_PLANE_U, |
| 1141 | &cm->rst_info[AOM_PLANE_U], |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1142 | cm->rst_info[AOM_PLANE_U].restoration_type, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1143 | &cpi->trial_frame_rst); |
Debargha Mukherjee | 00c5433 | 2017-03-03 15:44:17 -0800 | [diff] [blame] | 1144 | search_wiener_uv(src, cpi, method == LPF_PICK_FROM_SUBIMAGE, AOM_PLANE_V, |
| 1145 | &cm->rst_info[AOM_PLANE_V], |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1146 | cm->rst_info[AOM_PLANE_V].restoration_type, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1147 | &cpi->trial_frame_rst); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1148 | /* |
Debargha Mukherjee | 09ad6d8 | 2017-01-06 13:02:57 -0800 | [diff] [blame] | 1149 | printf("Frame %d/%d restore types: %d %d %d\n", |
| 1150 | cm->current_video_frame, cm->show_frame, |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1151 | cm->rst_info[0].frame_restoration_type, |
| 1152 | cm->rst_info[1].frame_restoration_type, |
| 1153 | cm->rst_info[2].frame_restoration_type); |
Debargha Mukherjee | b3c43bc | 2017-02-01 13:09:03 -0800 | [diff] [blame] | 1154 | printf("Frame %d/%d frame_restore_type %d : %f %f %f %f\n", |
| 1155 | cm->current_video_frame, cm->show_frame, |
| 1156 | cm->rst_info[0].frame_restoration_type, cost_restore[0], |
| 1157 | cost_restore[1], cost_restore[2], cost_restore[3]); |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 1158 | */ |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 1159 | |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 1160 | for (r = 0; r < RESTORE_SWITCHABLE_TYPES; r++) { |
| 1161 | aom_free(tile_cost[r]); |
| 1162 | aom_free(restore_types[r]); |
| 1163 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1164 | } |