blob: 488dc2729525f819479596065ef901253fdffa65 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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 Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <string.h>
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -040013#include <math.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#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 Xuf883b422016-08-30 14:01:10 -070020#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021
22static 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 Xuf883b422016-08-30 14:01:10 -070037int av1_dering_search(YV12_BUFFER_CONFIG *frame, const YV12_BUFFER_CONFIG *ref,
38 AV1_COMMON *cm, MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070039 int r, c;
40 int sbr, sbc;
41 int nhsb, nvsb;
42 od_dering_in *src;
43 int16_t *ref_coeff;
44 unsigned char *bskip;
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 Xuc27fc142016-08-22 16:08:15 -070050 int level;
51 int best_level;
Yaowu Xuf883b422016-08-30 14:01:10 -070052 int coeff_shift = AOMMAX(cm->bit_depth - 8, 0);
53 src = aom_malloc(sizeof(*src) * cm->mi_rows * cm->mi_cols * 64);
54 ref_coeff = aom_malloc(sizeof(*ref_coeff) * cm->mi_rows * cm->mi_cols * 64);
55 bskip = aom_malloc(sizeof(*bskip) * cm->mi_rows * cm->mi_cols);
56 av1_setup_dst_planes(xd->plane, frame, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070057 for (pli = 0; pli < 3; pli++) {
58 dec[pli] = xd->plane[pli].subsampling_x;
59 bsize[pli] = 8 >> dec[pli];
60 }
61 stride = bsize[0] * cm->mi_cols;
62 for (r = 0; r < bsize[0] * cm->mi_rows; ++r) {
63 for (c = 0; c < bsize[0] * cm->mi_cols; ++c) {
Yaowu Xuf883b422016-08-30 14:01:10 -070064#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070065 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 Xuf883b422016-08-30 14:01:10 -070075#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070076 }
77#endif
78 }
79 }
80 for (r = 0; r < cm->mi_rows; ++r) {
81 for (c = 0; c < cm->mi_cols; ++c) {
82 const MB_MODE_INFO *mbmi =
83 &cm->mi_grid_visible[r * cm->mi_stride + c]->mbmi;
84 bskip[r * cm->mi_cols + c] = mbmi->skip;
85 }
86 }
87 nvsb = (cm->mi_rows + MAX_MIB_SIZE - 1) / MAX_MIB_SIZE;
88 nhsb = (cm->mi_cols + MAX_MIB_SIZE - 1) / MAX_MIB_SIZE;
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -040089 /* Pick a base threshold based on the quantizer. The threshold will then be
90 adjusted on a 64x64 basis. We use a threshold of the form T = a*Q^b,
91 where a and b are derived empirically trying to optimize rate-distortion
92 at different quantizer settings. */
93 best_level = (int)floor(
94 .5 + .45 * pow(av1_ac_quant(cm->base_qindex, 0, cm->bit_depth), 0.6));
Yaowu Xuc27fc142016-08-22 16:08:15 -070095 for (sbr = 0; sbr < nvsb; sbr++) {
96 for (sbc = 0; sbc < nhsb; sbc++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070097 int nvb, nhb;
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -040098 int gi;
99 int best_gi;
100 int32_t best_mse = INT32_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 int16_t dst[MAX_MIB_SIZE * MAX_MIB_SIZE * 8 * 8];
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 nhb = AOMMIN(MAX_MIB_SIZE, cm->mi_cols - MAX_MIB_SIZE * sbc);
103 nvb = AOMMIN(MAX_MIB_SIZE, cm->mi_rows - MAX_MIB_SIZE * sbr);
Jean-Marc Valin56b0c3c2016-07-18 11:48:59 -0400104 if (sb_all_skip(cm, sbr * MAX_MIB_SIZE, sbc * MAX_MIB_SIZE)) continue;
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -0400105 best_gi = 0;
106 for (gi = 0; gi < DERING_REFINEMENT_LEVELS; gi++) {
Yaowu Xu9c323bc2016-09-01 11:35:16 -0700107 int cur_mse;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108 int threshold;
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -0400109 level = compute_level_from_index(best_level, gi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110 threshold = level << coeff_shift;
111 od_dering(
112 &OD_DERING_VTBL_C, dst, MAX_MIB_SIZE * bsize[0],
113 &src[sbr * stride * bsize[0] * MAX_MIB_SIZE +
114 sbc * bsize[0] * MAX_MIB_SIZE],
115 cm->mi_cols * bsize[0], nhb, nvb, sbc, sbr, nhsb, nvsb, 0, dir, 0,
116 &bskip[MAX_MIB_SIZE * sbr * cm->mi_cols + MAX_MIB_SIZE * sbc],
117 cm->mi_cols, threshold, OD_DERING_NO_CHECK_OVERLAP, coeff_shift);
Yaowu Xu9c323bc2016-09-01 11:35:16 -0700118 cur_mse = (int)compute_dist(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119 dst, MAX_MIB_SIZE * bsize[0],
120 &ref_coeff[sbr * stride * bsize[0] * MAX_MIB_SIZE +
121 sbc * bsize[0] * MAX_MIB_SIZE],
122 stride, nhb, nvb, coeff_shift);
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -0400123 if (cur_mse < best_mse) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700124 best_gi = gi;
Jean-Marc Valin6d5a7a92016-08-22 15:47:09 -0400125 best_mse = cur_mse;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 }
127 }
128 cm->mi_grid_visible[MAX_MIB_SIZE * sbr * cm->mi_stride +
129 MAX_MIB_SIZE * sbc]
130 ->mbmi.dering_gain = best_gi;
131 }
132 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 aom_free(src);
134 aom_free(ref_coeff);
135 aom_free(bskip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136 return best_level;
137}