Commit | Line | Data |
---|---|---|
72b9787e JB |
1 | /***************************************************************************** |
2 | * Copyright (C) 2013 x265 project | |
3 | * | |
4 | * Authors: Steve Borho <steve@borho.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. | |
19 | * | |
20 | * This program is also available under a commercial proprietary license. | |
21 | * For more information, contact us at license @ x265.com. | |
22 | *****************************************************************************/ | |
23 | ||
24 | #ifndef X265_RDCOST_H | |
25 | #define X265_RDCOST_H | |
26 | ||
27 | #include "common.h" | |
28 | #include "slice.h" | |
29 | ||
30 | namespace x265 { | |
31 | // private namespace | |
32 | ||
33 | class RDCost | |
34 | { | |
35 | public: | |
36 | ||
37 | /* all weights and factors stored as FIX8 */ | |
38 | uint64_t m_lambda2; | |
39 | uint64_t m_lambda; | |
b53f7c52 JB |
40 | uint32_t m_chromaDistWeight[2]; |
41 | uint32_t m_psyRdBase; | |
72b9787e JB |
42 | uint32_t m_psyRd; |
43 | int m_qp; | |
44 | ||
b53f7c52 | 45 | void setPsyRdScale(double scale) { m_psyRdBase = (uint32_t)floor(256.0 * scale * 0.33); } |
72b9787e JB |
46 | |
47 | void setQP(const Slice& slice, int qp) | |
48 | { | |
49 | m_qp = qp; | |
b53f7c52 JB |
50 | int qpCb, qpCr; |
51 | /* Scale PSY RD factor by a slice type factor */ | |
52 | static const uint32_t psyScaleFix8[3] = { 300, 256, 96 }; /* B, P, I */ | |
53 | m_psyRd = (m_psyRdBase * psyScaleFix8[slice.m_sliceType]) >> 8; | |
72b9787e JB |
54 | |
55 | setLambda(x265_lambda2_tab[qp], x265_lambda_tab[qp]); | |
b53f7c52 JB |
56 | if (slice.m_sps->chromaFormatIdc == X265_CSP_I420) |
57 | qpCb = Clip3(QP_MIN, QP_MAX_MAX, (int)g_chromaScale[qp + slice.m_pps->chromaQpOffset[0]]); | |
58 | else | |
59 | qpCb = X265_MIN(qp + slice.m_pps->chromaQpOffset[0], QP_MAX_SPEC); | |
72b9787e JB |
60 | int chroma_offset_idx = X265_MIN(qp - qpCb + 12, MAX_CHROMA_LAMBDA_OFFSET); |
61 | uint16_t lambdaOffset = m_psyRd ? x265_chroma_lambda2_offset_tab[chroma_offset_idx] : 256; | |
b53f7c52 | 62 | m_chromaDistWeight[0] = lambdaOffset; |
72b9787e | 63 | |
b53f7c52 JB |
64 | if (slice.m_sps->chromaFormatIdc == X265_CSP_I420) |
65 | qpCr = Clip3(QP_MIN, QP_MAX_MAX, (int)g_chromaScale[qp + slice.m_pps->chromaQpOffset[0]]); | |
66 | else | |
67 | qpCr = X265_MIN(qp + slice.m_pps->chromaQpOffset[0], QP_MAX_SPEC); | |
72b9787e JB |
68 | chroma_offset_idx = X265_MIN(qp - qpCr + 12, MAX_CHROMA_LAMBDA_OFFSET); |
69 | lambdaOffset = m_psyRd ? x265_chroma_lambda2_offset_tab[chroma_offset_idx] : 256; | |
b53f7c52 | 70 | m_chromaDistWeight[1] = lambdaOffset; |
72b9787e JB |
71 | } |
72 | ||
73 | void setLambda(double lambda2, double lambda) | |
74 | { | |
75 | m_lambda2 = (uint64_t)floor(256.0 * lambda2); | |
76 | m_lambda = (uint64_t)floor(256.0 * lambda); | |
77 | } | |
78 | ||
79 | inline uint64_t calcRdCost(uint32_t distortion, uint32_t bits) const | |
80 | { | |
81 | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda2, | |
b53f7c52 | 82 | "calcRdCost wrap detected dist: %u, bits %u, lambda: "X265_LL"\n", distortion, bits, m_lambda2); |
72b9787e JB |
83 | return distortion + ((bits * m_lambda2 + 128) >> 8); |
84 | } | |
85 | ||
86 | /* return the difference in energy between the source block and the recon block */ | |
b53f7c52 | 87 | inline int psyCost(int size, const pixel* source, intptr_t sstride, const pixel* recon, intptr_t rstride) const |
72b9787e JB |
88 | { |
89 | return primitives.psy_cost_pp[size](source, sstride, recon, rstride); | |
90 | } | |
91 | ||
92 | /* return the difference in energy between the source block and the recon block */ | |
b53f7c52 | 93 | inline int psyCost(int size, const int16_t* source, intptr_t sstride, const int16_t* recon, intptr_t rstride) const |
72b9787e JB |
94 | { |
95 | return primitives.psy_cost_ss[size](source, sstride, recon, rstride); | |
96 | } | |
97 | ||
98 | /* return the RD cost of this prediction, including the effect of psy-rd */ | |
99 | inline uint64_t calcPsyRdCost(uint32_t distortion, uint32_t bits, uint32_t psycost) const | |
100 | { | |
101 | return distortion + ((m_lambda * m_psyRd * psycost) >> 16) + ((bits * m_lambda2) >> 8); | |
102 | } | |
103 | ||
104 | inline uint64_t calcRdSADCost(uint32_t sadCost, uint32_t bits) const | |
105 | { | |
106 | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda, | |
b53f7c52 | 107 | "calcRdSADCost wrap detected dist: %u, bits %u, lambda: "X265_LL"\n", sadCost, bits, m_lambda); |
72b9787e JB |
108 | return sadCost + ((bits * m_lambda + 128) >> 8); |
109 | } | |
110 | ||
b53f7c52 | 111 | inline uint32_t scaleChromaDist(uint32_t plane, uint32_t dist) const |
72b9787e | 112 | { |
b53f7c52 JB |
113 | X265_CHECK(dist <= (UINT64_MAX - 128) / m_chromaDistWeight[plane - 1], |
114 | "scaleChromaDist wrap detected dist: %u, lambda: %u\n", dist, m_chromaDistWeight[plane - 1]); | |
115 | return (uint32_t)((dist * (uint64_t)m_chromaDistWeight[plane - 1] + 128) >> 8); | |
72b9787e JB |
116 | } |
117 | ||
118 | inline uint32_t getCost(uint32_t bits) const | |
119 | { | |
120 | return (uint32_t)((bits * m_lambda + 128) >> 8); | |
121 | } | |
122 | }; | |
123 | } | |
124 | ||
125 | #endif // ifndef X265_TCOMRDCOST_H |