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; | |
40 | uint64_t m_cbDistortionWeight; | |
41 | uint64_t m_crDistortionWeight; | |
42 | uint32_t m_psyRd; | |
43 | int m_qp; | |
44 | ||
45 | void setPsyRdScale(double scale) { m_psyRd = (uint32_t)floor(256.0 * scale * 0.33); } | |
46 | void setCbDistortionWeight(uint16_t weightFix8) { m_cbDistortionWeight = weightFix8; } | |
47 | void setCrDistortionWeight(uint16_t weightFix8) { m_crDistortionWeight = weightFix8; } | |
48 | ||
49 | void setQP(const Slice& slice, int qp) | |
50 | { | |
51 | m_qp = qp; | |
52 | ||
53 | setLambda(x265_lambda2_tab[qp], x265_lambda_tab[qp]); | |
54 | ||
55 | int qpCb = Clip3(QP_MIN, QP_MAX_MAX, qp + slice.m_pps->chromaCbQpOffset); | |
56 | int chroma_offset_idx = X265_MIN(qp - qpCb + 12, MAX_CHROMA_LAMBDA_OFFSET); | |
57 | uint16_t lambdaOffset = m_psyRd ? x265_chroma_lambda2_offset_tab[chroma_offset_idx] : 256; | |
58 | setCbDistortionWeight(lambdaOffset); | |
59 | ||
60 | int qpCr = Clip3(QP_MIN, QP_MAX_MAX, qp + slice.m_pps->chromaCrQpOffset); | |
61 | chroma_offset_idx = X265_MIN(qp - qpCr + 12, MAX_CHROMA_LAMBDA_OFFSET); | |
62 | lambdaOffset = m_psyRd ? x265_chroma_lambda2_offset_tab[chroma_offset_idx] : 256; | |
63 | setCrDistortionWeight(lambdaOffset); | |
64 | } | |
65 | ||
66 | void setLambda(double lambda2, double lambda) | |
67 | { | |
68 | m_lambda2 = (uint64_t)floor(256.0 * lambda2); | |
69 | m_lambda = (uint64_t)floor(256.0 * lambda); | |
70 | } | |
71 | ||
72 | inline uint64_t calcRdCost(uint32_t distortion, uint32_t bits) const | |
73 | { | |
74 | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda2, | |
75 | "calcRdCost wrap detected dist: %d, bits %d, lambda: %d\n", distortion, bits, (int)m_lambda2); | |
76 | return distortion + ((bits * m_lambda2 + 128) >> 8); | |
77 | } | |
78 | ||
79 | /* return the difference in energy between the source block and the recon block */ | |
80 | inline int psyCost(int size, pixel *source, intptr_t sstride, pixel *recon, intptr_t rstride) const | |
81 | { | |
82 | return primitives.psy_cost_pp[size](source, sstride, recon, rstride); | |
83 | } | |
84 | ||
85 | /* return the difference in energy between the source block and the recon block */ | |
86 | inline int psyCost(int size, int16_t *source, intptr_t sstride, int16_t *recon, intptr_t rstride) const | |
87 | { | |
88 | return primitives.psy_cost_ss[size](source, sstride, recon, rstride); | |
89 | } | |
90 | ||
91 | /* return the RD cost of this prediction, including the effect of psy-rd */ | |
92 | inline uint64_t calcPsyRdCost(uint32_t distortion, uint32_t bits, uint32_t psycost) const | |
93 | { | |
94 | return distortion + ((m_lambda * m_psyRd * psycost) >> 16) + ((bits * m_lambda2) >> 8); | |
95 | } | |
96 | ||
97 | inline uint64_t calcRdSADCost(uint32_t sadCost, uint32_t bits) const | |
98 | { | |
99 | X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda, | |
100 | "calcRdSADCost wrap detected dist: %d, bits %d, lambda: "X265_LL"\n", sadCost, bits, m_lambda); | |
101 | return sadCost + ((bits * m_lambda + 128) >> 8); | |
102 | } | |
103 | ||
104 | inline uint32_t scaleChromaDistCb(uint32_t dist) const | |
105 | { | |
106 | X265_CHECK(dist <= (UINT64_MAX - 128) / m_cbDistortionWeight, | |
107 | "scaleChromaDistCb wrap detected dist: %d, lambda: "X265_LL"\n", dist, m_cbDistortionWeight); | |
108 | return (uint32_t)(((dist * m_cbDistortionWeight) + 128) >> 8); | |
109 | } | |
110 | ||
111 | inline uint32_t scaleChromaDistCr(uint32_t dist) const | |
112 | { | |
113 | X265_CHECK(dist <= (UINT64_MAX - 128) / m_crDistortionWeight, | |
114 | "scaleChromaDistCr wrap detected dist: %d, lambda: "X265_LL"\n", dist, m_crDistortionWeight); | |
115 | return (uint32_t)(((dist * m_crDistortionWeight) + 128) >> 8); | |
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 |