/src/x265/source/encoder/rdcost.h
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright (C) 2013-2020 MulticoreWare, Inc |
3 | | * |
4 | | * Authors: Steve Borho <steve@borho.org> |
5 | | * Min Chen <chenm003@163.com> |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
20 | | * |
21 | | * This program is also available under a commercial proprietary license. |
22 | | * For more information, contact us at license @ x265.com. |
23 | | *****************************************************************************/ |
24 | | |
25 | | #ifndef X265_RDCOST_H |
26 | | #define X265_RDCOST_H |
27 | | |
28 | | #include "common.h" |
29 | | #include "slice.h" |
30 | | |
31 | | namespace X265_NS { |
32 | | // private namespace |
33 | | |
34 | | class RDCost |
35 | | { |
36 | | public: |
37 | | |
38 | | /* all weights and factors stored as FIX8 */ |
39 | | uint64_t m_lambda2; |
40 | | uint64_t m_lambda; |
41 | | uint32_t m_chromaDistWeight[2]; |
42 | | uint32_t m_psyRdBase; |
43 | | uint32_t m_psyRd; |
44 | | uint32_t m_ssimRd; |
45 | | int m_qp; /* QP used to configure lambda, may be higher than QP_MAX_SPEC but <= QP_MAX_MAX */ |
46 | | |
47 | 22.6k | void setPsyRdScale(double scale) { m_psyRdBase = (uint32_t)floor(65536.0 * scale * 0.33); } |
48 | 22.6k | void setSsimRd(int ssimRd) { m_ssimRd = ssimRd; }; |
49 | | |
50 | | void setQP(const Slice& slice, int qp) |
51 | 28.1k | { |
52 | 28.1k | x265_emms(); /* TODO: if the lambda tables were ints, this would not be necessary */ |
53 | 28.1k | m_qp = qp; |
54 | 28.1k | setLambda(x265_lambda2_tab[qp], x265_lambda_tab[qp]); |
55 | | |
56 | | /* Scale PSY RD factor by a slice type factor */ |
57 | 28.1k | static const uint32_t psyScaleFix8[3] = { 300, 256, 96 }; /* B, P, I */ |
58 | 28.1k | m_psyRd = (m_psyRdBase * psyScaleFix8[slice.m_sliceType]) >> 8; |
59 | | |
60 | | /* Scale PSY RD factor by QP, at high QP psy-rd can cause artifacts */ |
61 | 28.1k | if (qp >= 40) |
62 | 0 | { |
63 | 0 | int scale = qp >= QP_MAX_SPEC ? 0 : (QP_MAX_SPEC - qp) * 23; |
64 | 0 | m_psyRd = (m_psyRd * scale) >> 8; |
65 | 0 | } |
66 | | |
67 | 28.1k | int qpCb, qpCr; |
68 | 28.1k | if (slice.m_sps->chromaFormatIdc == X265_CSP_I420) |
69 | 28.1k | { |
70 | 28.1k | qpCb = (int)g_chromaScale[x265_clip3(QP_MIN, QP_MAX_MAX, qp + slice.m_pps->chromaQpOffset[0] + slice.m_chromaQpOffset[0])]; |
71 | 28.1k | qpCr = (int)g_chromaScale[x265_clip3(QP_MIN, QP_MAX_MAX, qp + slice.m_pps->chromaQpOffset[1] + slice.m_chromaQpOffset[1])]; |
72 | 28.1k | } |
73 | 2 | else |
74 | 2 | { |
75 | 2 | qpCb = x265_clip3(QP_MIN, QP_MAX_SPEC, qp + slice.m_pps->chromaQpOffset[0] + slice.m_chromaQpOffset[0]); |
76 | 2 | qpCr = x265_clip3(QP_MIN, QP_MAX_SPEC, qp + slice.m_pps->chromaQpOffset[1] + slice.m_chromaQpOffset[1]); |
77 | 2 | } |
78 | | |
79 | 28.1k | if (slice.m_sps->chromaFormatIdc == X265_CSP_I444) |
80 | 0 | { |
81 | 0 | int chroma_offset_idx = X265_MIN(qp - qpCb + 12, MAX_CHROMA_LAMBDA_OFFSET); |
82 | 0 | uint16_t lambdaOffset = m_psyRd ? x265_chroma_lambda2_offset_tab[chroma_offset_idx] : 256; |
83 | 0 | m_chromaDistWeight[0] = lambdaOffset; |
84 | |
|
85 | 0 | chroma_offset_idx = X265_MIN(qp - qpCr + 12, MAX_CHROMA_LAMBDA_OFFSET); |
86 | 0 | lambdaOffset = m_psyRd ? x265_chroma_lambda2_offset_tab[chroma_offset_idx] : 256; |
87 | 0 | m_chromaDistWeight[1] = lambdaOffset; |
88 | 0 | } |
89 | 28.1k | else |
90 | 28.1k | m_chromaDistWeight[0] = m_chromaDistWeight[1] = 256; |
91 | 28.1k | } |
92 | | |
93 | | void setLambda(double lambda2, double lambda) |
94 | 28.1k | { |
95 | 28.1k | m_lambda2 = (uint64_t)floor(256.0 * lambda2); |
96 | 28.1k | m_lambda = (uint64_t)floor(256.0 * lambda); |
97 | 28.1k | } |
98 | | |
99 | | inline uint64_t calcRdCost(sse_t distortion, uint32_t bits) const |
100 | 0 | { |
101 | 0 | #if X265_DEPTH < 10 |
102 | 0 | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda2, |
103 | 0 | "calcRdCost wrap detected dist: %u, bits %u, lambda: " X265_LL "\n", |
104 | 0 | distortion, bits, m_lambda2); |
105 | | #else |
106 | | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda2, |
107 | | "calcRdCost wrap detected dist: " X265_LL ", bits %u, lambda: " X265_LL "\n", |
108 | | distortion, bits, m_lambda2); |
109 | | #endif |
110 | 0 | return distortion + ((bits * m_lambda2 + 128) >> 8); |
111 | 0 | } |
112 | | |
113 | | /* return the difference in energy between the source block and the recon block */ |
114 | | inline int psyCost(int size, const pixel* source, intptr_t sstride, const pixel* recon, intptr_t rstride) const |
115 | 13.7M | { |
116 | 13.7M | return primitives.cu[size].psy_cost_pp(source, sstride, recon, rstride); |
117 | 13.7M | } |
118 | | |
119 | | /* return the RD cost of this prediction, including the effect of psy-rd */ |
120 | | inline uint64_t calcPsyRdCost(sse_t distortion, uint32_t bits, uint32_t psycost) const |
121 | 10.5M | { |
122 | 10.5M | #if X265_DEPTH < 10 |
123 | 10.5M | X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (psycost <= UINT64_MAX / (m_lambda * m_psyRd)), |
124 | 10.5M | "calcPsyRdCost wrap detected dist: %u, bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n", |
125 | 10.5M | distortion, bits, m_lambda, m_lambda2); |
126 | | #else |
127 | | X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (psycost <= UINT64_MAX / (m_lambda * m_psyRd)), |
128 | | "calcPsyRdCost wrap detected dist: " X265_LL ", bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n", |
129 | | distortion, bits, m_lambda, m_lambda2); |
130 | | #endif |
131 | 10.5M | return distortion + ((m_lambda * m_psyRd * psycost) >> 24) + ((bits * m_lambda2) >> 8); |
132 | 10.5M | } |
133 | | |
134 | | inline uint64_t calcSsimRdCost(uint64_t distortion, uint32_t bits, uint32_t ssimCost) const |
135 | 0 | { |
136 | 0 | #if X265_DEPTH < 10 |
137 | 0 | X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (ssimCost <= UINT64_MAX / m_lambda), |
138 | 0 | "calcPsyRdCost wrap detected dist: " X265_LL " bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n", |
139 | 0 | distortion, bits, m_lambda, m_lambda2); |
140 | | #else |
141 | | X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (ssimCost <= UINT64_MAX / m_lambda), |
142 | | "calcPsyRdCost wrap detected dist: " X265_LL ", bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n", |
143 | | distortion, bits, m_lambda, m_lambda2); |
144 | | #endif |
145 | 0 | return distortion + ((m_lambda * ssimCost) >> 14) + ((bits * m_lambda2) >> 8); |
146 | 0 | } |
147 | | |
148 | | inline uint64_t calcRdSADCost(uint32_t sadCost, uint32_t bits) const |
149 | 61.4M | { |
150 | 61.4M | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda, |
151 | 61.4M | "calcRdSADCost wrap detected dist: %u, bits %u, lambda: " X265_LL "\n", sadCost, bits, m_lambda); |
152 | 61.4M | return sadCost + ((bits * m_lambda + 128) >> 8); |
153 | 61.4M | } |
154 | | |
155 | | inline sse_t scaleChromaDist(uint32_t plane, sse_t dist) const |
156 | 7.73M | { |
157 | 7.73M | #if X265_DEPTH < 10 |
158 | 7.73M | X265_CHECK(dist <= (UINT64_MAX - 128) / m_chromaDistWeight[plane - 1], |
159 | 7.73M | "scaleChromaDist wrap detected dist: %u, lambda: %u\n", |
160 | 7.73M | dist, m_chromaDistWeight[plane - 1]); |
161 | | #else |
162 | | X265_CHECK(dist <= (UINT64_MAX - 128) / m_chromaDistWeight[plane - 1], |
163 | | "scaleChromaDist wrap detected dist: " X265_LL " lambda: %u\n", |
164 | | dist, m_chromaDistWeight[plane - 1]); |
165 | | #endif |
166 | 7.73M | return (sse_t)((dist * (uint64_t)m_chromaDistWeight[plane - 1] + 128) >> 8); |
167 | 7.73M | } |
168 | | |
169 | | inline uint32_t getCost(uint32_t bits) const |
170 | 0 | { |
171 | 0 | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda, |
172 | 0 | "getCost wrap detected bits: %u, lambda: " X265_LL "\n", bits, m_lambda); |
173 | 0 | return (uint32_t)((bits * m_lambda + 128) >> 8); |
174 | 0 | } |
175 | | }; |
176 | | } |
177 | | |
178 | | #endif // ifndef X265_TCOMRDCOST_H |